Greatest Common Increasing Subsequence
求兩個序列的最長公共遞增子序列
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=555;
int a[maxn],b[maxn],len[maxn];
int n,m;//a,b數組的長度
int LCIS()
{
int locate;
memset(len,0,sizeof(len));
for(int i=1;i<=n;++i)
{
locate=1;
for(int j=1;j<=m;++j)
{
if(b[j]<a[i]&&len[j]>len[locate])
locate=j;
if(a[i]==b[j])
len[j]=len[locate]+1;
}
}
int max=-1;
for(int i=1;i<=m;i++)
max=max>len[i]?max:len[i];
return max;
}
int main()
{
int t;
scanf("%d",&t);
int i,j;
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;++i)
scanf("%d",&a[i]);
scanf("%d",&m);
for(i=1;i<=m;++i)
scanf("%d",&b[i]);
int ans=LCIS();
printf("%d\n",ans);
if(t)
printf("\n");
}
return 0;
}