poj 2192 Zipper
2017-08-11 22:07
351 查看
题目链接:http://poj.org/problem?id=2192
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
Source
Pacific Northwest 2004
题目大意:
给出两串,从两个串取出字符重新组合,看能否组成第三个串。要求:从第一个串取出的字符在第三个串中的顺序不变,第二个串取出的字符在第三个串中的顺序也不变。
算法分析:
此题深搜和DP都能解决:
深搜的话需要几个强有力剪枝条件
1、 第三个串最后一个字符要么是串1的最后一个字符,要么是串2的最后一个字符
2、 按照串1的顺序对串3进行搜索,若不匹配则该字符必是串2的下一个字符。
参考来源:http://www.cnblogs.com/yu-chao/archive/2012/02/26/2369052.html
View Code
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18658 | Accepted: 6651 |
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
Source
Pacific Northwest 2004
题目大意:
给出两串,从两个串取出字符重新组合,看能否组成第三个串。要求:从第一个串取出的字符在第三个串中的顺序不变,第二个串取出的字符在第三个串中的顺序也不变。
算法分析:
此题深搜和DP都能解决:
深搜的话需要几个强有力剪枝条件
1、 第三个串最后一个字符要么是串1的最后一个字符,要么是串2的最后一个字符
2、 按照串1的顺序对串3进行搜索,若不匹配则该字符必是串2的下一个字符。
参考来源:http://www.cnblogs.com/yu-chao/archive/2012/02/26/2369052.html
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 char first[201],second[201],third[401]; 6 int res[201][201]; 7 int init(int n,int m) 8 { 9 int i; 10 for(i=1;i<=m;i++) 11 if(second[i]==third[i]) res[0][i]=1; 12 else break; 13 for(i=1;i<=n;i++) 14 if(first[i]==third[i]) res[i][0]=1; 15 else break; 16 return 0; 17 } 18 int dp(int n,int m) 19 { 20 int i,j; 21 for(i=1;i<=n;i++) 22 for(j=1;j<=m;j++) 23 { 24 if(third[i+j]==first[i] && res[i-1][j]) res[i][j]=1; 25 if(third[i+j]==second[j] && res[i][j-1]) res[i][j]=1; 26 } 27 if(res [m]) return 1; 28 return 0; 29 } 30 int main() 31 { 32 int n,len1,len2,count=0;; 33 scanf("%d",&n); 34 while(n--) 35 { 36 count++; 37 scanf("%s %s %s",first+1,second+1,third+1); 38 len1=strlen(first+1); 39 len2=strlen(second+1); 40 memset(res,0,sizeof(res)); 41 init(len1,len2); 42 43 if(dp(len1,len2)) 44 printf("Data set %d: yes\n",count); 45 else 46 printf("Data set %d: no\n",count); 47 } 48 return 0; 49 }
View Code
相关文章推荐
- poj 2192 Zipper (DP)
- poj 2192 Zipper
- poj_2192Zipper
- HDU 1501 & POJ 2192 Zipper(dp记忆化搜索)
- 【动态规划】POJ - 2192 Zipper
- poj 2192 Zipper
- poj 2192 Zipper
- 【POJ 2192 Zipper】+ 贪心 + 字符匹配
- TOJ 3445 POJ 2192 Zipper
- POJ---2192-Zipper
- poj 2192 Zipper
- HDU 1501 & POJ 2192 Zipper(dp记忆化搜索)
- POJ-2192 Zipper-顺序合成串匹配
- POJ 2192 Zipper
- (DP)POJ2192—Zipper
- POJ_2192_Zipper
- POJ 2192 Zipper [dfs+剪枝 || dp]
- [简单DP] POJ 2192 Zipper
- POJ 2192 Zipper (简单DP)
- POJ 2192 Zipper