http://acm.hdu.edu.cn/showproblem.php?pid=1501
简单搜索。只需记录当前状态是否到过就行了。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <algorithm>
#include <map>
#include <vector>
#include <stack>
using namespace std;
#define M 1000005
#define ll long long
#define int64 __int64
char ch1[205] , ch2[205] , str[405];
int vis[205][205];
bool Dfs(int index , int c1 , int c2)
{
if (str[index] == '\0')return true;
if (vis[c1][c2])return false;
if (ch1[c1] == str[index] && Dfs(index+1 , c1+1 , c2))return true;
if (ch2[c2] == str[index] && Dfs(index+1 , c1 , c2+1))return true;
vis[c1][c2] = 1;
return false;
}
int main()
{
int t , tcase = 1;
scanf("%d",&t);
while (t--)
{
scanf("%s%s%s",ch1,ch2,str);
memset(vis , 0 , sizeof vis);
if (Dfs(0,0,0))
printf("Data set %d: yes\n",tcase++);
else
printf("Data set %d: no\n",tcase++);
}
return 0;
}