HDU 1501 Zipper

记录一下第i个放入之后可能分割的情况,然后可以推出放入第i+1个的分割情况。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;

const int maxn=500;
char s1[maxn],s2[maxn],s[maxn];
struct X
{
    int A,B;
    X(int a,int b)
    {
        A=a;
        B=b;
    }
};

int main()
{
    int T; scanf("%d",&T);
    for(int Case=1;Case<=T;Case++)
    {
        memset(s1,0,sizeof s1);
        memset(s2,0,sizeof s2);
        memset(s,0,sizeof s);

        scanf("%s%s%s",s1,s2,s);

        queue<X>Q[2]; int now=0;

        map<int,bool>m;
        int lenA=strlen(s1),lenB=strlen(s2),lenS=strlen(s);

        m[0]=1;
        Q[now].push(X(0,0));

        for(int i=0;s[i];i++)
        {
            m.clear();
            now=now^1;
            while(!Q[now^1].empty())
            {
                X head=Q[now^1].front(); Q[now^1].pop();
                if(s1[head.A]==s[i]&&head.B+lenS-i>=lenB&&m[head.A+1]==0){
                    Q[now].push(X(head.A+1,head.B));
                    m[head.A+1]=1;
                }
                if(s2[head.B]==s[i]&&head.A+lenS-i>=lenA&&m[head.A]==0){
                    Q[now].push(X(head.A,head.B+1));
                    m[head.A]=1;
                }
            }
        }

        int ans=0;

        while(!Q[now].empty())
        {
            X head=Q[now].front(); Q[now].pop();
            if(head.A==lenA&&head.B==lenB) ans=1;
        }
        printf("Data set %d: ",Case);
        if(ans==1) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}
posted @ 2016-05-02 14:25  Fighting_Heart  阅读(198)  评论(0编辑  收藏  举报