HDU 5904 LCIS (最长公共上升序列)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 5904 LCIS (最长公共上升序列)相关的知识,希望对你有一定的参考价值。

传送门

Description

Alex has two sequences a1,a2,...,an and b1,b2,...,bm. He wants find a longest common subsequence that consists of consecutive values in increasing order.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:The first line contains two integers n and m (1≤n,m≤100000) -- the length of two sequences. The second line contains n integers: a1,a2,...,an (1≤ai≤106). The third line contains n integers: b1,b2,...,bm (1≤bi≤106).There are at most 1000 test cases and the sum of n and m does not exceed 2×106.

Output

For each test case, output the length of longest common subsequence that consists of consecutive values in increasing order.

Sample Input

3 
3 3
1 2 3
3 2 1
10 5
1 23 2 32 4 3 4 5 6 1
1 2 3 4 5
1 1
2
1

Sample Output

1 
5
0

思路

题意:Alex有两个序列a1,a2,...,ana和b1,b2,...,bm. 他想找到它们的最长公共递增子序列, 并且这个子序列的值是连续的(x,x+1,...,y-1,yx,x+1,...,y−1,y). 

 dp[i]表示以i结尾的最长序列,对两个序列进行dp,求出dpa[i]和dpb[i]的公共部分的最大值即可

 
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
int a[maxn],b[maxn],dpa[maxn],dpb[maxn];
int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        memset(dpa, 0, sizeof dpa);
        memset(dpb, 0, sizeof dpb);
        int m,n,res = 0,maxa = 0,maxb = 0,minn;
        scanf("%d%d",&n,&m);
        for (int i = 0;i < n;i++)    scanf("%d",&a[i]),maxa = maxa>a[i]?maxa:a[i];
        for (int i = 0;i < m;i++)    scanf("%d",&b[i]),maxb = maxb>b[i]?maxb:b[i];
        dpa[a[0]] = 1,dpb[b[0]] = 1;
        for (int i = 1;i < n;i++)    dpa[a[i]] = dpa[a[i] - 1] + 1;
        for (int i = 1;i < m;i++)    dpb[b[i]] = dpb[b[i] - 1] + 1;
        minn = maxa<maxb?maxa:maxb;
        for (int i = 1;i <= minn;i++)    res = max(res,min(dpa[i],dpb[i]));
        printf("%d\n",res);
    }
    return 0;
}

  

以上是关于HDU 5904 LCIS (最长公共上升序列)的主要内容,如果未能解决你的问题,请参考以下文章

[HDU5904]LCIS(DP)

HDU 5904 - LCIS (BestCoder Round #87)

HDU 5904 LCIS

最长公共子上升序列(LCIS)

HDU1423 最长公共上升子序列LCIS

HDU 5904 LCIS (DP)