Zipper

来源:互联网 发布:冰心和林徽因知乎 编辑:程序博客网 时间:2024/05/14 13:40

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

链接: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;}
0 0