Problem descriptiongiven 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 shocould be replaced by the data set number. See the sample output below for an example.
Sample Input
3cat tree tcraetecat tree catrteecat tree cttaree
Sample output
Data set 1: yesData set 2: yesData set 3: no
Both sourcepacific northwest 2004 memory and DP can combine the third string through two strings. The order remains unchanged. Memory:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<limits.h>typedef long long LL;using namespace std;const int maxn=220;char s1[maxn],s2[maxn],s[maxn<<1];int dp[maxn][maxn];int len,len1,len2;int t;int dfs(int i,int j,int k){ if(k==len) return 1; if(dp[i][j]) return 0; dp[i][j]=1; if(s1[i]==s[k]) { if(dfs(i+1,j,k+1)) return 1; } if(s2[j]==s[k]) { if(dfs(i,j+1,k+1)) return 1; } return 0;}int main(){ int l=0; scanf("%d",&t); while(t--) { scanf("%s%s%s",s1,s2,s); len1=strlen(s1); len2=strlen(s2); len=strlen(s); memset(dp,0,sizeof(dp)); if(dfs(0,0,0)) printf("Data set %d: yes\n",++l); else printf("Data set %d: no\n",++l); } return 0;}
DP:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<limits.h>using namespace std;const int maxn=220;char s1[maxn],s2[maxn],s[maxn<<1];int dp[maxn][maxn];int main(){ int t; int l=0; scanf("%d",&t); getchar(); while(t--) { scanf("%s%s%s",s1+1,s2+1,s+1); int len1=strlen(s1+1); int len2=strlen(s2+1); int len=strlen(s+1); memset(dp,0,sizeof(dp)); dp[0][0]=1; for(int i=0;i<=len1;i++) { for(int j=0;j<=len2;j++) { if(i==0&&j==0) continue; if(s[i+j]==s1[i]&&i&&dp[i-1][j]) dp[i][j]=1; if(s[i+j]==s2[j]&&j&&dp[i][j-1]) dp[i][j]=1; } } if(dp[len1][len2]) printf("Data set %d: yes\n",++l); else printf("Data set %d: no\n",++l); } return 0;}