Zipper

题意: 给出三个字符串,判断第三个字符串是否能由第一二个字符串中的字符构成,其中第一二个字符串中的字符次序不能乱。

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1501

思路:典型的搜索问题,用dfs,对一二个字符串进行搜索,定义一个标志数组,确定第一二个字符串序列的访问情况。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
using namespace std;
int vis[205][205];
string str1,str2,str3;
int len1,len2,len3,flag;

void dfs(int a,int b, int c)
{
    if(c == len3)  //循环终止条件。
    {
        flag = 1;
        return ;
    }
    if(vis[a][b])  //判定第一二个序列的访问情况
        return ;
    vis[a][b] = 1;
    if(str1[a] == str3[c])
        dfs(a+1,b,c+1);
    if(str2[b] == str3[c])
        dfs(a,b+1,c+1);
}

int main()
{
    int t,k=0;
    cin >> t;
    while(t--)
    {
        cin >> str1 >> str2 >> str3;
        cout << "Data set " << ++k << ": ";
        len1 = str1.size();
        len2 = str2.size();
        len3 = str3.size();
        memset(vis, 0,sizeof vis);
        if(len1 + len2 != len3)
        {
            cout << "no"<< endl;
            continue;
        }
        else
        {
            flag = 0;
            dfs(0,0,0);
        }
        if(flag)
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值