hdu 6170 Two strings

dp[i][j]代表s1的前i个字符和s2的前j个字符匹配的情况,记录此时s2[j-1]匹配的字符。-1代表不匹配,0代表s2[j-1]匹配空字符。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<cmath>
#include<set>
#include<stack>
#define ll long long
#define pb push_back
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
#define cls(name,x) memset(name,x,sizeof(name))
#define fs first
#define sc second
#define mp make_pair
#define L(x) (1<<x)
#define next Next
using namespace std;
const int inf=1e9+10;
const ll llinf=1e16+10;
const int maxn=25e2+10;
const int maxm=1e3+10;
const int mod=1e9+7;
char s1[maxn],s2[maxn];
int len1,len2;
char dp[maxn][maxn];
int main()
{
    //freopen("in.txt","r",stdin);
    int ncas;
    scanf("%d",&ncas);
    while(ncas--)
    {
        scanf("%s %s",s1,s2);
        cls(dp,-1);
        len1=strlen(s1);
        len2=strlen(s2);
        dp[0][0]=0;
        for(int i=0;i<=len1;i++)
        {
            for(int j=1;j<=len2;j++)
            {
                if(s1[i-1]==s2[j-1] && dp[i-1][j-1]!=-1 &&i!=0)
                    dp[i][j]=s1[i-1];
                else if( s2[j-1]=='.' && dp[i-1][j-1]!=-1 &&i!=0)
                    dp[i][j]=s1[i-1];
                else if(s2[j-1]=='*')
                {
                    if(dp[i-1][j-1]==s1[i-1])//由前一个重复
                        dp[i][j]=s1[i-1];
                    else if(dp[i-1][j]==s1[i-1])//当前的重复
                        dp[i][j]=s1[i-1];
                    if((dp[i][j-2]!=-1&&j!=1)||dp[i][j-1]!=-1)//重复0次或1次
                        dp[i][j]=0;
                }
            }
        }
        printf("%s\n",dp[len1][len2]!=-1?"yes":"no");
    }
    return 0;
}

 

posted @ 2017-08-22 20:23  爱种树的码农  阅读(122)  评论(0编辑  收藏  举报