您的位置:首页 > 其它

最长公共子串问题

2016-06-28 20:53 357 查看
参考  http://blog.csdn.net/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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: