Zipper |
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB |
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 |
////////// 解题报告
题目意思: 给出三个字符串,求第三个字符串里是否按顺序的包含了前面两个串
方法: 对于第三个串里某位置的字符,要么属于第一个串,要么属于第二个串,如果按顺序不属于任何一个串,那么肯定不包含,如果按顺序既可以是第一个串,也可以是第二个串,则该位置需要入栈。。。。
//// mycode
#include <stdio.h>
char a[202], b[202], str[404];
struct node{
int i,j,k;
}stack[202];
int test(){
int i,j,k,sp;
for(i=j=k=sp=0; str[k]; k++){
if(str[k]==a[i]){
if(str[k]==b[j]){
stack[sp].i=i;
stack[sp].j=j;
stack[sp].k=k;
sp++;
}
i++;
}else if(str[k]!=b[j]&&b[j]){
if(sp==0)
break;
sp--;
i=stack[sp].i;
j=stack[sp].j;
k=stack[sp].k;
j++;
}else if(b[j])
j++;
}
if(str[k]||a[i]||b[j])
return 0;
return 1;
}
int main(){
int n, i;
scanf("%d",&n);
for(i=1; i<=n; i++){
scanf("%s %s %s",a,b,str);
if(test())
printf("Data set %d: yes\n",i);
else
printf("Data set %d: no\n",i);
}
return 0;
}
评论