hdu 1501 Zipper

Zipper

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5593    Accepted Submission(s): 2039


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".
 

 

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
 

 

Recommend
linle   |   We have carefully selected several similar problems for you:  1502 1227 1158 1080 1078 
 
 1 //31MS    404K    743 B    C++
 2 /*
 3 
 4     题意:
 5         给出三个字符串,问前两个是否可顺序排成第三个
 6         
 7     记忆化搜索或DP:
 8         我是使用记忆化搜索,不然会超时 
 9 
10 */
11 #include<stdio.h>
12 #include<string.h>
13 char s1[205],s2[205],s3[405];
14 int dp[205][205]; 
15 int n,m;
16 int dfs(int la,int lb,int l)
17 {
18     if(dp[la][lb]!=-1) return dp[la][lb];
19     if(la==n && lb==m) return dp[la][lb]=1;
20     if(la>n || lb>m) return dp[la][lb]=0;
21     int flag=0;
22     if(s3[l]==s1[la]) flag+=dfs(la+1,lb,l+1);
23     if(s3[l]==s2[lb]) flag+=dfs(la,lb+1,l+1);
24     return dp[la][lb]=flag;
25 } 
26 int main(void)
27 {
28     int t;
29     int k=1;
30     scanf("%d",&t);
31     while(t--)
32     {
33         memset(dp,-1,sizeof(dp));
34         scanf("%s %s %s",&s1,&s2,&s3);
35         n=strlen(s1);
36         m=strlen(s2);
37         printf("Data set %d: ",k++);
38         if(dfs(0,0,0))puts("yes");
39         else puts("no");
40     }
41     return 0;
42 }

 

 1 //109MS    872K    734 B    C++
 2 /*
 3 
 4     DP做法:
 5         dp[i][j]表示s1前i个字符和s2的前j个字符是否可组成s3的钱i+j
 6     个字符,1表示可以,0表示不可以
 7     
 8     状态转移:
 9         if  s1[i-1]==s3[i+j-1] && dp[i-1][j] then
10             dp[i][j]=1;
11         if s2[j-1]==s3[i+j-1] && dp[i][j-1] then
12             dp[i][j]=1;
13          
14 */
15 #include<stdio.h>
16 #include<string.h>
17 char s1[205],s2[205],s3[405];
18 int dp[405][405];
19 int n,m;
20 int main(void) 
21 {
22     int t;
23     int k=1;
24     scanf("%d",&t);
25     while(t--)
26     {
27         scanf("%s %s %s",s1,s2,s3);
28         memset(dp,0,sizeof(dp));
29         n=strlen(s1);
30         m=strlen(s2);
31         dp[0][0]=1;
32         for(int i=0;i<=n;i++){
33             for(int j=0;j<=m;j++){
34                 if(i>0 && s1[i-1]==s3[i+j-1] && dp[i-1][j])
35                     dp[i][j]=1;
36                 if(j>0 && s2[j-1]==s3[i+j-1] && dp[i][j-1])
37                     dp[i][j]=1;
38             }
39         }
40         printf("Data set %d: ",k++);
41         if(dp[n][m]) puts("yes");
42         else puts("no"); 
43     }
44     return 0;
45 }

 

posted @ 2013-11-02 13:40  heaventouch  阅读(163)  评论(0)    收藏  举报