http://acm.hdu.edu.cn/showproblem.php?pid=1501
分析:记忆化搜索
注释掉后反而更快了
#include <iostream>
#include <cstdio>
//#include <queue>
#include <cstring>
#include <string>
using namespace std;
const int NM=205;
char s1[NM],s2[NM],str[NM<<1];
int vis[NM][NM],len1,len2,len;
bool flag;
void DFS(int t1,int t2,int t)
{
if(flag) return;
if(t==len){
flag=true;return;
}
if(vis[t1][t2]) return;
vis[t1][t2]=1;
/*if(str[t]!=s1[t1] && str[t]!=s2[t2]){
return ;
}
if(t1==len1){
if(strcmp(&s2[t2],&str[t])==0) flag=true;
return;
}
if(t2==len2){
if(strcmp(&s1[t1],&str[t])==0) flag=true;
return;
}*/
if(str[t]==s1[t1]) DFS(t1+1,t2,t+1);
if(str[t]==s2[t2]) DFS(t1,t2+1,t+1);
}
int main()
{
int T,i;
scanf("%d",&T);
i=1;
while(T--){
scanf("%s%s%s",s1,s2,str);
len1=strlen(s1);
len2=strlen(s2);
len=strlen(str);
flag=false;
memset(vis,0,sizeof(vis));
DFS(0,0,0);
if(flag) printf("Data set %d: yes\n",i++);
else printf("Data set %d: no\n",i++);
}
return 0;
}