Zipper(DFS)

在这里插入图片描述
本题给出的数据,没有太大为难,三个数组的大小都是有限制的
但是要注意记忆化搜索,不然会超时。

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
queue < char > q1;
int a1[205][205];
char a[205],b[205],c[405];
int x,y,z;
bool ans;
int sum;
void DFS(int x,int y,int z)
{
    if(a[x]=='\0'&&b[y]=='\0'&&c[z]=='\0')   //剪枝
    {
        ans=1;
        return;
    }
    if(a[x]!=c[z]&&b[y]!=c[z])  //剪枝
        return;
    if(ans==1)     //剪枝
        return;
    if(a1[x][y]==1)     //剪枝
        return;
        a1[x][y]=1;    /记忆化
    if(a[x]==c[z])
        DFS(x+1,y,z+1);
    if(b[y]==c[z])
        DFS(x,y+1,z+1);
}
int main()
{
    int n;
    scanf("%d",&n);
    sum=1;
    while(n--)
    {
        memset(a1,0,sizeof(a1));
        scanf("%s %s %s",a,b,c);
        ans=0;
        DFS(0,0,0);
        printf("Data set %d: ",sum++);
        if(ans==0)
            printf("no\n");
        else
            printf("yes\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值