hdu6170(dp)

来源:互联网 发布:大学 知乎 编辑:程序博客网 时间:2024/06/16 17:40

Two strings

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


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
3aaa*abba.*abbaab
 

Sample Output
yesyesno
 

#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;char s1[2550];char s2[2550];bool dp[2550][2550];int len1,len2;void solve(){memset(dp,0,sizeof(dp));dp[0][0]=1;if (s2[2]=='*')dp[0][2]=1;for(int i=1;i<=len1;i++){for(int j=1;j<=len2;j++){if(s2[j]=='.'||s2[j]==s1[i])dp[i][j]=dp[i-1][j-1];else if(s2[j]=='*'){dp[i][j]=max(dp[i][j-1],dp[i][j-2]);if (dp[i][j])continue;if (s1[i]==s1[i-1])dp[i][j]=max(dp[i-1][j],dp[i-2][j]);}}}if(dp[len1][len2])printf("yes\n");elseprintf("no\n");}int main(){int t;scanf("%d",&t);while(t--){scanf("%s",s1+1);scanf("%s",s2+1);len1=strlen(s1+1);len2=strlen(s2+1);solve();}}





原创粉丝点击