标签:
记录路径开二维数组就够了,开三维会超内存,开始直接把字符串传递进去直接删除字符,但是这样记录不了路径,会重复搜索导致超时,还是转化为下标处理吧
#include<iostream> #include<cstring> using namespace std; string a,b,c; int flag; int visit[201][201]; void dfs(int x,int y,int z) { visit[x][y]=1; if(z==c.size()) {flag=1;return;} if(flag) {return;} if(a[x]==b[y]&&b[y]==c[z]) { if(!visit[x+1][y]) { dfs(x+1,y,z+1); visit[x+1][y]=1; } if(!visit[x][y+1]) { dfs(x,y+1,z+1); visit[x][y+1]=1; } } else if(a[x]==c[z]&&!visit[x+1][y]) dfs(x+1,y,z+1),visit[x+1][y]=1; else if(b[y]==c[z]&&!visit[x][y+1]) dfs(x,y+1,z+1),visit[x][y+1]=1; } int main() { int s,t; cin>>t; for(s=1;s<=t;s++) { cin>>a>>b>>c; if(c.size()!=(a.size()+b.size())) { cout<<"Data set "<<s<<": no"<<endl; continue; } flag=0; memset(visit,0,sizeof(visit)); dfs(0,0,0); cout<<"Data set "; cout<<s<<": "; if(flag) cout<<"yes"; else cout<<"no"; cout<<endl; } return 0; }
标签:
原文地址:http://blog.csdn.net/zafkiel_nightmare/article/details/45000361