|
ZipperTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) 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 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 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 3 cat tree tcraete cat tree catrtee cat tree cttaree Sample Output Data set 1: yes Data set 2: yes Data set 3: no |
至今还在wa,已经无奈,,,,,贴着求大神找错;
代码:
1 #include<stdio.h> 2 #include<string.h> 3 char A[201],B[201],C[402],t1,t2,t3,flot; 4 void dfs(int len1,int len2,int len3){ 5 if(flot)return; 6 if(len3>=t3){ 7 flot=1; 8 return; 9 } 10 if(C[len3]==A[len1]){ 11 dfs(len1+1,len2,len3+1); 12 } 13 if(C[len3]==B[len2]){ 14 dfs(len1,len2+1,len3+1); 15 } 16 } 17 int main(){ 18 int n,tot=0; 19 scanf("%d",&n); 20 while(n--){flot=0;tot++; 21 memset(A,0,sizeof(A)); 22 memset(B,0,sizeof(B)); 23 memset(C,0,sizeof(C)); 24 scanf("%s%s%s",A,B,C); 25 t1=strlen(A); 26 t2=strlen(B); 27 t3=strlen(C); 28 if(t1+t2==t3&&(A[t1-1]==C[t3-1]||B[t2-1]==C[t3-1]))dfs(0,0,0); 29 if(flot)printf("Data set %d: yes\n",tot); 30 else printf("Data set %d: no\n",tot); 31 } 32 return 0; 33 }
大神的就能ac,感觉都差不多啊:
1 #include<stdio.h> 2 #include<string.h> 3 char a[220],b[220],c[220*2]; 4 bool flag; 5 int len1,len2,len3; 6 void dfs(int x,int y,int len) 7 { 8 if(flag) 9 return; 10 if(len>=len3) 11 { 12 flag=true; 13 return; 14 } 15 if(a[x]==c[len]) 16 { 17 dfs(x+1,y,len+1); 18 } 19 if(b[y]==c[len]) 20 dfs(x,y+1,len+1); 21 } 22 int main() 23 { 24 int t,cot=0; 25 scanf("%d",&t); 26 while(t--) 27 { 28 scanf("%s%s%s",a,b,c); 29 len1=strlen(a); 30 len2=strlen(b); 31 len3=strlen(c); 32 if(len1+len2!=len3) 33 { 34 printf("Data set %d: no\n",++cot); 35 continue; 36 } 37 flag=false; 38 if(a[len1-1]==c[len3-1]||b[len2-1]==c[len3-1]) 39 { 40 dfs(0,0,0); 41 } 42 if(flag) 43 printf("Data set %d: yes\n",++cot); 44 else 45 printf("Data set %d: no\n",++cot); 46 } 47 }