HDU 1501 Zipper (DFS+剪枝做法)

本文介绍了一个经典的字符串组合问题,即如何判断第三个字符串是否能够通过前两个字符串的有序组合形成。文章给出了具体的例子,并提供了一种使用深度优先搜索(DFS)的方法来解决该问题的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值