http://acm.hdu.edu.cn/showproblem.php?pid=1501
记忆化搜索
由于第三个串是由 前两个串按顺序组成的 那么从前两个串的 第一位 和 第三个串的第一位 开始比 相等就比下一个能比到尾就行了
从这些路径中找出可行的 记忆化搜索
代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
using namespace std;
char a[205];
char b[205];
char c[405];
bool flag[205][205];
int lena,lenb,lenc;
int m=1;
int dfs(int x,int y,int z)
{
if(z>lenc)
return 1;
if(flag[x][y])
return 0;
flag[x][y]=1;
if(x>lena+1||y>lenb+1)
return 0;
if(a[x]==c[z]&&dfs(x+1,y,z+1))
return 1;
if(b[y]==c[z]&&dfs(x,y+1,z+1))
return 1;
return 0;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s%s%s",a+1,b+1,c+1);
memset(flag,0,sizeof(flag));
lena=strlen(a+1);
lenb=strlen(b+1);
lenc=strlen(c+1);
if(dfs(1,1,1))
printf("Data set %d: yes\n",m++);
else
printf("Data set %d: no\n",m++);
}
}