参考 http://blog.youkuaiyun.com/hackbuteer1/article/details/6686931
public class Test4 {
static int longest_com_substr(String str1, String str2) {
int postion = 0,j = 0,len1, len2, len, s1_start = 0, s2_start = 0, curmax = 0, max = 0;
len1 = str1.length();
len2 = str2.length();
len = len1 + len2;
StringBuilder sb = new StringBuilder();
char[] str1Array = new char[len1];
str1Array = str1.toCharArray();
char[] str2Array = new char[len2];
str2Array = str2.toCharArray();
for (int i = 1; i < len; i++) {
if (i <= len1)
s1_start = len1 - i;
else
s2_start = i - len1;
curmax = 0;
for ( j = 0; (s1_start + j < len1) && (s2_start + j < len2); j++) {
if (str1Array[s1_start + j] == str2Array[s2_start + j]) {
curmax++;
} else {
if (max < curmax) {
postion = s1_start+j-1;
max = curmax;
}
curmax = 0;
}
}
if (max < curmax) {
postion = s1_start+j-1;
max = curmax;
}
}
for(int k=0;k<max;k++){
sb.append(str1Array[postion-max+1+k]);
}
System.out.println(sb);
return max;
}
public static void main(String[] args) {
int max;
max = longest_com_substr("bbbbbba", "qqqqqq");
System.out.println(max);
}
}