Zipper Hang electric 1501

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

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

OutputFor 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

1 SET the Data: Yes 
the Data SET 2: Yes 
NO: 3 SET the Data 
suggest the effect that the composition of the case where the subject does not change the order of the string 1 and string 2, string 3
idea of using a two-dimensional array :: dp [X position] [y] is marked strings and string position X 1 y 2 whether accessed
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
string a,b,c;
int flag=0;
int dp[201][201];
void dfs(int x,int y,int z){
    if(flag) return;
    if(x==a.size()&&y==b.size()){
        flag=1;
        return ;
    }
    if(dp[x][y]) return ;
    dp[x][y]=1;
    if(a[x]==c[z]){
        
        dfs(x+1,y,z+1);
    }
    if(b[y]==c[z]){
        dfs(x,y+1,z+1);
    }
}
int main()
{
    int t;
    cin>>t;
    for(int i=1;i<=t;i++){
        cin>>a>>b>>c;
        memset(dp,0,sizeof(dp));
        flag=0;
        dfs(0,0,0);
        printf("Data set %d: ",i); 
        if(flag)
            puts("yes");
        else puts("no");
        a="",b="",c="";
    }
    return 0;
}

 






Guess you like

Origin www.cnblogs.com/Accepting/p/11271022.html