找两个字符串的最长公共子序列的长度
2012-10-21 23:57
190 查看
简述:
找两个字符串的最长公共子序列的长度
例如
String str1 = “caaac”;
String str2 = "bbdaaaca";
两者最长公共子序列就是aaac 为4
代码实现:
输出:
![](http://img.my.csdn.net/uploads/201210/21/1350833691_2324.JPG)
但是由于只要保留斜对角线的resultArray[i - 1][ j - 1] 所以没有必要声明整个二维数组 ,只要保留一个维度就可以了
, 最后遍历一下得到每一次生成行的最大值,下面就是按照这个思想实现的算法
代码2(只保留一维数组)
输出:(循环单行输出)
找两个字符串的最长公共子序列的长度
例如
String str1 = “caaac”;
String str2 = "bbdaaaca";
两者最长公共子序列就是aaac 为4
代码实现:
package offer; public class LongestCommonSubsequence { public static void main(String[] args) { LongestCommonSubsequence obj = new LongestCommonSubsequence(); String str1 = "caaac"; String str2 = "bbdaaaca"; System.out.println("LongestCommonSubsequence: " + obj.getMaxLength(str1, str2)); } public int getMaxLength(String str1, String str2){ int m = str1.length(); int n = str2.length(); int resultArray[][] = new int[m + 1][n + 1]; int maxLength = 0; //initialize the raw array for(int i = 1; i <= m; i++) for(int j = 1; j <= n; j++){ if(str1.charAt(i - 1) == str2.charAt(j - 1) && resultArray[i - 1][j - 1] != 0 ) resultArray[i][j] = 1 + resultArray[i - 1][j - 1]; else if(str1.charAt(i - 1) == str2.charAt(j - 1)) resultArray[i][j] = 1; else resultArray[i][j] = 0; } //output the resultArray System.out.println("resultArray: "); for(int i = 1; i <= m; i++){ for(int j = 1; j <= n; j++){ System.out.print(resultArray[i][j] + ", "); } System.out.println(); } for(int i = 0; i <= m; i++) for(int j = 0; j <= n; j++){ if(resultArray[i][j] > maxLength) maxLength = resultArray[i][j]; } return maxLength; } }
输出:
但是由于只要保留斜对角线的resultArray[i - 1][ j - 1] 所以没有必要声明整个二维数组 ,只要保留一个维度就可以了
, 最后遍历一下得到每一次生成行的最大值,下面就是按照这个思想实现的算法
代码2(只保留一维数组)
package offer; public class LongestCommonSubsequence { public static void main(String[] args) { LongestCommonSubsequence obj = new LongestCommonSubsequence(); String str1 = "caaac"; String str2 = "bbdaaaca"; System.out.println("LongestCommonSubsequence: " + obj.getMaxLength(str1, str2)); } public int getMaxLength(String str1, String str2){ int m = str1.length(); int n = str2.length(); //use tempArray to restore the data int tempArray[] = new int ; int maxLength = 0; for(int i = 0; i < m; i++){ for(int j = n - 1; j >= 0; j--){ if(str1.charAt(i) != str2.charAt(j)) tempArray[j] = 0; else{ tempArray[j] = tempArray[j - 1] + 1; if(tempArray[j] > maxLength) maxLength = tempArray[j]; } } if(str1.charAt(i) != str2.charAt(0)){ tempArray[0] = 0; if(tempArray[0] > maxLength) maxLength = tempArray[0]; } /** * output for test ****************************************/ for(int k = 0; k < n; k++) System.out.print(tempArray[k] + ", "); System.out.println(); /****************************************/ } return maxLength; } }
输出:(循环单行输出)
相关文章推荐
- 比较两个字符串的最长公共子序列的长度,字符不区分大小写
- 两个字符串的最长公共子序列的长度
- 给出两个字符串,找到最长公共子序列(LCS),返回LCS的长度。C实现
- 求两个字符串的最长公共子序列的长度(动态规划)
- 求两个字符串的最长公共子序列的长度(动态规划)
- 求两个字符串的最长公共子序列的长度(动态规划)
- 求两个字符串最长公共子串的长度
- ZOJ1733 | | HDU1159简单的DP求两个字符串最大子序列的长度,没啥好说的,照着书上敲得。
- Python实现求两个字符串的最长公共子序列的算法
- 计算两个字符串间符合字典序且在一定长度内的字符串个数
- 默认字符编码是UTF8的linux服务器,java计算字符串长度(半角算一个,全角算两个)
- 获取字符串的长度 (汉字占两个字节)
- JS 判断输入字符串的长度(中文占用两个字节,英文占用一个字节)
- JS 判断输入字符串的长度(中文占用两个字节,英文占用一个字节)
- Base64 报错 的解决办法 (Base-64 字符数组或字符串的长度无效。, 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符。)
- 求两个字符串的公共子串的最大长度
- JS判断输入字符串长度(汉字算两个字符,字母数字算一个)
- 求两个字符串公共子序列的最大长度(参考阿里巴巴2015研发笔试)(简单)
- java_基础知识_字符串练习题_计算两个字符串的最长公共字串长度
- leetcode_318. Maximum Product of Word Lengths 求两个不相交的字符串的长度乘积的最大值,将字母转换成二进制形式,按位与比较是否有相同字母