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 ...
随机推荐
- docker中清理冗余的image,container
1) 首先进入超级用户模式 [root@docker ~]# sudo su2) 删除container ( container运行时是不能删除的 )首先停止container [root@docke ...
- 头像上传功能实现,PC端的需要做兼容
暂时实现的效果: http://sandbox.runjs.cn/show/v2vkds3j <form action=""> <img id="vie ...
- Java NIO 概述
Channel 和 Buffer 标准的Java IO编程接口是面向字节流和字符流的 而 NIO 是面向通道和缓冲区的 数据总是从通道中读到Buffer中,或者从Buffer写入通道中 NIO可以理解 ...
- 001医疗项目-项目框架的搭建(四个maven工程)
这个项目资料来源于传智播客.用的是ssm框架, 我们首先建立一个working sets里面存放,我们的maven工程. 如下:
- BPM到底能做什么?K2为你解读
和平镇,镇如其名,几百年来一直很和平,夜不闭户路不拾遗.可是这一年来,镇上金光寺的和尚却开始不断离奇死亡…… 衙门里新调来的李捕头正好负责这个案子,经过了几个月的不眠不休,现场侦查和缜密推理之后,一切 ...
- 思科产品选型pdf
以前做工程时候想起了设备选型时候用过的一份文档. 有个小伙伴今天问起思科设备选型,恰好google到了这份文档 https://www.cisco.com/web/CN/products/pdf/04 ...
- 数据库SQL优化大总结之 百万级数据库优化方案(转)
1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- Java系列,《Java核心技术 卷1》,chapter 13,集合
13.1.2 Java类库中的集合接口和迭代器接口 删除元素,对于next和remove的调用是互相依赖的,如果调用remove之前没有调用next,则会跑出IllegalStateExcep ...
- 20135316王剑桥 linux第五周课实验笔记
4.1.1程序员的可见的状态 ———— Y86的每条指令都会读取或修改处理器状态的某些部分,称为程序员可见状态.如图1所示. 1.程序寄存器(Program registers): %eax, %ec ...
- 20145208 实验二 Java面向对象程序设计
20145208 实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步 ...