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 ABCDGH and AEDFHR is ADH of length 3.
  • LCS for input Sequences AGGTAB and GXTXAYB is GTAB of 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;
}
												

最新文章

  1. python语句
  2. Picard 法求方程根
  3. apache 虚拟ip
  4. PE渲染引擎 一
  5. JBOSS安全配置
  6. Aspose.Cells单元格转换为数字格式
  7. VC++中内存对齐
  8. [Unity3D]支持的视频格式
  9. 老司机的奇怪noip模拟T2-huangyueying
  10. 百度API-------热力图
  11. [开源]Dapper Repository 一种实现方式
  12. BigDecimal 类型转换、运算、比较
  13. [转帖]你云我云•兄弟夜谈会 第三季 企业IT架构
  14. Gin框架初识
  15. Linux编程 3 (初识bash shell与man查看手册)
  16. async 常用函数总结
  17. 4、JUC--CountDownLatch闭锁
  18. constructor 属性返回变量或对象的构造函数。判断是否为日期,数组的例子
  19. 【洛谷】P2000 拯救世界
  20. Linux CPU 100%, kill -9 杀不掉进程

热门文章

  1. UML-领域模型-准则
  2. 剑指offer【13】- 链表中倒数第k个结点
  3. cocoaPods安装使用亲体验
  4. 六、Shell脚本高级编程实战第六部
  5. java 中的小数点、大数、随机数处理
  6. PAT甲级——1140.Look-and-say Sequence (20分)
  7. zabbix数据库占用磁盘空间较大的处理方法
  8. poj-3658 Artificial Lake(模拟)
  9. [LC] 146. LRU Cache
  10. 吴裕雄--天生自然C语言开发:存储类