您的位置:首页 > 其它

poj 2192 Zipper

2017-08-11 22:07 351 查看
题目链接:http://poj.org/problem?id=2192

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 18658Accepted: 6651
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

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: