A - Zipper


Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u


Submit   Status



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


 

解题报告:

设:A=a0a1a2...an-1,其中前缀串Ai=a0a1a2...ai(i>=0,i<=n-1);

       B=b0b1b2...bn-1,其中前缀串Bj=b0b1..bj(0<=j>=m-1);

      C=c0c1c2..cn-1,其中前缀串Ck=c0c1c2..ci(k>=0,k<=n+m-1);

can[i][j]为Ai-1(A 中长度为i的前缀串),和Bj成功组成Ci+j-1(C中长度为i+j的前缀串)的标志,can[0][0]=1;

当i>=1且ci+j-1=ai-1时,需要看Ai-2和Bj-1能否成功组成ci+j-2,即

   can[i][j]=can[i][j]||can[i-1][j];

当j>=1且ci+j-1=bj-1时,需要看ai-1和bj-2能否组成ci+j-2,即

 can[i][j]=can[i][j]||can[i][j-1];

最后can[n][m]就是最后结果。

代码:

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
	int t,i,j,n,m;
	char a[210],b[210],c[420];
	cin>>t;
	int k;
	int can[210][210];
	for(k=1;k<=t;k++)
	{
		
		cin>>a>>b>>c;
		n=strlen(a);
		m=strlen(b);
		cout<<"Data set "<<k;
		int i1=0,j1=0;
		memset(can,0,sizeof(can));
		can[0][0]=1;
		for(i=0;i<=n;i++)
		{
			for(j=0;j<=m;j++)
			{
				if(i>=1&&a[i-1]==c[i+j-1])
					can[i][j]=can[i][j]||can[i-1][j];
				if(j>=1&&b[j-1]==c[i+j-1])
					can[i][j]=can[i][j]||can[i][j-1];
			}
		}
		if(can[n][m])
                cout<<": yes"<<endl;
			else
				cout<<": no"<<endl;
	}
	return 0;
}