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

  1. [LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列

    Give an integer array,find the longest increasing continuous subsequence in this array. An increasin ...

  2. [LintCode] Longest Increasing Continuous subsequence

    http://www.lintcode.com/en/problem/longest-increasing-continuous-subsequence/# Give you an integer a ...

  3. LintCode 397: Longest Increasing Continuous Subsequence

    LintCode 397: Longest Increasing Continuous Subsequence 题目描述 给定一个整数数组(下标从0到n - 1,n表示整个数组的规模),请找出该数组中 ...

  4. Lintcode397 Longest Increasing Continuous Subsequence solution 题解

    [题目描述] Give an integer array,find the longest increasing continuous subsequence in this array. An in ...

  5. LintCode "Longest Increasing Continuous subsequence II" !!

    DFS + Memorized Search (DP) class Solution { int dfs(int i, int j, int row, int col, vector<vecto ...

  6. 300. Longest Increasing Subsequence

    题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For exam ...

  7. 300最长上升子序列 · Longest Increasing Subsequence

    [抄题]: 往上走台阶 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列,这种子序列不一定是连续的或者唯一的. 样例 给出 [5,4,1,2,3],LIS 是 [1,2 ...

  8. <Sicily> Longest Common Subsequence

    一.题目描述 Given a sequence A = < a1, a2, -, am >, let sequence B = < b1, b2, -, bk > be a s ...

  9. Longest common subsequence(LCS)

    问题 说明该问题在生物学中的实际意义 Biological applications often need to compare the DNA of two (or more) different ...

随机推荐

  1. RecyclerView (一) 基础知识

    RecyclerView是什么? RecyclerView是一种新的视图组,目标是为任何基于适配器的视图提供相似的渲染方式.它被作为ListView和GridView控件的继承者,在最新的suppor ...

  2. 承香墨影 Android--Matrix图片变换处理

    承香墨影 Android--Matrix图片变换处理 前言 本篇博客主要讲解一下如何处理对一个Bitmap对象进行处理,包括:缩放.旋转.位移.倾斜等.在最后将以一个简单的Demo来演示图片特效的变换 ...

  3. IEnumerable和IEnumerator 详解 (转)

    原文链接:http://blog.csdn.net/byondocean/article/details/6871881 参考链接:http://www.cnblogs.com/hsapphire/a ...

  4. Android studio disign 问题

    有些低配置的电脑使用android studio 写xml的时候,disign会一直处于rendering,有可能是xml使用的图片过大导致渲染不出来

  5. [资料]PHP Yaf

    QConShanghai2013-惠新宸-微博LAMP性能优化之路 Yaf-一个PHP扩展实现的PHP框架 Baidu Yaf

  6. NET中MSMQ的使用----附例子

    目录 一:MSMQ的一些理论上的知识 二:队列类型(Queue Type) 三:安装消息队列 四:在C#中Messagequeue class 五:MSMQ-发送消息到远程专用队列 六:例子   一. ...

  7. Java系列: 我的第一个spring aop练习

    看<Spring in action>有一段时间了,陆续也都看懂了,但是看懂和自己动手写确实是两回事,今天花了几个小时陆续开始安装spring,开始使用DI,然后使用AOP,在写AOP例子 ...

  8. Fork一个仓库

    Fork 是对一个仓库的克隆.克隆一个仓库允许你自由试验各种改变,而不影响原始的项目. 一般来说,forks 被用于去更改别人的项目(贡献代码给已经开源的项目)或者使用别人的项目作为你自己想法的初始开 ...

  9. iOS开发系列--音频播放(音效和音乐)播放本地的

    音频 在iOS中音频播放从形式上可以分为音效播放和音乐播放.前者主要指的是一些短音频播放,通常作为 点缀音频,对于这类音频不需要进行进度.循环等控制.后者指的是一些较长的音频,通常是主音频,对于这些音 ...

  10. storm基础框架分析

    背景 前期收到的问题: 1.在Topology中我们可以指定spout.bolt的并行度,在提交Topology时Storm如何将spout.bolt自动发布到每个服务器并且控制服务的CPU.磁盘等资 ...