HDU 1501 Zipper (DFS+剪枝做法)

Zipper

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


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
 


DFS,剪枝,深搜判断当前C里面的字符能不能用pos_in_a或者pos_in_b的字符凑成,如果能 true,不能false.

代码: View Source On GitHub

#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

char a[300];
char b[300];
char c[700];

int lena,lenb,lenc;
bool dfs(int pos_in_a,int pos_in_b,int pos_in_c)
{
    if(pos_in_a==lena&&pos_in_b==lenb)
    {
        return true;
    }
    if(pos_in_a<lena&&a[pos_in_a]==c[pos_in_c]&&dfs(pos_in_a+1,pos_in_b,pos_in_c+1))
    {
        return true;
    }
    if(pos_in_b<lenb&&b[pos_in_b]==c[pos_in_c]&&dfs(pos_in_a,pos_in_b+1,pos_in_c+1))
    {
        return true;
    }
    return false;
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int tt=1;tt<=t;tt++)
    {
        scanf("%s %s %s",a,b,c);
        printf("Data set %d: ",tt);
        lena=strlen(a);
        lenb=strlen(b);
        lenc=strlen(c);
        if(a[0]!=c[0]&&b[0]!=c[0])
        {
            printf("no\n");
            continue;
        }
        if(a[lena-1]!=c[lenc-1]&&b[lenb-1]!=c[lenc-1])
        {
            printf("no\n");
            continue;
        }
        bool ans=dfs(0,0,0);
        if(ans)
        {
            printf("yes\n");
        }
        else
        {
            printf("no\n");
        }
    }
    return 0;
}

我在GitHub上建立了一个仓库,用于存放已经AC的题目的源代码。如果各位有未收录的题目或者有更好的解法,欢迎fork仓库+PR~ 让我们共同创建一个AC代码集中仓库,造福ACM Beginner ~

仓库地址: OJ-Problems-Source On GitHub


你可能感兴趣的:(HDU 1501 Zipper (DFS+剪枝做法))