LCS最长公共子序列
来源:互联网 发布:哪个软件有百家讲坛 编辑:程序博客网 时间:2024/05/04 14:10
LCS最长公共子序列
邝倍靖
最长公共子序列:已知两个字符串序列x、y,求他们的最长公共子序列,去表最长公共子串--连续的。
<img src="http://img.blog.csdn.net/20161024101358883?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
具体代码实现时,使用数据结构:二维数组c[m,n],该数组表述
<img src="http://img.blog.csdn.net/20161024101909464?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
代码如下public class LCS {public static void main(String[] args) {//String str1 = "TCGGATCGACTT";//String str2 = "AGCCTACGTA";String str1 = "BCDABAB";String str2 = "CBACBAABA";int length = getMaxLength(str1, str2);}public static int getMaxLength(String str1,String str2){char[] s1 = str1.toCharArray();char[] s2 = str2.toCharArray();int size1 = str1.length();int size2 = str2.length();int[][] chess = new int[size1+1][size2+1];int i,j;for(i = 0; i<=size1; i++){chess[i][0] = 0;}for(j = 0; j<=size2; j++){chess[0][j] = 0;}for(i = 1; i<=size1; i++){for(j = 1; j<=size2; j++){if(s1[i-1] == s2[j-1]){chess[i][j] = chess[i-1][j-1] + 1;}else{chess[i][j] = Math.max(chess[i-1][j], chess[i][j-1]);}}}i = size1;j = size2;StringBuffer sb = new StringBuffer();while(i!=0 && j!=0){if(s1[i - 1] == s2[j - 1]){sb.append(s1[i-1]);i--;j--;}else{if(chess[i][j-1] > chess[i-1][j]){j--;}else {i--;}}}String result = reverse(sb.toString());System.out.println(result);return chess[size1][size2];}public static String reverse(String str){char[] charArray = str.toCharArray();char temp;int length = charArray.length;for(int i = 0; i<length/2; i++){temp = charArray[i];charArray[i] = charArray[length - i - 1];charArray[length - i - 1] = temp;}String s = String.copyValueOf(charArray);return s;}}
0 0
- LCS:最长公共子序列
- LCS---最长公共子序列
- 最长公共子序列 LCS
- LCS -- 最长公共子序列
- LCS最长公共子序列
- 最长公共子序列LCS
- LCS-最长公共子序列
- 最长公共子序列 LCS
- 最长公共子序列(LCS)
- 最长公共子序列(LCS)
- 最长公共子序列LCS
- LCS最长公共子序列
- 最长公共子序列LCS
- 最长公共子序列 LCS
- LCS最长公共子序列
- 最长公共子序列(LCS)
- 最长公共子序列LCS
- 最长公共子序列LCS
- title失效问题(select 中option过长)
- leetcode 204: Count Primes
- 使用Ant编译Apk包
- 编译和解释的区别是什么?
- Java并发编程:Callable、Future和FutureTask
- LCS最长公共子序列
- 关于使用Myeclipse 部署 web项目到tomcat上出现PermGen space的解决方案
- linux 下载一个rpm包
- CSS3 三次贝塞尔曲线(cubic-bezier)
- 查看硬盘大小
- linux git的安装与使用
- OS: 脏牛(Dirty COW)漏洞:Linux 内核通杀提权漏洞 (CVE-2016-5195)
- mysql修改乱码
- linux系统性能监控--网络利用率