动态规划进阶题目之Zipper

时间: 1ms        内存:64M

描述:

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".

输入:

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

输出:

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.

示例输入:

3
cat tree tcraete
cat tree catrtee
cat tree cttaree

示例输出:

Data set 1: yes
Data set 2: yes
Data set 3: no

提示:

参考答案(内存最优[1096]):

    #include <cstdio>  
    #include <cstring>   
    #define N 247  
    char a[N], b[N], c[N+N];  
    int len1, len2, len3;  
    int dfs(int x, int y, int sum)  
    {  
        if(sum >len3)  
        return 1;  
        if(a[x]!=c[sum] && b[y]!=c[sum])  
        return 0;  
        if(a[x]==c[sum] && dfs(x+1,y,sum+1))  
        return 1;  
        if(b[y]==c[sum] && dfs(x,y+1,sum+1))  
        return 1;  
        return 0;  
    }  
    int main()  
    {  
        int t, cas = 0;  
        while(~scanf("%d",&t))  
        {  
            while(t--)  
            {  
                scanf("%s%s%s",a+1,b+1,c+1);//从字符串下标为1开始输入   
                len1 = strlen(a+1);  
                len2 = strlen(b+1);  
                len3 = strlen(c+1);  
                int flag = 0;  
                if(c[len3]==a[len1] || c[len3]==b[len2])//优化,如果C最后的字母是a的最后一个   
                {                                       //或者b的最后一个才有可能是由它们组成的   
                    flag = dfs(1, 1, 1);  
                }  
                if(flag == 1)  
                printf("Data set %d: yes\n",++cas);  
                else  
                printf("Data set %d: no\n",++cas);  
            }  
        }  
        return 0;  
    }  

参考答案(时间最优[0]):

    #include <cstdio>  
    #include <cstring>   
    #define N 247  
    char a[N], b[N], c[N+N];  
    int len1, len2, len3;  
    int dfs(int x, int y, int sum)  
    {  
        if(sum >len3)  
        return 1;  
        if(a[x]!=c[sum] && b[y]!=c[sum])  
        return 0;  
        if(a[x]==c[sum] && dfs(x+1,y,sum+1))  
        return 1;  
        if(b[y]==c[sum] && dfs(x,y+1,sum+1))  
        return 1;  
        return 0;  
    }  
    int main()  
    {  
        int t, cas = 0;  
        while(~scanf("%d",&t))  
        {  
            while(t--)  
            {  
                scanf("%s%s%s",a+1,b+1,c+1);//从字符串下标为1开始输入   
                len1 = strlen(a+1);  
                len2 = strlen(b+1);  
                len3 = strlen(c+1);  
                int flag = 0;  
                if(c[len3]==a[len1] || c[len3]==b[len2])//优化,如果C最后的字母是a的最后一个   
                {                                       //或者b的最后一个才有可能是由它们组成的   
                    flag = dfs(1, 1, 1);  
                }  
                if(flag == 1)  
                printf("Data set %d: yes\n",++cas);  
                else  
                printf("Data set %d: no\n",++cas);  
            }  
        }  
        return 0;  
    }  

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。