Zipper

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8580    Accepted Submission(s): 3028

 

 

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

3

cat tree tcraete

cat tree catrtee

cat tree cttaree

 

 

Sample Output

Data set 1: yes

Data set 2: yes

Data set 3: no

 

 

Source

Pacific Northwest 2004

 

 

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int maxn=1010;
 6 bool v[maxn][maxn];
 7 int f=0;
 8 int l1,l2,l3;
 9 char a[maxn],b[maxn],c[maxn];
10 void dfs(int x1,int x2,int x3)
11 {
12     if(x3>=l3) {
13         f=1;
14         return ;
15     }
16     if(f==1) return ;
17     if(v[x1][x2]) return ;
18     if(a[x1]==c[x3]) dfs(x1+1,x2,x3+1);
19     if(b[x2]==c[x3]) dfs(x1,x2+1,x3+1);
20     v[x1][x2]=1;
21 }
22 int main()
23 {
24     int n;
25     cin>>n;
26     for(int i=1;i<=n;i++){
27         cout<<"Data set "<<i<<": ";
28         scanf("%s%s%s",a,b,c);
29         memset(v,0,sizeof(v));
30         l1=strlen(a);
31         l2=strlen(b);
32         l3=strlen(c);
33         if(l1+l2!=l3){
34             cout<<"no"<<endl;
35             continue;
36         }
37         else{
38             f=0;
39             dfs(0,0,0);
40             if(f==0) cout<<"no"<<endl;
41             else cout<<"yes"<<endl;
42         }
43     }
44     return 0;
45 }

 

posted on 2016-01-29 20:37  Sunny糖果  阅读(124)  评论(0编辑  收藏  举报