Problem Description
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".
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.
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.
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
dp解法:
dp[i][j]代表a中1~i 和b中1~j 能否拼成c的1~i+j
转移方程 dp[i][j] = (dp[i - 1][j] && (a[i] == c[i + j])) || (dp[i][j - 1] && (b[j] == c[i + j]));
#include<cstdio>
#include<cstring>
using namespace std;
char a[300], b[300], c[500];
int dp[300][300];
int main()
{
int n,k=1;
scanf("%d", &n);
a[0] = 1; b[0] = 1; c[0] = 1;
while (n--)
{
scanf("%s%s%s", a+1, b+1, c+1);
int la = strlen(a), lb = strlen(b);
memset(dp, 0, sizeof(dp));
for (int i = 1; i < la; i++)
if (a[i] == c[i])dp[i][0] = 1;
for (int i = 1; i < lb; i++)
if (b[i] == c[i])dp[0][i] = 1;
for (int i = 1; i < la; i++)
for (int j = 1; j < lb; j++)
dp[i][j] = (dp[i - 1][j] && (a[i] == c[i + j])) || (dp[i][j - 1] && (b[j] == c[i + j]));
printf("Data set %d: ", k++);
if (dp[la - 1][lb - 1]==1)printf("yes\n");
else printf("no\n");
}
}
dfs解法 :记忆化搜索即可,避免重复
#include<cstdio>
#include<cstring>
using namespace std;
char a[300], b[300], c[500];
int vis[300][300];
int f,la, lb, lc;
void dfs(int x, int y, int z)
{
if (z == lc){
f = 1; return;
}
if (vis[x][y])return;
vis[x][y] = 1;
if (a[x] == c[z] && x < la)
dfs(x + 1, y, z + 1);
if (b[y] == c[z] && y < lb)
dfs(x, y + 1, z + 1);
}
int main()
{
int n, k = 1;
scanf("%d", &n);
while (n--)
{
scanf("%s%s%s", a, b, c);
printf("Data set %d: ", k++);
la = strlen(a), lb = strlen(b), lc = strlen(c);
memset(vis, 0, sizeof(vis));
f = 0;
dfs(0, 0, 0);
if (f)printf("yes\n");
else printf("no\n");
}
}