1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| const int N, M; int n, m; char p[N],s[M]; void init() { ne[0] = -1; for (int i = 0, j = -1; i < n;) { if (j == -1 || p[i] == p[j]) { ++i, ++j; ne[i] = j; } else j = ne[j]; } }
void kmp() { for (int i = -1, j = -1; i < m;) { if (j == -1 || p[j] == s[i]) ++i, ++j; else j = ne[j]; if (j == n) { cout << i - j << " "; } } }
|