传送门

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. Asp.net WebApi Put模式调用,“HTTP 错误 405.0 - Method Not Allowed”解决方法

    IIS10.0在部署了WebAPI之后,默认是不支持Put模式调用的.需要按照下面方法启用. 步骤一:在IIS管理界面要支持Put模式的IIS站点,选择 "功能视图". 步骤二:选 ...

  2. CAP原理的证明

    CAP概述 C: Consistency 一致性 A: Availability 可用性 P:Partition Tolerance分区容错性 CAP理论的核心是:一个分布式系统不可能同时很好的满足一 ...

  3. Java 的世界,我不懂:奇葩的 json 序列化

    先上张图,代表我心中的十万头草泥马: 写这么长的代码,头回见数组和单个实体共用同一个 json 节点的! 恐怕只有 java 社区的大牛B 才能做出这等事.. 由 Apache 发布: http:// ...

  4. python动态网页爬取——四六级成绩批量爬取

    需求: 四六级成绩查询网站我所知道的有两个:学信网(http://www.chsi.com.cn/cet/)和99宿舍(http://cet.99sushe.com/),这两个网站采用的都是动态网页. ...

  5. 深入学习JavaScript(二)

    函数表达式和函数声明 函数声明 function 函数名(参数){函数体} 函数表达式 function 函数名(可选)(参数){函数体} 示例: function foo(){} // 声明,因为它 ...

  6. java代码注释规范

    java代码注释规范   代码注释是架起程序设计者与程序阅读者之间的通信桥梁,最大限度的提高团队开发合作效率.也是程序代码可维护性的重要环节之一.所以我们不是为写注释而写注释.下面说一下我们在诉求网二 ...

  7. ajax 跨域提交数据

    $.ajax({ url:"http://my.demo.com/jsonp/server.php",//不同域的文件; cache: false, //是否使用缓存; error ...

  8. 【前端积累】createElement createTextNode

    <!DOCTYPE html> <html><!--树根--> <head> <meta charset="utf-8"> ...

  9. 【摘录】某表含有N个字段超精简模糊查询方法

    SELECT * FROM  table where CONCAT(a,b,c......) like '%key%' select name from syscolumns where id=obj ...

  10. android 随记 ContentValues

    ContentValues 和HashTable类似都是一种存储的机制 但是两者最大的区别就在于,contenvalues只能存储基本类型的数据,像string,int之类的,不能存储对象这种东西,而 ...