private static void count(String str) {
if (str == null)
return;
int max = 0; // 重复字符串最大长度
int first = 0; // 重复字符串起始位置
int dis = 1;
int k = 0;
int j = 0;
for (int i = 0; i + dis < str.length(); i++) {
if (str.charAt(j) == str.charAt(j + dis)) {
k++;
dis++;
} else {
j++;
k = 0;
dis = 1;
}
if (k > max) {
max = k;
first = i - k + 1;
}
}
// 输出最长字串
if (max > 0) {
System.out.println(max);
for (int count = 0; count < max; count++)
System.out.println(str.charAt(first + count));
}
}
求字符串的最长重复子串
最新推荐文章于 2022-02-22 23:21:41 发布