ZipperTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9847 Accepted Submission(s): 3517
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
Sample Output
|
分析:
把B串的按串内字符顺序不变的原则插入A串,问能不能得到C串。
#include<stdio.h>
#include<string.h>
char a[210],b[210],c[450];
int l1,l2,ans,v[210][210];
void dfs(int x,int y)
{
if(ans)
return ;
if(v[x][y])//已经访问过的就返回上一轮
return;
if(x==l1&&y==l2)
{
ans=1;
return ;
}
int z=x+y;
if(a[x]==c[z])
dfs(x+1,y);
if(b[y]==c[z])
dfs(x,y+1);
v[x][y]=1;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
memset(v,0,sizeof(v));
scanf("%s%s%s",a,b,c);
l1=strlen(a);
l2=strlen(b);
ans=0;
dfs(0,0);
printf("Data set %d: ",i);
if(ans)
printf("yes\n");
else
printf("no\n");
}
return 0;
}