您的位置:首页 > 其它

HDU1501zipper

2016-05-02 13:20 381 查看

Zipper

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8968 Accepted Submission(s): 3192[align=left]Problem Description[/align]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: catString B: treeString C: tcraeteAs 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: catString B: treeString C: catrteeFinally, notice that it is impossible to form "cttaree" from "cat" and "tree".[align=left]Input[/align]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 dataset 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 twostrings will have lengths between 1 and 200 characters, inclusive.[align=left]Output[/align]For each data set, print:Data set n: yesif the third string can be formed from the first two, orData set n: noif it cannot. Of course n should be replaced by the data set number. See the sample output below for an example.[align=left]Sample Input[/align]
3
cat tree tcraete
cat tree catrtee
cat tree cttaree
[align=left]Sample Output[/align][align=left]Data set 1: yes[/align]Data set 2: yesData 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;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: