http://acm.hdu.edu.cn/showproblem.php?pid=3779
记忆化搜索...没其他说的了
#include<cstdio>
#include<cstring>
#define N 1002
int hash[N][N];
int a[N],b[N],c[2*N];
int n,m;
int dfs(int a_pos,int b_pos,int c_pos)
{
if(a_pos>n&&b_pos>m)
return 1;
if(hash[a_pos][b_pos]!=-1)
return hash[a_pos][b_pos];
if(a_pos<=n&&a[a_pos]==c[c_pos])
{
if(dfs(a_pos+1,b_pos,c_pos+1))
{
hash[a_pos+1][b_pos]=1;
return 1;
}
hash[a_pos+1][b_pos]=0;
}
if(b_pos<=m&&b[b_pos]==c[c_pos])
{
if(dfs(a_pos,b_pos+1,c_pos+1))
{
hash[a_pos][b_pos+1]=1;
return 1;
}
hash[a_pos][b_pos+1]=0;
}
return 0;
}
int main()
{
while(scanf("%d%d",&n,&m)==2&&(n||m))
{
memset(hash,-1,sizeof(hash));
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=m;i++)
scanf("%d",&b[i]);
for(int i=1;i<=n+m;i++)
scanf("%d",&c[i]);
if(dfs(1,1,1))
printf("possible\n");
else
printf("not possible\n");
}
return 0;
}