6887.String Functions

Time Limit: 1s Memory Limit: 512MB

We consider a string of $n$ characters, indexed $1,2,\dots,n$ . Your task is to calculate all values of the following functions:

  • $z(i)$ denotes the maximum length of a substring that begins at position $i$ and is a prefix of the string. In addition, $z(1)=0$ .
  • $\pi(i)$ denotes the maximum length of a substring that ends at position $i$ , is a prefix of the string, and whose length is at most $i-1$ .

Note that the function $z$ is used in the Z-algorithm, and the function $\pi$ is used in the KMP algorithm.

Input Format(From the terminal/stdin)

The only input line has a string of length $n$ . Each character is between a–z.

  • $1 \le n \le 10^6$

Output Format(To the terminal/stdout)

Print two lines: first the values of the $z$ function, and then the values of the $\pi$ function.

Sample Input

Copy
abaabca
       \n

Sample Output

Copy
0 0 1 2 0 0 1
0 0 1 1 2 0 1
 · · · · · · \n
 · · · · · · \n
Source: CSES, String Algorithms, 2107

Submit

请先 登录

© 2025 FAQs