This page is still under construction.

Parts of this page are still being built. What you see may change.

Suffix Array

Time limit3sMemory limit256 MB

Summary
Read a lowercase string of length up to 500000 and print its suffix array and LCP array, writing x for the first LCP entry.
Level

Medium7 of 10

Topics
String matching, Sorting
Solved
No attempts yet

Problem

The suffix array of a string SS lists the starting positions of every suffix of SS in lexicographic order. Positions are counted from 1. For example, if SS is banana, there are six suffixes.

SuffixStarting position
banana1
anana2
nana3
ana4
na5
a6

Sorted lexicographically, they come out in this order.

SuffixStarting position
a6
ana4
anana2
banana1
na5
nana3

Reading the starting positions down the sorted table gives [6, 4, 2, 1, 5, 3], the suffix array of banana.

The LCP array is built on top of the suffix array. For each suffix in sorted order it stores the length of the LCP (Longest Common Prefix) shared with the suffix immediately before it. The first suffix has nothing before it, so its value is undefined. For banana the LCP array is [x, 1, 3, 0, 0, 2].

Given a string of length at most 500000, write a program that computes its suffix array and its LCP array.

Input

The first line contains a string SS made up of lowercase letters only. The length of SS is at most 500000.

Output

Print the suffix array on the first line and the LCP array on the second line, separating values with a single space. Print the first value of the LCP array as x.

Examples2

  1. Example 1

    Input
    abracadabra
    
    Expected output
    11 8 1 4 6 9 2 5 7 10 3
    x 1 4 1 1 0 3 0 0 0 2
    
  2. Example 2

    Input
    banana
    
    Expected output
    6 4 2 1 5 3
    x 1 3 0 0 2