水题 最长公共子序列
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> using namespace std; int dp[1010][1010],a[1010],b[1010]; int n,m; int LCS(int x, int y) { if(x==-1 || y==-1) return 0; if(dp[x][y]!=-1) return dp[x][y]; if(a[x]==b[y]) return dp[x][y]=LCS(x-1, y-1)+1; else return dp[x][y]=max(LCS(x-1,y),LCS(x,y-1)); } int main() { int t; scanf("%d",&t); while(t--) { n=0; int x; int mx=-1; while(scanf("%d",&x)==1 && x) { a[n++]=x; } m=0; while(scanf("%d",&x)==1 && x) { b[m++]=x; while(scanf("%d",&x)==1 && x) { b[m++]=x; } memset(dp, -1, sizeof(dp)); x=LCS(n-1,m-1); //printf("%d\n",x); m=0; if(x>mx) mx=x; } printf("%d\n",mx); } return 0; }