#P3375. 【模板】KMP

【模板】KMP

Description

Given two strings s1s_1 and s2s_2, if the substring of s1s_1 on the interval [l,r][l, r] is exactly the same as s2s_2, then s2s_2 is said to occur in s1s_1, and its occurrence position is ll.
Now please find all occurrence positions of s2s_2 in s1s_1.

Define the border of a string ss as a substring tt of ss that is not the whole ss, and satisfies that tt is both a prefix and a suffix of ss.
For s2s_2, you also need to compute, for each of its prefixes ss', the length of the longest border tt'.

Input Format

The first line contains a string, which is s1s_1.
The second line contains a string, which is s2s_2.

Output Format

First output several lines, each with an integer, output the occurrence positions of s2s_2 in s1s_1 in increasing order.
The last line outputs s2|s_2| integers, where the ii-th integer denotes the length of the longest border of the prefix of s2s_2 with length ii.

ABABABC
ABA

1
3
0 0 1 

Hint

Explanation for Sample 1

.

For the length-33 prefix ABA of s2s_2, the string A is both its suffix and its prefix, and it is the longest one, so the longest border length is 11.

Constraints and Notes

This problem uses bundled test points, with 4 subtasks.

  • Subtask 0 (30 points): s115|s_1| \leq 15, s25|s_2| \leq 5.
  • Subtask 1 (40 points): s1104|s_1| \leq 10^4, s2102|s_2| \leq 10^2.
  • Subtask 2 (30 points): No special constraints.
  • Subtask 3 (0 points): Hack.

For all test points, it is guaranteed that 1s1,s21061 \leq |s_1|, |s_2| \leq 10^6, and both s1s_1 and s2s_2 contain only uppercase English letters.

Translated by ChatGPT 5