hdu 3779
简单记忆化搜索

#include<stdio.h> #include<string.h> int as[1005],bs[1005],cs[2005],ok,n,m; char hash[1005][1005]; void dfs(int x,int y,int z) { if(hash[x][y]||ok)return; if(z==n+m)ok = 1; hash[x][y] = 1; if(as[x]==cs[z]&&x<n)dfs(x+1,y,z+1); if(ok)return; if(bs[y]==cs[z]&&y<m)dfs(x,y+1,z+1); } int main() { while(scanf("%d%d",&n,&m)==2) { if(!(n+m))break; for(int i = 0; i < n; ++ i) scanf("%d",as+i); for(int i = 0; i < m; ++ i) scanf("%d",bs+i); for(int i = 0; i < n + m; ++ i) scanf("%d",cs+i); memset(hash,0,sizeof(hash)); ok = 0; dfs(0,0,0); if(ok)puts("possible"); else puts("not possible"); }return 0; }