DP poj 2192

Zipper
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13247   Accepted: 4655

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



题意:

判断第一个串与第二个串字符顺序不变的组合,是否能得到第三个串。


思路:

DP题。

a[i][j]的意思为,第一个串的1~i加上第二个串的1~j是否能组成第三个串的1~i+j.

a[i][j]=a[i-1][j]&&cmp(fir[i],thr[i+j])||a[i][j-1]&&cmp(se[j],thr[i+j])

首先应初始化a[0][0]=true.


代码实现:
View Code
 1 #include<iostream>
 2 #include<string.h>
 3 #include<stdio.h>
 4 using namespace std;
 5 char a[1010];
 6 char fir[1010];
 7 char se[1010];
 8 int sum[1010][1010];
 9 int main(){
10     int n;
11     cin>>n;
12     int times=0;
13     while(n--){
14         times++;
15         memset(sum,-1,sizeof(sum));
16         scanf("%s",fir+1);
17         scanf("%s",se+1);
18         scanf("%s",a+1);
19         int len1=strlen(fir+1);
20         int len2=strlen(se+1);
21         sum[0][0]=1;
22         if(a[1]==fir[1]){
23                sum[1][0]=1;
24                for(int i=2;i<=len1;i++){
25                        if(fir[i]==a[i]&&sum[i-1][0]==1){
26                                sum[i][0]=1;
27                                }
28                        else{
29                                sum[i][0]=-1;
30                                }
31                                }
32                                }
33         if(a[1]==se[1]){
34                sum[0][1]=1;
35                for(int i=2;i<=len2;i++){
36                        if(se[i]==a[i]&&sum[0][i-1]==1){
37                                sum[0][i]=1;
38                                }
39                        else{
40                                sum[0][i]=-1;
41                                }
42                                }
43                                }
44         for(int j=1;j<=len1;j++){
45                 for(int k=1;k<=len2;k++){
46                         if((sum[j][k-1]==1&&a[j+k]==se[k])||(sum[j-1][k]==1&&a[j+k]==fir[j])){
47                                 //    cout<<a[j+k]<<" "<<se[k]<<" "<<fir[j]<<endl;
48                                     sum[j][k]=1;
49                                     }
50                                     }
51                                     }
52         for(int i=0;i<=len1;i++){
53                 for(int j=0;j<=len2;j++){
54                     //    cout<<i<<" "<<j<<" "<<sum[i][j]<<endl;
55                         }
56                         }
57         cout<<"Data set "<<times<<": ";
58         if(sum[len1][len2]==1)
59               cout<<"yes"<<endl;
60         else
61               cout<<"no"<<endl;
62               }
63         return 0;
64         }

 

posted on 2012-09-05 21:02  yumao  阅读(177)  评论(0编辑  收藏  举报

导航