Do not know why has been overtime, and finally added a pruning on it: The last letter of the synthetic word should be at least the same as the last letter of one of the previous two words
#include "Cstdio" #include "Cmath" #include "CString" #include "iostream" #define MAXN 205using namespace Std;char arr[4][ Maxn];int len[4];int vis[maxn][maxn];int p=1,ok;void dfs (int x,int y,int z) { if (OK) return; if (Z>=len[3]) {Ok=1;return;} if (Vis[x][y]) return; if (Arr[1][x]==arr[3][z]) { vis[x][y]=1; DFS (x+1,y,z+1); vis[x][y]=0; } if (Arr[2][y]==arr[3][z]) { vis[x][y]=1; DFS (x,y+1,z+1); vis[x][y]=0;} } int main () { int n; scanf ("%d", &n); while (n--) { scanf ("%s%s%s", arr[1],arr[2],arr[3]); for (int i=1;i<=3;i++) Len[i]=strlen (Arr[i]); ok=0; memset (vis,0,sizeof (Vis)); if (arr[1][len[1]-1]==arr[3][len[3]-1]| | ARR[2][LEN[2]-1]==ARR[3][LEN[3]-1]) DFS (0,0,0); if (OK) printf ("Data set%d:yes\n", p++); else printf ("Data set%d:no\n", p++); } return 0;}
HDU 1501: (DFS)