POJ 2192 Zipper
2013-06-30 17:20
417 查看
[b]Zipper[/b]
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".
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.
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.
Sample Input
Sample Output
方法二:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14193 | Accepted: 4992 |
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".
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.
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.
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 题目大意:输入三个字符串A,B,C,问C能否有A,B中的字符组成,且字符的顺序必须是原来在字符A,B中的顺序。 解题方法:用动态规划,我用了两种方法,其实思想都是一样的。 方法一:
#include <stdio.h> #include <string> #include <cstring> #include <iostream> using namespace std; int dp[205][205]; int main() { int N; scanf("%d", &N); char str1[205], str2[205], str3[405]; for (int i = 1; i <= N; i++) { cin>>str1+1>>str2+1>>str3+1; int nLen1 = strlen(str1 + 1); int nLen2 = strlen(str2 + 1); int nLen3 = strlen(str3 + 1); memset(dp, 0, sizeof(dp)); int Max = -1; for (int j = 0; j <= nLen1; j++) { for (int k = 0; k <= nLen2; k++) { if (j > 0 && str1[j] == str3[j + k]) { dp[j][k] = max(dp[j][k], dp[j - 1][k] + 1); Max = max(Max, dp[j][k]); } if (k > 0 && str2[k] == str3[j + k]) { dp[j][k] = max(dp[j][k], dp[j][k - 1] + 1); Max = max(Max, dp[j][k]); } } } if (nLen3 == Max) { printf("Data set %d: yes\n", i); } else { printf("Data set %d: no\n", i); } } return 0; }
方法二:
#include <stdio.h> #include <string> #include <cstring> #include <iostream> using namespace std; int dp[205][205]; int main() { int N; scanf("%d", &N); char str1[205], str2[205], str3[405]; for (int i = 1; i <= N; i++) { cin>>str1+1>>str2+1>>str3+1; int nLen1 = strlen(str1 + 1); int nLen2 = strlen(str2 + 1); int nLen3 = strlen(str3 + 1); memset(dp, 0, sizeof(dp)); int Max = -1; dp[0][0] = 1; for (int j = 0; j <= nLen1; j++) { for (int k = 0; k <= nLen2; k++) { if (j > 0 && str1[j] == str3[j + k] && dp[j - 1][k]) { dp[j][k] = 1; } if (k > 0 && str2[k] == str3[j + k] && dp[j][k - 1]) { dp[j][k] = 1; } } } if (dp[nLen1][nLen2]) { printf("Data set %d: yes\n", i); } else { printf("Data set %d: no\n", i); } } return 0; }
相关文章推荐
- POJ - 2192 - Zipper (简单DP)
- POJ 2192 / ZOJ 2401 : Zipper - DP
- POJ 2192 Zipper
- poj 2192 Zipper
- poj 2192 Zipper
- POJ 2192 判断两个字符串是否能按顺序组合成第三个字符串 ZIPPER
- POJ 2192 && HDU 1501 Zipper (记忆化搜索)
- Zipper poj 2192
- POJ 2192 Zipper (dp)
- poj_2192Zipper
- POJ 2192 Zipper [dfs+剪枝 || dp]
- [简单DP] POJ 2192 Zipper
- POJ 2192 判断两个字符串是否能按顺序组合成第三个字符串 ZIPPER
- poj 2192 zipper
- POJ 2192 Zipper
- 【算法刷题】poj 2192 Zipper
- poj_2192Zipper
- POJ 2192 Zipper(判断第3个字符串能否有前两个字符串组成) 水dp
- poj 2192 Zipper
- 【动态规划】POJ - 2192 Zipper