HDU1501zipper

来源:互联网 发布:非农数据分析 编辑:程序博客网 时间:2024/06/05 14:44

Zipper

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8968 Accepted Submission(s): 3192


Problem Description
Given three strings, you are to determine whether the third string can be formed by combining the characters in the first two strings. The first two strings can be mixed arbitrarily, but each must stay in its original order.

For example, consider forming "tcraete" from "cat" and "tree":

String A: cat
String B: tree
String C: tcraete


As you can see, we can form the third string by alternating characters from the two strings. As a second example, consider forming "catrtee" from "cat" and "tree":

String A: cat
String B: tree
String C: catrtee


Finally, notice that it is impossible to form "cttaree" from "cat" and "tree".

Input
The first line of input contains a single positive integer from 1 through 1000. It represents the number of data sets to follow. The processing for each data set is identical. The data sets appear on the following lines, one data set per line.

For each data set, the line of input consists of three strings, separated by a single space. All strings are composed of upper and lower case letters only. The length of the third string is always the sum of the lengths of the first two strings. The first two strings will have lengths between 1 and 200 characters, inclusive.


Output
For each data set, print:

Data set n: yes

if the third string can be formed from the first two, or

Data set n: no

if it cannot. Of course n should be replaced by the data set number. See the sample output below for an example.

Sample Input
3cat tree tcraetecat tree catrteecat tree cttaree

Sample Output
Data set 1: yes
Data set 2: yes

Data set 3: no

三个例子里面排序过程是这样的:用最后一个和前面两个数组长度比较大小,可以剪枝,当len1和len2和!=len3时结束,同时搜索时i增加时k增加,如果不满足再回头

看b,和c搜索比较,j和k增加搜索,直到搜到c的最后一个字符。

#include <iostream>#include <cstdio>#include <cstring>using namespace std;int vist[201][201];char a[201],b[201],c[401];int len1,len2,len3;int dfs(int i,int j,int k){    if(c[k]=='\0')        return 1;    if(vist[i][j])        return 0;    vist[i][j]=1;    if(len1<len3)        if(a[i]==c[k]&&dfs(i+1,j,k+1))            return 1;    if(len2<len3)        if(b[j]==c[k]&&dfs(i,j+1,k+1))            return 1;    return 0;}int main(){    int t,k;    while(scanf("%d",&t)!=EOF)    {        for(k=1; k<=t; k++)        {            scanf("%s%s%s",a,b,c);            len1=strlen(a);            len2=strlen(b);            len3=strlen(c);            memset(vist,0,sizeof(vist));            if(dfs(0,0,0)==1)                printf("Data set %d: yes\n",k);            else                printf("Data set %d: no\n",k);        }    }    return 0;}

还有直接递归的代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int main(){    int t,k;    char s1[210],s2[210],s3[500];    int dp[210][210];    int len1,len2;    while(scanf("%d",&t)!=EOF)    {        for(k=1; k<=t; k++)        {            memset(dp,0,sizeof(dp));            scanf("%s%s%s",s1,s2,s3);            len1=strlen(s1);            len2=strlen(s2);            if(s3[0]==s1[0])                dp[1][0]=1; //---------            else if(s3[0]==s2[0])//    |---两个为记忆化搜索标记                dp[0][1]=1;//----------            for(int i=1; i<=len1; i++)                for(int j=1; j<=len2; j++) //不能顺序反过来 "cttaree" from "cat" and "tree".                {                    if(dp[i-1][j])//第一个字符串排序                    {                        if(s3[i+j-1]==s1[i-1])                            dp[i][j]=1;                        if(s3[i+j-1]==s2[j])                            dp[i-1][j+1]=1;                    }                    else if(dp[i][j-1])//第二个字符串排序                    {                        if(s3[i+1-1]==s1[i])                            dp[i+1][j-1]=1;                        if(s3[i+j-1]==s2[j-1])                            dp[i][j]=1;                    }                }            if(dp[len1][len2])                printf("Data set %d: yes\n",k);            else                printf("Data set %d: no\n",k);        }    }    return 0;}



0 0
原创粉丝点击