本题给出的数据,没有太大为难,三个数组的大小都是有限制的
但是要注意记忆化搜索,不然会超时。
#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;
}