1.题目描述:
Zipper
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9863 Accepted Submission(s): 3528
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
Source
Recommend
linle
给你两个字符串s1和s2和新字符串s,问你s是否能够由s2混入s1顺序组成,同时s1和s2混合构成的新串各个字符原本的顺序不打乱。
3.解题思路:
对于第i个字符,要么是来自于s1,要么是来自于s2要么都不来自于他们,这样递归思路就出来了
if (x < len1 && a[x] == c[step])
dfs(x + 1, y, step + 1);
if (y < len2 && b[y] == c[step])
dfs(x, y + 1, step + 1);
else
dfs(x, y, step + 1);
思路有点像dp,貌似dp也能过这道题。
4.AC代码:
#include <string.h>
#include <stdio.h>
#define N 202
using namespace std;
int vis[N][N], flag, len1, len2, len;
char a[N], b[N], c[N * 2];
void dfs(int x, int y, int step)
{
if (flag || vis[x][y])
return;
if (step == len)
{
flag = 1;
return;
}
vis[x][y] = 1;
if (x < len1 && a[x] == c[step])
dfs(x + 1, y, step + 1);
if (y < len2 && b[y] == c[step])
dfs(x, y + 1, step + 1);
else
dfs(x, y, step + 1);
}
int main()
{
int t, kase = 1;
scanf("%d", &t);
while (t--)
{
scanf("%s%s%s", a, b, c);
memset(vis, 0, sizeof(vis));
flag = 0;
len1 = strlen(a);
len2 = strlen(b);
len = strlen(c);
dfs(0, 0, 0);
printf("Data set %d: ", kase++);
if (flag)
printf("yes\n");
else
printf("no\n");
}
return 0;
}