KMP算法--通俗易通
public class KMPAlgorithm {
public static void main(String[] args) {
String str1 = "BBC ABCDAB ABCDABCDABDE";
String str2 = "ABCDABD";
int[] kmpTable = kmpTable(str2);
System.out.println(Arrays.toString(kmpTable));
}
public static int[] kmpTable(String dest) {
int next[] = new int[dest.length()];
next[0] = 0; // 如果字符的长度为1,则部分匹配值就是0
for (int i = 0, j = 1; j < dest.length(); j++) { //ABCDABD
while (i > 0 && dest.charAt(i) != dest.charAt(j)) {
i = next[i - 1];
}
if (dest.charAt(i) == dest.charAt(j)) { //
i++;
}
next[j] = i;
}
return next;
}
} 相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
ChaITSimpleLove 2020-10-06
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25