标签:length class next else while kmp get ret ++
int[] getNext(String ps) {
next[0] = -1;
int j = 0;
int k = -1;
while (j < p.length - 1) {
if (k == -1 || p[j] == p[k]) {
if (p[++j] == p[++k]) {
next[j] = next[k];
} else {
next[j] = k;
}
} else {
k = next[k];
}
}
return next;
}
int KMP(String ts, String ps) {
int i = 0;
int j = 0;
int[] next = getNext(ps);
while (i < t.length && j < p.length) {
if (j == -1 || t[i] == p[j]) {
i++;
j++;
} else {
j = next[j];
}
}
if (j == p.length) {
return i - j;
} else {
return -1;
}
}
标签:length class next else while kmp get ret ++
原文地址:https://www.cnblogs.com/ukcxrtjr/p/11195031.html