最长上升公共子序列(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. esc设置多站点 域名解析

    你的域名是:a.com IP: 1.1.1.1 一 设置域名解析: 1.a.com  解析到 1.1.1.1 2.a.com  解析到  2.2.2.2 LOOP 二  到服务器上: 在1站点设置主机 ...

  2. javascript从url中获取请求参数

    function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)( ...

  3. 安装和使用Karma-Jasmine进行自动化测试

    注意:本文中出现的资料链接.karma的插件安装等,均可能需要翻$墙后才能正确执行. Jasmine是一个Javascript的测试工具,在Karma上运行Jasmine可完成Javascript的自 ...

  4. 【Asp.Net】Asp.Net CommandName作用

    数据绑定控件的模板中 CommandName 属性以下属性值会触发特定的事件: Cancel(取消) Delete(删除) Select(选择) Edit(编辑) Insert(插入) Update( ...

  5. c语言自加自减三道题

    int  x , y,z; x = 0; y = z = -1; x += -z ---y; printf("x=%d\n",x) x = 2 为什么? x  + = -z - - ...

  6. 数据库服务器的安装 (MySQL Server 5.7) :

    MySQL 和 MariaDB 都是 Ubuntu 16.04 中的数据库服务器. MySQL Server 和 MariaDB Server的安装包都可以在Ubuntu 的默认软件源中找到,我们可以 ...

  7. [CareerCup] 11.4 Sort the File 文件排序

    11.4 Imagine you have a 20 GB file with one string per line. Explain how you would sort the file. 这道 ...

  8. UIButton利用分类扩展方法(封装)

    UIButton+BackgroundColor.h #import <UIKit/UIKit.h> @interface UIButton (BackgroundColor) - (vo ...

  9. VS2010下配置使用OpenGL的glut库

    我已在我机上测试成功,机装VS2010! 在win7(windows7 ultimate SP1)下成功安装VS2010(Visual Studio 2010 ultimate x86). 下载glu ...

  10. 一个基于.NET平台的自动化/压力测试系统设计简述

    AutoTest系统设计概述 AutoTest是一个基于.NET平台实现的自动化/压力测试的系统,可独立运行于windows平台下,支持分布式部署,不需要其他配置或编译器的支持.(本质是一个基于协议的 ...