Zipper 杭电 1501
阅读原文时间:2023年07月12日阅读:1

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

Data set 1: yes
Data set 2: yes
Data set 3: no
题目大意就是 在不改变字符串1和字符串2的顺序的情况下能否组成字符串3
思路::用一个二维数组dp[x][y]来标记字符串1的位置X和字符串2的位置y是否被访问过

#include
#include
#include
using namespace std;
string a,b,c;
int flag=;
int dp[][];
void dfs(int x,int y,int z){
if(flag) return;
if(x==a.size()&&y==b.size()){
flag=;
return ;
}
if(dp[x][y]) return ;
dp[x][y]=;
if(a[x]==c[z]){

    dfs(x+,y,z+);  
}  
if(b\[y\]==c\[z\]){  
    dfs(x,y+,z+);  
}  

}
int main()
{
int t;
cin>>t;
for(int i=;i<=t;i++){ cin>>a>>b>>c;
memset(dp,,sizeof(dp));
flag=;
dfs(,,);
printf("Data set %d: ",i);
if(flag)
puts("yes");
else puts("no");
a="",b="",c="";
}
return ;
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章