Java代码实现
/**
* 查找两个串的最大公共子串
*
* @param s1
* @param s2
* @return
*/
public static String commonMaxSubString(String s1, String s2) {
String maxstr = "";
String substring = "";
if (s1.length() > s2.length()) { // s1为两个串中的短串;s2为长串
String temp = s1;
s1 = s2;
s2 = temp;
}
int len = s1.length();
ok: for (int i = len; i > 0; i--) {
for (int j = 0; j < len - i + 1; j++) {
substring = s1.substring(j, j + i);
if (s2.indexOf(substring) != -1) {
maxstr = substring;
break ok; // 只要一找到最大子串,就退出这个for循环
}
}
}
return maxstr;
}
posted on 2009-08-20 23:42
jadmin 阅读(76)
评论(0) 编辑 收藏