重新定义水博客
border[y] = x 表示 b[1 ~ x] = b[(y - x + 1) ~ y]
1 2 3 4 5 6 7 8 9 10 11 12 13
| inline void find_border() { int j = 0; for (int i = 2; i <= lb; i++) { while (b[i] != b[j + 1] && j) { j = border[j]; }
if (b[i] == b[j + 1]) { j++; } border[i] = j; } }
|
1 2 3 4 5 6 7 8
| void kmp() { int j = 0; for (int i = 1; i <= la; i++) { while (a[i] != b[j + 1] && j) j = border[j]; if (a[i] == b[j + 1]) j++; if (j == lb) cout << i - j + 1 << endl,j = border[j] } }
|