Longest Increasing Common Subsequence (LICS)
最长上升公共子序列(Longest Increasing Common Subsequence,LICS)也是经典DP问题,是LCS与LIS的混合。
Problem
求数列 a[1..n], b[1..m]的LICS的长度, a[], b[]数组的元素均为正整数。
Solution
考虑如何定义DP状态,定义DP状态就是定义所谓的最优子问题(optimal subproblem),而DP状态要能转移,就是所谓最优子问题要具有重叠子结构。
将DP状态定义为
DP[i][j]:a[1..i], b[1..j]的以b[j]结尾的LICS的长度
状态转移方程为
DP[i][j] = DP[i-1][j], a[i] != b[j]
= max{DP[i][k] : k<j, b[k] < b[j]} + 1, a[i] == b[j]
---------------------------------------------------------------------
上面的转移方程,时间复杂度为O(N^3), 空间复杂度为O(N^2),都不能接受,必须优化。
先考虑时间优化,不难发现无法O(1)转移的是a[i]==b[j]的情况,我们考虑在转移的同时维护的这种情况所需要的那个最大值。
我们将转移过程写成两循环
for(i=1; i<=n; i++)
for(j=1; j<=m; j++)
dp[i][j]..
i在外层循环,内层循环时i不变。
我们将第二种情况下的转移方程该成 DP[i][j] = max{DP[i][k] : k<j, b[k]<a[i]} + 1, a[i] == b[j]
优化的方法就显而易见了,在每层内循环内维护 max{ DP[i][k] : k<j, b[k]<a[i] }。
for(i=1; i<=n; i++)
for(j=1, ma=0; j<=m; j++)
if(b[j]==a[i])
dp[i][j]=ma+1;
else{
dp[i][j]=dp[i-1][j];
if(a[i]>b[j])
ma=max(ma, dp[i][j]);
}
这样时间上就优化到O(N^2)
-----------------------------------------------------------------------
再考虑空间优化,根据转移方程不难看出可用滚动数组
for(i=1; i<=n; i++)
for(j=1, ma=0; j<=m; j++)
if(a[i]==b[j])
dp[j]=ma+1;
else if(a[i]>b[j])
ma=max(dp[j], ma);
空间优化到O(N)
---------------------------------------------------------------------------
不难看出DP的一切优化都建立在正确的转移方程之上,所以对于DP问题,写转移方程是最关键的一步。
LICS的O(N^2)的复杂度还是偏高的,不知这是否理论复杂度。
Longest Increasing Common Subsequence (LICS)的更多相关文章
- [LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列
Give an integer array,find the longest increasing continuous subsequence in this array. An increasin ...
- [LintCode] Longest Increasing Continuous subsequence
http://www.lintcode.com/en/problem/longest-increasing-continuous-subsequence/# Give you an integer a ...
- LintCode 397: Longest Increasing Continuous Subsequence
LintCode 397: Longest Increasing Continuous Subsequence 题目描述 给定一个整数数组(下标从0到n - 1,n表示整个数组的规模),请找出该数组中 ...
- Lintcode397 Longest Increasing Continuous Subsequence solution 题解
[题目描述] Give an integer array,find the longest increasing continuous subsequence in this array. An in ...
- LintCode "Longest Increasing Continuous subsequence II" !!
DFS + Memorized Search (DP) class Solution { int dfs(int i, int j, int row, int col, vector<vecto ...
- 300. Longest Increasing Subsequence
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For exam ...
- 300最长上升子序列 · Longest Increasing Subsequence
[抄题]: 往上走台阶 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列,这种子序列不一定是连续的或者唯一的. 样例 给出 [5,4,1,2,3],LIS 是 [1,2 ...
- <Sicily> Longest Common Subsequence
一.题目描述 Given a sequence A = < a1, a2, -, am >, let sequence B = < b1, b2, -, bk > be a s ...
- Longest common subsequence(LCS)
问题 说明该问题在生物学中的实际意义 Biological applications often need to compare the DNA of two (or more) different ...
随机推荐
- <<Effective Java>>之Comparable接口的实现约定
对于BigDecimal类在HashSet和TreeSet中 new BigDecimal("1.00") new BigDecimal("1.0") 在Has ...
- intellij IDEA15 设置背景颜色
File--> Settings 2. Appearance & Behavior --> Appearance 设置边框背景颜色 3. Editor --> Colors ...
- 如何将list转为json?
- 【MySQL】Linux MySQL学习记录
1.查看日志存放路径 show variables like 'general_log_file'; 2.查看日志是否开启 show global variables like 'log_bin%'; ...
- Runtime类及其常用方法
每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接.可以通过 getRuntime 方法获取当前运行时. 常用方法: 1.public static Runt ...
- 2016北京PHP39期 ThinkPHP Discuz Dedecms 微信开发视频教程
2016北京PHP39期 ThinkPHP Discuz Dedecms 微信开发视频教程 所有项目均带有软件,笔记,视频,源码 日期 课程(空内容代表放假) 2015/7/10 星期五 开学典礼 ...
- 自己实现一个高大尚的Android客户端
毕业差不多一年了,一直做得都是很底层的东西,由于面向的客户群不同,主要实现在于功能,效率,没有很炫的界面,客户也并不在意界面有多炫.看到各大市场各种高大尚的app,简直亮瞎了我的眼啊,下决心自己实现一 ...
- 嵌入式Linux应用开发——Linux下的C编程基础
一.markdown简单操作 1.标题 在文字开头加上 “#”,通过“#”数量表示几级标题. 通过在文字下方添加“=”和“-”,他们分别表示一级标题和二级标题. 2.块注释 通过在文字开头添加“> ...
- 报这个错 unrecognized selector sent to instance
1.给一个对象赋空值: 2.调不存在的方法:3.数据类型不对.
- Sublime Text 之运行 ES6 (基于babel)
本文同步自我的个人博客:http://www.52cik.com/2015/10/21/sublime-text-run-es6.html 之前在博客园里写过一篇<Sublime Text 之运 ...