Greatest Common Increasing Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7567 Accepted Submission(s):
2442
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ql(a) memset(a,0,sizeof(a))
#define CIN(a) scanf("%d",&a)
#define COUT(a) printf("%d
",a)
int dp[505][505];
int main()
{
int a[505],b[505];
int i,j,k=0,sumn,maxn,temp,tmp;
int n1,n2,t;
cin>>t;
while(t--){ql(dp);k++;
if(k>1) cout<<endl;
CIN(n1);
for(i=1;i<=n1;++i) CIN(a[i]);
CIN(n2);
for(i=1;i<=n2;++i) CIN(b[i]);
for(i=1;i<=n1;++i)
for(j=1;j<=n2;++j)
{
if(a[i]!=b[j]) dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
else{maxn=0;
dp[i][j]=max(1,dp[i-1][j-1]);
for(int di=1;di<i;++di)
for(int dj=1;dj<j;++dj)
if(a[di]==b[dj]&&a[di]<a[i]) maxn=max(maxn,dp[di][dj]);
dp[i][j]=maxn+1;
}
}
COUT(dp[n1][n2]);
}
return 0;
}