hdu6170Two strings(第九场递推dp)

Two strings

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1421    Accepted Submission(s): 584


Problem Description
Giving two strings and you should judge if they are matched.
The first string contains lowercase letters and uppercase letters.
The second string contains lowercase letters, uppercase letters, and special symbols: “.” and “*”.
. can match any letter, and * means the front character can appear any times. For example, “a.b” can match “acb” or “abb”, “a*” can match “a”, “aa” and even empty string. ( “*” will not appear in the front of the string, and there will not be two consecutive “*”.
 

Input
The first line contains an integer T implying the number of test cases. (T≤15)
For each test case, there are two lines implying the two strings (The length of the two strings is less than 2500).
 

Output
For each test case, print “yes” if the two strings are matched, otherwise print “no”.
 

Sample Input
 
  
3 aa a* abb a.* abb aab
 

Sample Output
 
  
yes yes no
 

Source


求两串能否在两个条件下匹配成功,a串只有大小写,b串有大小写,‘.’,‘*’,点代表可以成为任何字符,*代表可以让前面字符出现任意次。

则将dp【ij】,设置为b串前i个字符和a串前j个字符匹配的结果,则dp[0][0]为1,那么我们则有两种情况
1.出现'点'符号,则有dp[i][j]=dp[i-1][j-1],两个的前面相同就行了。
2.出现*符号,则如果a串中出现了a[i]==a[i-1],的话则代表b中字符要出现多次,也就是dp[i][j]=dp[i][j-1];  其次就是字符出现单次的情况了,那么也就是dp[i][j]=dp[i-1][j];  还有一种情况,字符为空的情况,也就是a中当前字符我们可以不用看,直接dp[i][j-1]=dp[i-2[j-1];
\




#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#define clr(i,n) for(int i=0;i<n;i++)
#define N 3050
using namespace std;
char s1[N];
char s2[N];
bool dp[N][N];
int main()
{
    int t;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        gets(s1+1);
        gets(s2+1);
        int len1=strlen(s1+1);
        int len2=strlen(s2+1);
        memset(dp,0,sizeof(dp));
        dp[0][0]=true;
        for(int i=1;i<=len2;i++)
        {
            for(int j=1;j<=len1;j++)
            {
                if(s2[i]=='.'||s1[j]==s2[i])
                {
                    dp[i][j]|=dp[i-1][j-1];
                }
                else if(s2[i]=='*')
                {
                    if(s1[j]==s1[j-1])
                    {
                        dp[i][j]|=dp[i][j-1];///多个相同字符
                    }
                    dp[i][j]|=dp[i-1][j];///*为单个字符
                    dp[i][j-1]|=dp[i-2][j-1];///空字符
                }
            }
        }
        if(dp[len2][len1])
        {
            cout<<"yes"<<endl;
        }
        else
        cout<<"no"<<endl;
    }
}


猜你喜欢

转载自blog.csdn.net/qq_32792879/article/details/77568254
今日推荐