Skip to content

Commit f038f48

Browse files
author
laileon
committed
refactor
1 parent bd82f56 commit f038f48

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/com/blankj/csutom/KMP.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static int[] kmpnext(String dest) {
2121
for (int i = 1, j = 0; i < dest.length(); i++) {
2222
while (j > 0 && dest.charAt(j) != dest.charAt(i)) {
2323
j = next[j - 1];
24+
System.out.println("j1= " + j + " i1= " + i);
2425
}
2526
if (dest.charAt(i) == dest.charAt(j)) {
2627
j++;
@@ -34,6 +35,7 @@ public static int kmp(String str, String dest, int[] next) {//str文本串 dest
3435
for (int i = 0, j = 0; i < str.length(); i++) {
3536
while (j > 0 && str.charAt(i) != dest.charAt(j)) {
3637
j = next[j - 1];
38+
System.out.println("j= " + j + " i= " + i);
3739
}
3840
if (str.charAt(i) == dest.charAt(j)) {
3941
j++;
@@ -46,7 +48,8 @@ public static int kmp(String str, String dest, int[] next) {//str文本串 dest
4648
}
4749

4850
public static void main(String[] args) {
49-
String a = "abcabe";
51+
//next 数组确实是只要将各个最大前缀后缀的公共元素的长度值右移一位,且把初值赋为-1 即可
52+
String a = "abcabcaaf";
5053
String b = "abcabcabea";
5154
int[] next = kmpnext(a);
5255
int res = kmp(b, a, next);

0 commit comments

Comments
 (0)