hdu 1501 Zipper

http://acm.hdu.edu.cn/showproblem.php?pid=1501

两种做法:1.dfs,2,直接推

 1 #include <cstdio>

 2 #include <iostream>

 3 #include <cstring>

 4 #include <algorithm>

 5 #define maxn 1000

 6 using namespace std;

 7 

 8 int dp[maxn][maxn];

 9 char str1[maxn],str2[maxn],str3[maxn];

10 bool flag;

11 bool vis[maxn][maxn];

12 

13 void dfs(int p1,int p2,int pos)

14 {

15     if(str3[pos]=='\0') flag=true;

16     if(flag||vis[p1][p2]) return ;

17     vis[p1][p2]=true;

18     if(str1[p1]==str3[pos])

19         dfs(p1+1,p2,pos+1);

20     if(str2[p2]==str3[pos])

21         dfs(p1,p2+1,pos+1);

22 }

23 

24 int main()

25 {

26     int n;

27     while(scanf("%d",&n)!=EOF)

28     {

29         for(int cas=1; cas<=n; cas++)

30         {

31             scanf("%s %s %s",str1+1,str2+1,str3+1);

32             str1[0]='@'; str2[0]='@';str3[0]='@';

33             int k1=strlen(str1);

34             int k2=strlen(str2);

35             for(int i=1; i<k1; i++)

36             {

37                 if(str1[i]==str3[i]) dp[i][0]=1;

38             }

39             for(int i=1; i<k2; i++)

40             {

41                 if(str2[i]==str3[i]) dp[0][i]=1;

42             }

43             for(int i=1; i<k1; i++)

44             {

45                 for(int j=1; j<k2; j++)

46                 {

47                     dp[i][j]=(dp[i-1][j]&&str1[i]==str3[i+j])||(dp[i][j-1]&&str2[j]==str3[i+j]);

48                 }

49             }

50             printf("Data set %d: ",cas);

51             memset(vis,false,sizeof(vis));

52             flag=false;

53             dfs(1,1,1);

54             if(flag) printf("yes\n");

55             else printf("no\n");

56         }

57     }

58     return 0;

59 }
View Code

你可能感兴趣的:(zip)