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 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| #include <stdio.h> #include <stdlib.h> #include <string.h>
int main() { char s1[1000010], s2[1000010]; scanf("%s", s1); scanf("%s", s2); int len1 = strlen(s1); int len2 = strlen(s2); int len = len2; int *next = (int *)malloc(sizeof(int) * len2); next[0] = 0; int x = 1, now = 0; while (x < len2) { if (s2[now] == s2[x]) next[x++] = ++now; else if (now) now = next[now - 1]; else next[x++] = 0; } x = now = 0; while (x < len1) { if (s1[x] == s2[now]) { x++; now++; } else if (now) now = next[now - 1]; else x++; if (now == len2) { printf("%d\n", x - now + 1); now = next[now - 1]; } } while (len2--) { printf("%d ", next[len - len2 - 1]); } return 0; }
|