题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6170
题意:给了2个字符串,其中第2个字符串包含.和*两种特别字符,问第二个字符串能否和第一个匹配。
解法:dp[i][j]代表在第一个串的i位置,第2个串的j位置是否可以匹配,然后按照'*'这个特殊情况讨论转移即可。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3005;
bool dp[maxn][maxn];
char s1[maxn],s2[maxn];
bool match(char c1, char c2){
if(c2=='.') return 1;
if(c1==c2) return 1;
return 0;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(dp, 0, sizeof(dp));
scanf("%s %s", s1+1,s2+1);
int len1 = strlen(s1+1);
int len2 = strlen(s2+1);
dp[0][0]=1;
for(int i=0; i<=len1; i++){
for(int j=1; j<=len2; j++){
if(i>=1){
if(match(s1[i],s2[j])) dp[i][j]|=dp[i-1][j-1];
}
if(s2[j]=='*'){
if(j>=2) dp[i][j]|=dp[i][j-2];
if(i){
int c=s2[j-1];
if(match(s1[i],c)){
dp[i][j]|=dp[i][j-1];
if(s1[i]==s1[i-1]){
dp[i][j]|=dp[i-1][j];
}
}
}
}
}
}
if(dp[len1][len2]) puts("yes");
else puts("no");
}
return 0;
}