暑假集训第三周第二阶段搜索 H - Zipper


H - Zipper
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  HDU 1501

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
又是搜索,题意很好理解,关键是运用熟练,好好理解其中的要点

还有就是边界问题,注意处理好!只有在边界之内才能去做其他处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<stdio.h>
#include<string.h>
int f,l1,l2,l3,dp[205][205];
char s1[205],s2[205],s3[405];
void dfs(int a,int b,int c)
{
    if(f)
    return ;
    if(c==l3)
    {
        f=1;
        return ;
    }
    if(dp[a][b])
        return ;
    dp[a][b]=1;
    if(s1[a]==s3[c])
        dfs(a+1,b,c+1);
    if(s2[b]==s3[c])
        dfs(a,b+1,c+1);
}
int main()
{
    int i,t;
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {
        scanf("%s%s%s",s1,s2,s3);
        printf("Data set %d: ",i);
        l1=strlen(s1);
        l2=strlen(s2);
        l3=strlen(s3);
        if(l1+l2!=l3)
        {
            printf("no\n");
            continue;
        }
        memset(dp,0,sizeof(dp));
        f=0;
        dfs(0,0,0);
        if(f)
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}

你可能感兴趣的:(暑假集训第三周第二阶段搜索 H - Zipper)