Longest Common Subsequence (LCS)
最长公共子序列(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)的更多相关文章
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 最长公共字串算法, 文本比较算法, longest common subsequence(LCS) algorithm
''' merge two configure files, basic file is aFile insert the added content of bFile compare to aFil ...
- 动态规划 ---- 最长公共子序列(Longest Common Subsequence, LCS)
分析: 完整代码: // 最长公共子序列 #include <stdio.h> #include <algorithm> using namespace std; ; char ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
- Longest Common Subsequence
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- Longest Common Subsequence & Substring & prefix
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- Lintcode:Longest Common Subsequence 解题报告
Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...
- [Algorithms] Longest Common Subsequence
The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the le ...
- 【Lintcode】077.Longest Common Subsequence
题目: Given two strings, find the longest common subsequence (LCS). Your code should return the length ...
随机推荐
- 一篇搞定RSA加密与SHA签名|与Java完全同步
基础知识 什么是RSA?答:RSA是一种非对称加密算法,常用来对传输数据进行加密,配合上数字摘要算法,也可以进行文字签名. RSA加密中padding?答:padding即填充方式,由于RSA加密算法 ...
- addEvent和removeEvent优化写法
;(function(){ /** * 初始化分支是一种优化模式,当知道某个条件在整个生命周期内都不会发生变化时,仅对该条件测试一次. */ // 一般写法 var util = { addEvent ...
- android studio使用说明
一.学习的基本配置文档,搞好各种参数的基本配置,熟练使用. C:\Program Files\Java\jdk1.7.0_09\bin 二.problems meet in weather and ...
- 视频会议的3G智能手机移植技术
现今的视频会议系统已经兼容3G手机等移动终端设备,而3G智能手机使用的操作系统一般与PC的操作系统不一样,其开发环境一般都在PC上进行,通过模拟器在PC上进行手机系统的应用程序开发,而在这些操作系统上 ...
- [资料收集]MySQL在线DDL工具pt-online-schema-change
MySQL在线DDL工具pt-online-schema-change pt-online-schema-change使用说明(未完待续) 官网
- JS 之原型,实例,构造函数之间的关系
JS是面向对象的语言,函数也是对象.下面大致介绍下实例,原型与构造函数之间的关系. 构造函数模式 function Person(name,age){ this.name = name; this.a ...
- ace布置小作业: 制作一个简单的电话号码归属地查询软件:JSON解析和Volly发送get请求
大概就这个样子 用到JSON解析和Volly发送Get请求两个知识点 关于Volly的用法请看我的这篇: http://www.cnblogs.com/AceIsSunshineRain/p/5177 ...
- LeetCode:Jump Game I II
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
- MyBatis与Hibernate对比
一.相同点 都屏蔽 jdbc api 的底层访问细节,使用我们不用与 jdbc api 打交道,就可以访问数据. jdbc api 编程流程固定,还将 sql 语句与 java 代码混杂在了一起,经常 ...
- initializer for conditional binding must have optional type not AVAudioPlayer
if let buttonBeep = self.setupAudioPlayerWithFile("ButtonTap", type: "wav") { ...