正文

Zipper(字符串处理)2007-10-08 13:47:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lingdlz/29951.html

分享到:

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;
}

阅读(3939) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册