Zipper
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 118 Accepted Submission(s) : 53
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
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
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
3 cat tree tcraete cat tree catrtee cat tree cttaree
Sample Output
Data set 1: yes Data set 2: yesData set 3: no
#include<iostream> #include<cstdio> #include<algorithm> #include<string.h> char s1[201],s2[201],s3[402]; int len,cas;bool bb,b[201][201]; using namespace std; int main() { void dfs(int,int,int); int T;scanf("%d",&T); cas=0; while(T--) { memset(b,0,sizeof(b)); bb=0; cas++; scanf("%s%s%s",&s1,&s2,&s3); len=strlen(s3); dfs(0,0,0); if(bb==0) printf("Data set %d: no\n",cas); } return 0; } void dfs(int v1,int v2,int v3) { b[v1][v2]=1; if(bb==1) return; if(v3==len) { printf("Data set %d: yes\n",cas); bb=1;return; } if(s1[v1]==s3[v3]) { if(b[v1+1][v2]==0) dfs(v1+1,v2,v3+1),b[v1+1][v2]=1; } if(s2[v2]==s3[v3]) { if(b[v1][v2+1]==0) dfs(v1,v2+1,v3+1),b[v1][v2+1]=1; } if(bb==1) return; }