最长公共子序列LCS
2016-09-14 23:38
204 查看
package DynamicProgram; import org.junit.Test; public class LCS { // 输入两个序列,求最长公共子序列 // b和c两个数组一个用来保存计算结果,一个用来保存路径 // 数组b中用1表示左上方,2表示上方,3表示左方 public void LcsLength(int [] rows, int [] cols, int [] [] b, int [] [] c) { int rowsLength = rows.length; int colsLength = cols.length; for (int i = 1; i < rowsLength; ++i) { for (int j = 1; j < colsLength; ++j) { if (rows[i] == cols[j]) { c[i][j] = c[i-1][j-1] + 1; b[i][j] = 1; } else if (c[i][j - 1] > c[i - 1][j]) { c[i][j] = c[i][j - 1]; b[i][j] = 2; } else { c[i][j] = c[i - 1][j]; b[i][j] = 3; } } } } public void printLcs(int [] [] b, int [] rows, int i, int j) { if (i == 0 || j == 0) { return; } if (b[i][j] == 1) { System.out.println((char) rows[i]); printLcs(b, rows, i - 1, j - 1); } else if (b[i][j] == 2) { printLcs(b, rows, i, j - 1); } else printLcs(b, rows, i - 1, j); } @Test public void test() { // 下标为0的索引值暂时不用 int [] cols = {-1, 'A', 'B', 'C', 'B', 'D', 'A', 'B'}; int [] rows = {-1, 'B', 'D', 'C', 'A', 'B', 'A'}; // 准备工作 // 其实算法的空间复杂度可以进一步的优化 int rowsLength = rows.length; int colsLength = cols.length; // 声明两个m行n列的数组,这里是因为数组的下标0没有使用,否则的话需要数组的长度+1 // 用来记录一些额外的路径信息 int [][] path = new int [rowsLength][colsLength]; // 用来记录子问题,即最长子序列的长度 int [][] len = new int [rowsLength][colsLength]; for (int j = 0; j < rowsLength; ++j) len[j][0] = 0; for (int i = 0; i < colsLength; ++i) len[0][i] = 0; LcsLength(rows, cols, path, len); //System.out.println(len[rowsLength - 1][colsLength - 1]); // 注意这个打印出来的一个最优LCS的反序输出 printLcs(path, rows, rowsLength - 1, colsLength - 1); } }
相关文章推荐
- JavaWEB_POI导出大量数据excel(50万左右)
- 人体呼吸信号的数据挖掘
- xamarin android如何监听单击事件
- [Android] SeekBar 基本使用
- 人体呼吸信号的数据挖掘
- xamarin android如何监听单击事件
- java相关术语解释 terms of java
- Struts2初识
- 取原图旋转角度(IOS和Android相机拍的照片) 自动旋转
- JavaScript 数组详解
- (SSM框架)SpringMVC+Spring4+Mybatis+Maven环境快速搭建开发案例
- python核心编程学习笔记-2016-09-14-03-Web编程(七)
- 18 UI美化之level(等级显示显示)
- C++ 虚函数表解析
- 18 UI美化之level(等级显示显示)
- 聚类算法
- JAVAWEB创建和发送邮件
- LeetCode: Climbing Stairs
- JavaScript中的style.width/height和offsetWidth/height区别
- ObjectAnimator基本使用