求最长公共子序列-DP问题
Longest common subsequence problem
The longest common subsequence (LCS) problem is the problem of finding the longest subsequence common to all sequences in a set of sequences (often just two sequences). It differs from the longest common substring problem: unlike substrings, subsequences are not required to occupy consecutive positions within the original sequences.
Example
- LCS for input Sequences
ABCDGHandAEDFHRisADHof length 3. - LCS for input Sequences
AGGTABandGXTXAYBisGTABof length 4
以s1={1,3,4,5,6,7,7,8},s2={3,5,7,4,8,6,7,8,2}为例。
1.构建二维矩阵A[n+1][n+1] s1为列,s2为行
(1)A[0][any]=0,A[any][0]=0
(2)if(s1[columnIndex-1]==s2[rowIndex-1])则A[rowIndex][columnIndex]=A[rowIndex-1][columnIndex-1]+1;
else A[rowIndex][columnIndex]=max(A[rowIndex][columnIndex-1],A[rowIndex-1][columnIndex])
(3)由A[8][9]可知最大子序列长度。
图如下:

2.针对构建的二维矩阵求最长子序列
(1)当columnIndex>0或者rowIndex>0时。
(2)如果s1[columnIndex-1]==s2[rowIndex-1]则longestSequence.unshift(s1[columnIndex-1]).rowIndex--;columnIndex--.
(3)如果不等,
则1.如果A[rowIndex][columnIndex]==A[rowIndex][columnIndex-1];columnIndex--;//向左
2.否则 rowIndex--;

代码如下:
/**
* @param {string[]} set1
* @param {string[]} set2
* @return {string[]}
*/
export default function longestCommonSubsequence(set1, set2) {
// Init LCS matrix.
const lcsMatrix = Array(set2.length + ).fill(null).map(() => Array(set1.length + ).fill(null)); // Fill first row with zeros.
for (let columnIndex = ; columnIndex <= set1.length; columnIndex += ) {
lcsMatrix[][columnIndex] = ;
} // Fill first column with zeros.
for (let rowIndex = ; rowIndex <= set2.length; rowIndex += ) {
lcsMatrix[rowIndex][] = ;
} // Fill rest of the column that correspond to each of two strings.
for (let rowIndex = ; rowIndex <= set2.length; rowIndex += ) {
for (let columnIndex = ; columnIndex <= set1.length; columnIndex += ) {
if (set1[columnIndex - ] === set2[rowIndex - ]) {
lcsMatrix[rowIndex][columnIndex] = lcsMatrix[rowIndex - ][columnIndex - ] + ;
} else {
lcsMatrix[rowIndex][columnIndex] = Math.max(
lcsMatrix[rowIndex - ][columnIndex],
lcsMatrix[rowIndex][columnIndex - ],
);
}
}
} // Calculate LCS based on LCS matrix.
if (!lcsMatrix[set2.length][set1.length]) {
// If the length of largest common string is zero then return empty string.
return [''];
} const longestSequence = [];
let columnIndex = set1.length;
let rowIndex = set2.length; while (columnIndex > || rowIndex > ) {
if (set1[columnIndex - ] === set2[rowIndex - ]) {
// Move by diagonal left-top.
longestSequence.unshift(set1[columnIndex - ]);
columnIndex -= ;
rowIndex -= ;
} else if (lcsMatrix[rowIndex][columnIndex] === lcsMatrix[rowIndex][columnIndex - ]) {
// Move left.
columnIndex -= ;
} else {
// Move up.
rowIndex -= ;
}
} return longestSequence;
}
求最长公共子序列-DP问题的更多相关文章
- HDU 4681 string 求最长公共子序列的简单DP+暴力枚举
先预处理,用求最长公共子序列的DP顺着处理一遍,再逆着处理一遍. 再预处理串a和b中包含串c的子序列,当然,为了使这子序列尽可能短,会以c 串的第一个字符开始 ,c 串的最后一个字符结束 将这些起始位 ...
- Java实现 LeetCode 583 两个字符串的删除操作(求最长公共子序列问题)
583. 两个字符串的删除操作 给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符. 示例: 输入: " ...
- [algorithm]求最长公共子序列问题
最直白方法:时间复杂度是O(n3), 空间复杂度是常数 reference:http://blog.csdn.net/monkeyandy/article/details/7957263 /** ** ...
- HDU 1243 反恐训练营 (动态规划求最长公共子序列)
反恐训练营 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- poj1458 求最长公共子序列 经典DP
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45763 Accepted: 18 ...
- 【dp】求最长公共子序列
[题目描述] 一个给定序列的子序列是在该序列中删去若干元素后得到的序列.确切地说,若给定序列X=<x1,x2,…,xm>X=<x1,x2,…,xm>,则另一序列Z=<z1 ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- LCS最长公共子序列~dp学习~4
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 Palindrome Time Limit: 4000/2000 MS (Java/Others ...
- POJ 1458 最长公共子序列(dp)
POJ 1458 最长公共子序列 题目大意:给出两个字符串,求出这样的一 个最长的公共子序列的长度:子序列 中的每个字符都能在两个原串中找到, 而且每个字符的先后顺序和原串中的 先后顺序一致. Sam ...
随机推荐
- Samba 文件共享
介绍:用于Linux系统与Windows系统之间共享文件资源,小文件可以使用lrzsz,大文件可以使用samba 测试环境: [root@localhost home]# cat /etc/redha ...
- PAT Advanced 1127 ZigZagging on a Tree (30) [中序后序建树,层序遍历]
题目 Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree c ...
- python学习笔记-面向对象设计
前言 1.三大编程范式: 面向过程编程 函数式编程 面向对象编程 2.编程进化论 1.编程最开始就是无组织无结构,从简单控制流中按步写指令 2.从上述的指令中提取重复的代码块或逻辑,组织到一起,便实现 ...
- PowerShell创建 Profile
profile主要用于个性化常用的函数.别名等等.每次加载powershell的时候,都会执行profile中的内容. 查看是否有profile: $profile 如果结果是false说明没有.则创 ...
- C++随机迷宫生成[转载]
原文:http://tieba.baidu.com/p/2596809144 #include<iostream.h> #include"time.h" #includ ...
- 17.3.13--pycharm2和3的常识问题
1------pycharm 3~支持print(),不支持那个 print+一个空格的形式 pycharm2~~支持print+一个空格,,也支持那个print()形式 虽然print语法是Pyth ...
- msgfmt - 翻译汉化
说明 目前大部分自由软件实现国际化使用的是gettext. 国际化就是让程序可以使用多国语言来显示程序里的字符串. 程序里一般都有很多字符串,菜单名也好,错误信息也好,都是字符串.假设字符串为stri ...
- Android开发学习3
学习内容: 1.复选框CheckBox 2.ImageView & 使用第三方库加载网络图片 3.列表视图ListView 4.网格视图GridView 5.ScrollView & ...
- C++ for循环遍历几种写法
最近写for循环,发现以前用过的方法都忘记了,这里整理下几种方法,欢迎大佬补充: 1. for(itnt n =1;n<5;n++) { } 2. for (auto it = list.beg ...
- 887A. Div. 64#模特的数学技巧(字符串处理)
问题出处:http://codeforces.com/problemset/problem/887/A 问题大意:对于给出的一串二进制数,能否通过去掉一些数字,使之成为十进制下64的倍数 #inclu ...