public static int maxLenghtOfPublicString(String str1, String str2) {
if (str1.length() == 0 || str2.length() == 0) {
return 0;
}
int maxLength = 0;
int len1 = str1.length();
int len2 = str2.length();
int[] matrix = new int [len1 * len2];
int i, j;
// 计算数组
for (i = 0; i < len1; i++) {
String s1 = str1.substring(i, i + 1);
for (j = 0; j < len2; j++) {
String s2 = str2.substring(j, j + 1);
int index = i + j * len1;
int value = s1.equals(s2) ? 1 : 0;
matrix[index] = value;
}
}
// 统计以i开头的第一个系列的斜线
String []tmpResult = new String[len1 + len2];
for (i = 0; i < len1; i++) {
int tmpI = i;
StringBuilder sb = new StringBuilder();
for (j = 0; j < len2 && tmpI < len1; j++, tmpI++) {
int index = tmpI + j * len1;
sb.append(matrix[index]);
}
int tmpResultIndex = i;
tmpResult[tmpResultIndex] = sb.toString();
}
// 统计以j开头的第一个系列的斜线
for (j = 0; j < len2; j++) {
int tmpJ = j;
StringBuilder sb = new StringBuilder();
for (i = 0; i < len1 && tmpJ < len2; i++, tmpJ ++) {
int index = i + tmpJ * len1;
sb.append(matrix[index]);
}
//
int tmpResultIndex = len1 + j;
tmpResult[tmpResultIndex] = sb.toString();
}
// 求最长连续子序列
for (i = 0; i < tmpResult.length; i++) {
int tmpMax = 0;
String tmpString = tmpResult[i];
for (j = 0; j < tmpString.length(); j++) {
int value = Integer.parseInt(tmpString.substring(j, j + 1));
if (value == 1) {
tmpMax++;
} else {
maxLength = Math.max(maxLength, tmpMax);
tmpMax = 0;
}
}
maxLength = Math.max(maxLength, tmpMax);
}
return maxLength;
}