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 (最长公共上升序列)的更多相关文章
- HDU 5904 - LCIS (BestCoder Round #87)
HDU 5904 - LCIS [ DP ] BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式 ...
- hdu 1243 反恐训练营 最长公共字序列
此题的题意很明确,就是求最长公共子序列: #include<iostream> #include<algorithm> #include<cstdio> #incl ...
- LCIS最长公共上升子序列
最长公共上升子序列LCIS,如字面意思,就是在对于两个数列A和B的最长的单调递增的公共子序列. 这道题目是LCS和LIS的综合. 在LIS中,我们通过两重循环枚举当序列以当前位置为结尾时,A序列中当前 ...
- LCIS 最长公共上升子序列问题DP算法及优化
一. 知识简介 学习 LCIS 的预备知识: 动态规划基本思想, LCS, LIS 经典问题:给出有 n 个元素的数组 a[] , m 个元素的数组 b[] ,求出它们的最长上升公共子序列的长度. 例 ...
- HDU 4681 String 最长公共子序列
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4681 题意: 给你a,b,c三个串,构造一个d串使得d是a,b的子序列,并且c是d的连续子串.求d最大 ...
- [CodeForces10D]LCIS(最长公共上升子序列) - DP
Description 给定两个数列,求最长公共上升子序列,并输出其中一种方案. Input&Output Input 第一行一个整数n(0<n<=500),数列a的长度. 第二行 ...
- 最长公共字序列.cpp
<span style="color:#993399;">/* By yuan 2014/6/21 At nwpu.xf 1041.最长公共子序列 时限:1000ms ...
- CF10D LCIS 最长公共上升子序列
题目描述 This problem differs from one which was on the online contest. The sequence a1,a2,...,an a_{1}, ...
- HDU 1159.Common Subsequence-最长公共子序列(LCS)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- 2015-2016-2 《Java程序设计》 学生博客及Git@OSC 链接
2015-2016-2 <Java程序设计> 学生博客及Git@OSC 链接 博客 1451 20145101王闰开 20145102周正一 20145103冯文华 20145104张家明 ...
- C#发展历程以及C#6.0新特性
一.C#发展历程 下图是自己整理列出了C#每次重要更新的时间及增加的新特性,对于了解C#这些年的发展历程,对C#的认识更加全面,是有帮助的. 二.C#6.0新特性 1.字符串插值 (String In ...
- versionCompare 版本号比较工具
简介 需求非常简单,需要比较软件或app的版本号,判断大小,形如 0.10.2形式的版本号字符串.实现逻辑是按照点(.)分割字符串,然后逐级比较版本大小.不存在的按0处理,空字符串小于非空字符串. 测 ...
- js10秒倒计时鼠标点击次数统计
<html> <head> <meta charset="utf-8"/> <script type="text/javascr ...
- 学习Google Protocol buffer之语法
上一篇结尾的时候问了几个问题,其实主要就是这个protoBuffer协议的语法,弄清楚语法后边才好开展工作嘛,不然大眼而对小眼儿,互相不认识,就没法玩耍了.其实就是学习怎么用google提供的这套 p ...
- Linux中TFTP使用详解
FTP协议简介TFTP是用来下载远程文件的最简单网络协议,它其于UDP协议而实现. linux服务器端tftp-server的配置1.安装tftp服务器需要安装xinetd(守护tftp).tftp和 ...
- 【python】 [基础] 数据类型,字符串和编码
python笔记,写在前面:python区分大小写1.科学计数法,把10用e代替,1.23x10·9就是 1.23e9 或者 0.00012就是1 ...
- 迷你DVD管理器
import java.text.*; import java.util.*; class DVDSet { String[] name=new String[50]; //定义一个DVD数组 boo ...
- 360demo--关于WM_GETMINMAXINFO
在duilib的demo中,看到这么一段: LRESULT OnGetMinMaxInfo(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHa ...
- 1019mysql 复制技术
-- 第一步实现主从复制参照 http://369369.blog.51cto.com/319630/790921/核心点 :开启二进制日子和服务器ID,创建复制账号,配置连接主从服务器,查看各自状态 ...