题目链接
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
思路:我们规定a为原串,b为匹配串,定义dp【i】【j】表示b【1.。。i】到a【1.。。。j】是否匹配,首先当b【i】==‘.'或者a【j】==b【i】的时候直接从dp【i-1】【j-1】转移过来就行了,关键是b【i】为’“时,dp【i】【j】可以从dp【i-2】【j】和dp【i-1】【j】转移过来(之前一直不明白为什么能从i-2转移过来,后来才知道,(a)TM可以与空串匹配。。。如果a【j】==a【j-1】的话我们可以之前定义dp【i】【j】等于1`
#include<bits/stdc++.h>
using namespace std;
const int maxn=3e3+1;
char s1[maxn],s2[maxn];
int T,dp[maxn][maxn];
int main()
{
scanf("%d",&T);
while(T--)
{
memset(dp,0,sizeof(dp));
s1[0]=s2[0]=1;
scanf("%s %s",s1+1,s2+1);
int len1=strlen(s1)-1,len2=strlen(s2)-1;
dp[0][0]=1;
for(int i=1;i<=len2;++i)
{
if(i>=2&&s2[i]=='*') dp[i][0]|=dp[i-2][0];
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]=='*')
{
dp[i][j]=dp[i-1][j]|dp[i-2][j];
if((dp[i-1][j-1]||dp[i][j-1])&&s1[j-1]==s1[j]) dp[i][j]=1;
}
}
}
printf("%s\n",dp[len2][len1]?"yes":"no");
}
}