Two strings
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 492 Accepted Submission(s): 183
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
2017 Multi-University Training Contest - Team 9
参考http://blog.csdn.net/a664607530/article/details/77488213
题意:有两个字符串,问你第二个字符串和第一个字符串能否匹配,第二个字符串有两种符号,’.’可以匹配任意字符,’*’表示前一个字符可以重复零次或多次
体会:自己没想到可以用dp做,而且这个地方的a*的细节我也想不到。。。
char s1[N],s2[N];
int dp[N][N];
int main(){
int T;sf("%d",&T);
while(T--){
sf("%s",s1+1);sf("%s",s2+1);
int len1=strlen(s1+1);
int len2=strlen(s2+1);
mem(dp,0);
dp[0][0]=1;
for(int i=1;i<=len2;++i){
if(i==2&&s2[i]=='*')dp[2][0]=1;
for(int j=1;j<=len1;++j){
if(s2[i]=='.')dp[i][j]=dp[i-1][j-1];
else if(s2[i]!='*'){
if(s2[i]==s1[j])dp[i][j]=dp[i-1][j-1];
}
else{
dp[i][j]=max(dp[i-2][j],dp[i-1][j]);
if(dp[i][j-1]&&s1[j]==s1[j-1])dp[i][j]=1;
}
}
}
if(dp[len2][len1])puts("yes");
else puts("no");
}
}