public static int lcs(String str1, String str2) {
int len1 = str1.length();
int len2 = str2.length();
int c[][] = new int[len1+1][len2+1];
for (int i = 0; i <= len1; i++) {
for( int j = 0; j <= len2; j++) {
if(i == 0 || j == 0) {
c[i][j] = 0;
} else if (str1.charAt(i-1) == str2.charAt(j-1)) {
c[i][j] = c[i-1][j-1] + 1;
} else {
c[i][j] = max(c[i - 1][j], c[i][j - 1]);
}
}
}
return c[len1][len2];
}
public class LCSProblem
{
public static void main(String[] args)
{
//保留白字元串是為了getLength()方法的完整性也可以不保留
//但是在getLength()方法裡面必須額外的初始化c[][]第一個行第一列
String[] x = {"", "A", "B", "C", "B", "D", "A", "B"};
String[] y = {"", "B", "D", "C", "A", "B", "A"};
int[][] b = getLength(x, y);
Display(b, x, x.length-1, y.length-1);
}
/**
* @param x
* @param y
* @return 傳回一個記錄決定搜尋的方向的數組
*/
public static int[][] getLength(String[] x, String[] y)
{
int[][] b = new int[x.length][y.length];
int[][] c = new int[x.length][y.length];
for(int i=1; i<x.length; i++)
{
for(int j=1; j<y.length; j++)
{
//對應第一個性質
if( x[i] == y[j])
{
c[i][j] = c[i-1][j-1] + 1;
b[i][j] = 1;
}
//對應第二或者第三個性質
else if(c[i-1][j] >= c[i][j-1])
{
c[i][j] = c[i-1][j];
b[i][j] = 0;
}
//對應第二或者第三個性質
else
{
c[i][j] = c[i][j-1];
b[i][j] = -1;
}
}
}
return b;
}
//回溯的基本實作,采取遞歸的方式
public static void Display(int[][] b, String[] x, int i, int j)
{
if(i == 0 || j == 0)
return;
if(b[i][j] == 1)
{
Display(b, x, i-1, j-1);
System.out.print(x[i] + " ");
}
else if(b[i][j] == 0)
{
Display(b, x, i-1, j);
}
else if(b[i][j] == -1)
{
Display(b, x, i, j-1);
}
}
}
public static int lcs(String str1, String str2) {
int len1 = str1.length();
int len2 = str2.length();
int result = 0; //記錄最長公共子串長度
int c[][] = new int[len1+1][len2+1];
for (int i = 0; i <= len1; i++) {
for( int j = 0; j <= len2; j++) {
if(i == 0 || j == 0) {
c[i][j] = 0;
} else if (str1.charAt(i-1) == str2.charAt(j-1)) {
c[i][j] = c[i-1][j-1] + 1;
result = max(c[i][j], result);
} else {
c[i][j] = 0;
}
}
}
return result;
}
public class stringCompare {
//在動态規劃矩陣生成方式當中,每生成一行,前面的那一行就已經沒有用了,是以這裡隻需使用一維數組,而不是常用的二位數組
public static void getLCString(char[] str1, char[] str2) {
int len1, len2;
len1 = str1.length;
len2 = str2.length;
int maxLen = len1 > len2 ? len1 : len2;
int[] max = new int[maxLen];// 儲存最長子串長度的數組
int[] maxIndex = new int[maxLen];// 儲存最長子串長度最大索引的數組
int[] c = new int[maxLen];
int i, j;
for (i = 0; i < len2; i++) {
for (j = len1 - 1; j >= 0; j--) {
if (str2[i] == str1[j]) {
if ((i == 0) || (j == 0))
c[j] = 1;
else
c[j] = c[j - 1] + 1;//此時C[j-1]還是上次循環中的值,因為還沒被重新指派
} else {
c[j] = 0;
}
// 如果是大于那暫時隻有一個是最長的,而且要把後面的清0;
if (c[j] > max[0]) {
max[0] = c[j];
maxIndex[0] = j;
for (int k = 1; k < maxLen; k++) {
max[k] = 0;
maxIndex[k] = 0;
}
}
// 有多個是相同長度的子串
else if (c[j] == max[0]) {
for (int k = 1; k < maxLen; k++) {
if (max[k] == 0) {
max[k] = c[j];
maxIndex[k] = j;
break; // 在後面加一個就要退出循環了
}
}
}
}
for (int temp : c) {
System.out.print(temp);
}
System.out.println();
}
//列印最長子字元串
for (j = 0; j < maxLen; j++) {
if (max[j] > 0) {
System.out.println("第" + (j + 1) + "個公共子串:");
for (i = maxIndex[j] - max[j] + 1; i <= maxIndex[j]; i++)
System.out.print(str1[i]);
System.out.println(" ");
}
}
}
public static void main(String[] args) {
String str1 = new String("binghaven");
String str2 = new String("jingseven");
getLCString(str1.toCharArray(), str2.toCharArray());
}
}
/*
000000000
010000000
002000001
000300000
000000000
000000010
000000100
000000020
001000003
第1個公共子串:
ing
第2個公共子串:
ven
*/