Two strings

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


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
 千千详细分析
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<set>
 7 #include<map>
 8 #include<queue>
 9 #include<stack>
10 #include<vector>
11 using namespace std;
12 typedef long long ll;
13 #define PI acos(-1.0)
14 int t;
15 char a[2505],b[2505];
16 int dp[2505][2505];
17 int main()
18 {
19     scanf("%d",&t);
20     while(t--){
21         scanf("%s",a+1);
22         scanf("%s",b+1);
23         memset(dp,0,sizeof(dp));
24         int lena=strlen(a+1);
25         int lenb=strlen(b+1);
26         dp[0][0]=1;
27         for(int i=1;i<=lenb;i++){
28             if(i==2&&b[i]=='*')
29              dp[i][0]=1;
30             for(int j=1;j<=lena;j++){
31                 if(b[i]=='.'||b[i]==a[j])
32                     dp[i][j]=dp[i-1][j-1];
33                 else if(b[i]=='*'){
34                     dp[i][j]=dp[i-2][j]|dp[i-1][j];
35                     if((dp[i-1][j-1]||dp[i][j-1])&&a[j-1]==a[j])
36                     dp[i][j]=1;
37                 }
38             }
39         }
40         if(dp[lenb][lena]==1)
41             printf("yes\n");
42         else
43             printf("no\n");
44     }
45     return 0;
46 }