最长公共子序列(LCS)是经典的DP问题,求序列a[1...n], b[1..m]的LCS。

状态是DP[i][j],表示a[1..i],b[1..j]的LCS。

DP转移方程是

DP[i][j]=

  DP[i-1][j-1]+1,  a[i] == b[j]

  max{ DP[i][j-1], DP[i-1][j] },  a[i] != b[i] 

-------------------------------------------------------------------------------------------

时间复杂度O(N^2),空间复杂度0(N^2)。

使用滚动数组,可将空间复杂度降到 0(N)。

观察DP转移方程可看出,即使用滚动数组,也需要两个即DP[2][N],一个DP[N]行不通。

因为若只用一维数组DP[N]来保存状态,第一个式子要求从右向左更新,第二个式子要求从左向右更新。

------------------------------------------------------------------------------------

以上关于用滚动数组降低空间复杂度的论述有误

----------------------------------------------------------------

实际上只用一维数组DP[N]也可以。严格地说,上面的论述并没有错,若严格按照

DP[i][j]=

  DP[i-1][j-1]+1,  a[i] == b[j]

  max{ DP[i][j-1], DP[i-1][j] },  a[i] != b[i] 

来转移,一个DP[N]确实不够,但我们深入分析下一开始的论据--"第一个式子要求从右向左更新",

如果第一式也从左向右更新,那么在需要DP[i-1][j-1]时,它已被DP[i][j-1]覆盖

自然地,我们考虑把DP[i-1][j-1]单独存起来,问题就解决了。

---------------------------------------------------------------------------------------------------------------------------------------

还有一种思路,我们略微变通一下,将第一个转移方程改为

DP[i][j] = max{ DP[i-1][k] : k < j } +1

这样只要在从左到右更新时维护一个max{ DP[i-1][k] : k < j }

而DP[i-1][k] >= DP[i-1][k-1] (k >=1),所以实际上只要在计算DP[i][j]之前,把DP[i-1][j]存起来以备查询。

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

伪代码

FOR i := 0 to n

  dp[i] := 0

END FOR

FOR i := 1 to n

  tmp := dp[0]

  FOR j := 1 to m

    IF a[i] = b[j]

      IF tmp = dp[j]

        dp[j] := tmp + 1

      ELSE

        tmp := dp[j]

      END IF

    ELSE

      tmp := dp[j]

      dp[j] := max{dp[j], dp[j-1]}

    END IF

  END FOR

END FOR

      

Longest Common Subsequence (LCS)的更多相关文章

  1. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  2. 最长公共字串算法, 文本比较算法, longest common subsequence(LCS) algorithm

    ''' merge two configure files, basic file is aFile insert the added content of bFile compare to aFil ...

  3. 动态规划 ---- 最长公共子序列(Longest Common Subsequence, LCS)

    分析: 完整代码: // 最长公共子序列 #include <stdio.h> #include <algorithm> using namespace std; ; char ...

  4. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  5. Longest Common Subsequence

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  6. Longest Common Subsequence & Substring & prefix

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  7. Lintcode:Longest Common Subsequence 解题报告

    Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...

  8. [Algorithms] Longest Common Subsequence

    The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the le ...

  9. 【Lintcode】077.Longest Common Subsequence

    题目: Given two strings, find the longest common subsequence (LCS). Your code should return the length ...

随机推荐

  1. Android 动态加载 (一) 态加载机制 案例一

    在目前的软硬件环境下,Native App与Web App在用户体验上有着明显的优势,但在实际项目中有些会因为业务的频繁变更而频繁的升级客户端,造成较差的用户体验,而这也恰恰是Web App的优势.本 ...

  2. eval() 函数

    eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. var str = '12+45*45'; alert(eval(str))//计算结果 还有一个重要作用可以把字符串 ...

  3. UIImageJPEGRepresentation和UIImagePNGRepresentation

    UIImageJPEGRepresentation方法在耗时上比较少 而UIImagePNGRepresentation耗时操作时间比较长 -(void)imagePickerController:( ...

  4. Elasticsearch 相关名词理解

    Cluster包含多个node,Indices不应该理解成动词索引,Indices可理解成关系数据库中的databases,Indices可包含多个Index,Index对应关系数据库中的databa ...

  5. android Camera 中添加一种场景模式

    转自:http://blog.csdn.net/fulinwsuafcie/article/details/8833652 首先,来了解一下什么是场景模式. 最简单的方法当然是google了,这里有一 ...

  6. LUA GC 简单测试

    function table.count(t) if type(t) ~= "table" then assert(false) return end for k, _ in pa ...

  7. 实战 SQL Server 2008 数据库误删除数据的恢复

    SQL Server中误删除数据的恢复本来不是件难事,从事务日志恢复即可.但是,这个恢复需要有两个前提条件: 1. 至少有一个误删除之前的数据库完全备份. 2. 数据库的恢复模式(Recovery m ...

  8. REST签名认证

    139 开放平台与应用之间以REST协议进行通讯,为了保证通信的安全性,开放平台加入签名认证机制.应用一旦创建,系统生成唯一并且不公开的secretkey,只有应用的拥有者和开放平台知道.因此,当应用 ...

  9. 从0开始学java——JSP&Servlet——web容器搜索class的路径顺序

    在web应用程序如果要用到某个类,会按照如下的顺序来搜索: 1)在WEB-INF/classes目录下搜索: 2)如果该目录下没有,则会到WEB-INF/lib目录下的jar文件中搜索: 3)如果还没 ...

  10. virtualbox 打不开ubuntu解决

    装了一个win7x64,准备打开ubuntu12.04,后来竟然报错(最新版的virtualbox,VirtualBox-4.3.18-96516-Win): 也没找到什么原因,网上查了之后,禁用了w ...