만드는 중입니다

아직 만드는 중이라 바뀔 수 있습니다

Suffix Sort

시간 제한4초메모리 제한1024 MB

요약
소문자 문자열의 모든 접미사를 최소 표현 R(P)의 사전순으로 정렬하고 각 접미사의 시작 인덱스 배열을 출력합니다.
난이도

어려움10점 중 8점

유형
문자열, 문자열 매칭, 정렬
정답자
아직 제출이 없습니다

입력과 출력

Grammy has a string SS of length nn that consists of lowercase English letters.

For a string PP, reading it left to right, write down the letters that never occurred before as t_1,t_2,t_3,…,t_kt\_1, t\_2, t\_3, \ldots, t\_k. For example, if P=P ="sesame", we write down 's', 'e', 'a', 'm'. The minimal representation R(P)R(P) can be obtained by replacing every occurrence of t_1t\_1 in PP by the first character of the character set ("a"), replacing every occurrence of t_2t\_2 in PP by the second character of the character set ("b"), and so on.

For example, when the character set is lowercase English letters, the minimal representation of "sesame" is "abacdb", the minimal representation of "edcca" is "abccd", and minimal representations R(R("xy")) and R(R("zt")) are both "ab".

Your task is to sort all suffixes of SS by their minimal representation. Formally, denote suffix S_iS_i+1…S_n−1S_nS\_i S\_{i + 1} \ldots S\_{n - 1} S\_n as S\[i:]S\[i{:}]. For two suffixes S\[i:]S\[i{:}] and S\[j:]S\[j{:}], if R(S\[i:])R(S\[i{:}]) is less than R(S\[j:])R(S\[j{:}]) in lexicographical order, then S\[i:]S\[i{:}] has to occur before S\[j:]S\[j{:}] in the desired order.

Please output the result as an array of indices sa\mathit{sa}: the ii-th element of sa\[i]\mathit{sa}\[i] must be the position of the first character in the ii-th smallest suffix of SS in the desired order. Formally, the array must satisfy R(S\[sa\[1]:])<R(S\[sa\[2]:])<…<R(S\[sa\[n]:]).R(S\[\mathit{sa}\[1]{:}]) < R(S\[\mathit{sa}\[2]{:}]) < \ldots < R(S\[\mathit{sa}\[n]{:}])\text{.}

예제1

  1. 예제 1

    입력
    6
    aadead
    
    예상 출력
    6 1 5 4 3 2