HDOJ Zipper (DFS)

来源:互联网 发布:中国印度边境冲突 知乎 编辑:程序博客网 时间:2024/06/05 15:42

Zipper

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 2   Accepted Submission(s) : 1
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: yesif the third string can be formed from the first two, or Data set n: noif 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: yesData set 2: yesData set 3: no
 




ac代码:

#include<stdio.h>  #include<string.h>  #include<iostream>#include<algorithm>using namespace std;char str1[205],str2[205],str3[405];  int bz;  int len1,len2,len3;  int v[205][205];  void dfs(int a,int b,int c)  {      if(bz)      return;      if(c==len3)      {          bz=1;          return;      }      if(v[a][b])  //标记不能少    return;      v[a][b]=1;      if(str1[a]==str3[c])      dfs(a+1,b,c+1);      if(str2[b]==str3[c])      dfs(a,b+1,c+1);  }  int main()  {      int i,t;      int cas=0;    scanf("%d",&t);      while(t--)      {          scanf("%s%s%s",str1,str2,str3);          printf("Data set %d: ",++cas);          len1=strlen(str1);          len2=strlen(str2);          len3=strlen(str3);          if(len1+len2!=len3)          {              printf("no\n");              continue;          }          memset(v,0,sizeof(v));          bz=0;          dfs(0,0,0);          if(bz)          printf("yes\n");          else          printf("no\n");      }      return 0;  }        


0 0
原创粉丝点击