传送门

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 (最长公共上升序列)的更多相关文章

  1. HDU 5904 - LCIS (BestCoder Round #87)

    HDU 5904 - LCIS [ DP ]    BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式 ...

  2. hdu 1243 反恐训练营 最长公共字序列

    此题的题意很明确,就是求最长公共子序列: #include<iostream> #include<algorithm> #include<cstdio> #incl ...

  3. LCIS最长公共上升子序列

    最长公共上升子序列LCIS,如字面意思,就是在对于两个数列A和B的最长的单调递增的公共子序列. 这道题目是LCS和LIS的综合. 在LIS中,我们通过两重循环枚举当序列以当前位置为结尾时,A序列中当前 ...

  4. LCIS 最长公共上升子序列问题DP算法及优化

    一. 知识简介 学习 LCIS 的预备知识: 动态规划基本思想, LCS, LIS 经典问题:给出有 n 个元素的数组 a[] , m 个元素的数组 b[] ,求出它们的最长上升公共子序列的长度. 例 ...

  5. HDU 4681 String 最长公共子序列

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4681 题意: 给你a,b,c三个串,构造一个d串使得d是a,b的子序列,并且c是d的连续子串.求d最大 ...

  6. [CodeForces10D]LCIS(最长公共上升子序列) - DP

    Description 给定两个数列,求最长公共上升子序列,并输出其中一种方案. Input&Output Input 第一行一个整数n(0<n<=500),数列a的长度. 第二行 ...

  7. 最长公共字序列.cpp

    <span style="color:#993399;">/* By yuan 2014/6/21 At nwpu.xf 1041.最长公共子序列 时限:1000ms ...

  8. CF10D LCIS 最长公共上升子序列

    题目描述 This problem differs from one which was on the online contest. The sequence a1,a2,...,an a_{1}, ...

  9. HDU 1159.Common Subsequence-最长公共子序列(LCS)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. Java:注解(元数据)

    初识Java注解 所谓的元数据是指用来描述数据的数据,可能刚听到元数据的时候你会有点陌生,其实任何一个使用过struts或者hibernate的开发人员都在不知不觉中使用元数据,更通俗一点来说元数据是 ...

  2. DateTime.Now.ToString() 用法

    //2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...

  3. [BZOJ1564][NOI2009]二叉查找树(区间DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1564 分析: 首先因为每个点的数据值不变,所以无论树的形态如何变,树的中序遍历肯定不变 ...

  4. 2016 5.03开始记录我的it学习。

    好多谢立成师兄给我这个网址,我发现博客园不仅仅可以随笔记载很多东西,还是一个资源丰富的网站,接下来的四年我会用心去记录这些学习的点滴.

  5. JAVA多线程(二)

    Synchronized的使用: (一)synchronized:  Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 当某个方法或者代 ...

  6. IIS——发布网站

    当我们要上线一个网站时,不要把整个项目原封不动的发布到服务器,而要经过右键发布后,然后再将发布的文件路径配置到IIS~ 详细信息见链接:http://www.52ij.com/jishu/aspx/1 ...

  7. 虚拟机VirtualBox 5.1.0|VBOX

    Oracle VM VirtualBox是一款免费.开源的虚拟机软件,现属于Oracle旗下产品.可以安装Windows.Linux.IBM OS/2.Solaris.BSD等操作系统,具有远端桌面协 ...

  8. Spring学习进阶(四) Spring JDBC

    Spring JDBC是Spring所提供的持久层技术.主要目的是降低使用JDBC API的门槛,以一种更直接,更简洁的方式使用JDBC API.在Spring JDBC里用户仅需要做哪些比不可少的事 ...

  9. Rest API 开发 学习笔记(转)

    Rest API 开发 学习笔记 概述 REST 从资源的角度来观察整个网络,分布在各处的资源由URI确定,而客户端的应用通过URI来获取资源的表示方式.获得这些表徵致使这些应用程序转变了其状态.随着 ...

  10. Tomcat遇到的问题

    1. java.lang.OutOfMemoryError: PermGen space 启动tomcat服务时,报这个错,查了下是,内存泄露 PermGen space的全称是Permanent G ...