#P10469. 后缀数组

后缀数组

Problem Description

The suffix array (SA) is an important data structure, usually implemented using the doubling method or the DC3 algorithm, which is beyond the scope of our discussion.

In this problem, we want to build a simple O(nlog2n)O(n\log^2 n) method for constructing the suffix array using quicksort, hashing, and binary search.

More specifically, given a string SS of length nn (indexed from 0n10 \sim n-1), we can use an integer k(0k<n)k(0 \le k < n) to represent the suffix S(kn1)S(k \sim n-1) of SS.

Sort all suffixes of SS in lexicographical order, and let the suffix with rank ii be denoted as SA[i].

In addition, for the suffix ranked ii and the suffix ranked i1i-1, let the length of their longest common prefix be denoted as Height[i]. In particular, define Height[0] = 0.

Your task is to output the two arrays SA and Height.

Input Format

Input a string whose length does not exceed 300000300000.

The string consists of lowercase letters.

Output Format

The first line contains the array SA, where adjacent integers are separated by exactly 11 space.

The second line contains the array Height, where adjacent integers are separated by exactly 11 space.

ponoiiipoi
9 4 5 6 2 8 3 1 7 0
0 1 2 1 0 0 2 1 0 2

Hint

Translated by ChatGPT 5