其他分享
首页 > 其他分享> > HDU-1501 Zipper

HDU-1501 Zipper

作者:互联网

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

题目大意:给你三个字符串,问你前两个能否组成第三个,前两个串内部的顺序不能改变。

思路:dfs;

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[210],b[210],c[420];
int book[210][210];
int al,bl,cl;
int dfs(int s1,int s2,int sum)//s1为a字符串的下标,s2为b字符串的下标,sum为所组成的字符串下标
{
    if(sum>=cl)
        return 1;
    if(book[s1][s2])
        return 0;
    book[s1][s2]=1;
    if(a[s1]==c[sum])
    {
        if(dfs(s1+1,s2,sum+1))
            return 1;
    }
    if(b[s2]==c[sum])
    {
        if(dfs(s1,s2+1,sum+1))
            return 1;
    }
    return 0;
}
int main()
{
    int t;
    int o=1;
    scanf("%d",&t);
    while(t--)
    {
        memset(a,'\0',sizeof(a));
        memset(b,'\0',sizeof(b));
        memset(c,'\0',sizeof(c));
        memset(book,0,sizeof(book));
        scanf("%s %s %s",a,b,c);
        al=strlen(a);
        bl=strlen(b);
        cl=strlen(c);
        if(dfs(0,0,0))
            printf("Data set %d: yes\n",o++);
        else
            printf("Data set %d: no\n",o++);
    }
    return 0;
}

 

标签:HDU,set,int,sum,tree,cat,1501,Zipper,strings
来源: https://blog.csdn.net/XYBDX/article/details/96898680