package DynamicProgramming;class LongestCommonSubsequence {public static String getLCS(String str1, String str2) {//At least one string is nullif (str1 == null || str2 == null)return null;//At least one string is emptyif (str1.length() == 0 || str2.length() == 0)return "";String[] arr1 = str1.split("");String[] arr2 = str2.split("");//lcsMatrix[i][j] = LCS of first i elements of arr1 and first j characters of arr2int[][] lcsMatrix = new int[arr1.length + 1][arr2.length + 1];for (int i = 0; i < arr1.length + 1; i++)lcsMatrix[i][0] = 0;for (int j = 1; j < arr2.length + 1; j++)lcsMatrix[0][j] = 0;for (int i = 1; i < arr1.length + 1; i++) {for (int j = 1; j < arr2.length + 1; j++) {if (arr1[i - 1].equals(arr2[j - 1])) {lcsMatrix[i][j] = lcsMatrix[i - 1][j - 1] + 1;} else {lcsMatrix[i][j] = lcsMatrix[i - 1][j] > lcsMatrix[i][j - 1] ? lcsMatrix[i - 1][j] : lcsMatrix[i][j - 1];}}}return lcsString(str1, str2, lcsMatrix);}public static String lcsString(String str1, String str2, int[][] lcsMatrix) {StringBuilder lcs = new StringBuilder();int i = str1.length(),j = str2.length();while (i > 0 && j > 0) {if (str1.charAt(i - 1) == str2.charAt(j - 1)) {lcs.append(str1.charAt(i - 1));i--;j--;} else if (lcsMatrix[i - 1][j] > lcsMatrix[i][j - 1]) {i--;} else {j--;}}return lcs.reverse().toString();}public static void main(String[] args) {String str1 = "DSGSHSRGSRHTRD";String str2 = "DATRGAGTSHS";String lcs = getLCS(str1, str2);//Print LCSif (lcs != null) {System.out.println("String 1: " + str1);System.out.println("String 2: " + str2);System.out.println("LCS: " + lcs);System.out.println("LCS length: " + lcs.length());}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。