hdu6170-多看几遍之DP&递推&字符串-Two strings

来源:互联网 发布:ipadapp下载不了软件 编辑:程序博客网 时间:2024/06/07 19:18

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;}
原创粉丝点击