Fewest letters for a suffix array
Time limit2sMemory limit512 MB
Given a permutation that is a suffix array, find the minimum number of distinct letters needed for a string that realizes exactly this suffix array.
- Level
Hard8 of 10
- Topics
- String, Greedy, Implementation, Sorting
- Solved
- No attempts yet
Problem
The -th suffix of a string is the substring that starts at the -th letter of and runs to the end. Letters are numbered from 0. For example, if = "abcde", the 0th suffix is "abcde" and the 3rd suffix is "de".
The suffix array of lists the suffix numbers of in the order the suffixes appear after sorting all of them lexicographically. Sorting compares the suffixes themselves, not their numbers. For example, the suffix array of = "abca" is (3, 0, 1, 2).
Several strings can share one suffix array. The suffix array (3, 0, 1, 2) comes from "abca" and also from "aaba", and "aaba" uses only two distinct letters.
You are given a suffix array of length . Among all strings whose suffix array equals the given array, find the one that uses the fewest distinct letters and report how many that is. The alphabet is unlimited.
Input
The first line contains the length of the suffix array. ()
The second line contains the suffix array, separated by spaces. It contains every integer from 0 to exactly once.
Output
Print on one line the minimum number of distinct letters in a string whose suffix array is the given array.