Greatest Common Increasing Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9961    Accepted Submission(s): 3206


 

Problem Description

This is a problem from ZOJ make it easyer,you just need output the length of the subsequence.

 

Input

Each sequence is described with M - its length (1 <= M <= 500) and M integer numbers Ai (-2^31 <= Ai < 2^31) - the sequence itself.

 

Output

output print L - the length of the greatest common increasing subsequence of both sequences.

 

Sample Input

1 5 1 4 2 5 -12 4 -12 1 2 4

 

Sample Output

2

 

Source

​​ACM暑期集训队练习赛(二) ​​

 

Recommend

lcy   |   We have carefully selected several similar problems for you:  ​​1422​​ ​​1421​​ ​​1400​​ ​​1418​​ ​​1424​​ 

 

最长上升公共子序列模板题(​​具体解析点这里​​)

 

 

代码实现:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

const int MAXN = 1001;

int a[MAXN], b[MAXN];
int f[MAXN][MAXN];
int n, m,ans;

void init()
{
memset(f, 0, sizeof(f));
}
void dp()
{
init();
int i, j, k;
for(i = 1; i <= n; i++)
{
for(j = 1; j <= m; j++)
{
f[i][j] = f[i-1][j]; // if(a[i] != b[j])
if(a[i] == b[j])
{
int MAX = 0;
for(k = 1; k <= j-1; k++) if(b[j] > b[k]) //枚举最大的f[i-1][k]
{
MAX = max(MAX, f[i-1][k]);
}
f[i][j] = MAX+1;
}
}
}
ans = 0;
for(int i = 1; i <= m; i++) ans = max(ans, f[n][i]);

}
int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d",&n) ;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(int i=1;i<=m;i++)
scanf("%d",&b[i]);
dp();

printf("%d\n", ans);
if(T)
printf("\n");
}
}