zoj 2401 Zipper

刚开始想着只要遍历2遍第三个串,分别找到第一第二个串的所有字符,事实证明是错了,直到看了dp思想。。。。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define M 205
char a[M],b[M],c[2*M];
int dp(int x,int y){
    int t,f;
    if(x==-1&&y==-1)return 1;
    t=x+y+1;
    if(y!=-1&&c[t]==b[y]){
        f=dp(x,y-1);
        if(f==1)return  f;
    }
    if(x!=-1&&c[t]==a[x])return  dp(x-1,y);
    return 0;      
}
int main(){
    int T,cas,la,lb,lc,f;
    while(~scanf("%d",&T)){
        cas=0;
        while(T--){
            scanf("%s %s %s",a,b,c);
            la=strlen(a); lb=strlen(b); lc=strlen(c);
            f=dp(la-1,lb-1);
            if(f==1)
                printf("Data set %d: yes\n",++cas);
            else printf("Data set %d: no\n",++cas);
        }
    }
    return 0;
}

你可能感兴趣的:(dp,ZOJ)