hdu6170-多看幾遍之DP&遞推&字串-Two strings
阿新 • • 發佈:2018-12-24
http://acm.hdu.edu.cn/showproblem.php?pid=6170
給定兩個串。
a串是正常串。
b串中有兩種元素。
1 種是* ,可以讓前面那個出現任意次(甚至讓他出現0次)
2 是. 和任意字元匹配。
too young 系列。
日後,用搜索也寫寫,應該也行。
可以發現 幾種狀態轉移。
① 當為 . 時,這時沒的說,由dp[i-1][j-1】得來
② 當為 * 時,分為三種情況。
① 把前面的和自己都清0, 這樣為dp[i-2][j].
② 把自己置0 這樣為dp[i-1][0]
③ 當此狀態或者上一次 的和j-1可以匹配,(由之間的匹配的)
並且 b[j-1]==b[j]則可以匹配。
③ 當相等時,由dp[i-1][j-1].
#include <bits/stdc++.h>
using namespace std;
/* dp太爛。
還是 too young
*/
const int maxn=2400;
bool dp[maxn][maxn];
char a[maxn];
char b[maxn];
int main()
{ int t;
scanf("%d",&t);
while(t--){
cin>>a+1;
cin>>b+1;
int len1=strlen(a+1);
int len2=strlen(b+1);
//cout<<len1<<endl;
//cout<<len2<<endl;
memset(dp,false,sizeof(dp));
dp[0][0]=true;
for(int i=1;i<=len2;i++){
if(i>=2&&b[i]=='*')
dp[i][0]=true;//全部置為0
for(int j=1;j<=len1;j++){
if (b[i]=='.'||b[i]==a[j])
dp[i][j]=dp[i-1][j-1];
else if(b[i]=='*'){
dp[i][j]=dp[i-1][j]|dp[i-2][j];
if((dp[i-1][j-1]||dp[i][j-1])&&a[j]==a[j-1])
dp[i][j]=true;
}
}
}
if(dp[len2][len1])
puts("yes");
else
puts("no");
}
return 0;
}