Problems > Rank Among the Suffixes

Rank Among the Suffixes

Difficulty: 2200 6000 ms 128 MB

strings suffix-array

Sort all suffixes of S lexicographically. Answer Q queries: what is the rank (1-based) of the suffix that starts at position i?

Input: the first line has S; the second line has Q; each of the next Q lines has a start position i.

Output: one line per query with the suffix's rank.

Constraints:

  • 1 ≤ |S| ≤ 105
  • 1 ≤ Q ≤ 105

Sample Tests

Input 1
banana
2
1
2
Output 1
4
3
Input 2
ab
1
2
Output 2
2

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.