#x1008. CF1043G Speckled Band
CF1043G Speckled Band
Speckled Band
题面翻译
给你一个长为 ()的小写字母字符串 ,再给 ()个询问,每次给一个区间 ,你需要把 这个子串划分成若干段,使得至少有两个段是相同的,你需要构造一种分段方式使得划分出的段中本质不同的段最少,你只要求这个最小值。
题目描述
Ildar took a band (a thin strip of cloth) and colored it. Formally, the band has cells, each of them is colored into one of colors, so we can denote each color with one of the lowercase letters of English alphabet.
Ildar decided to take some segment of the band ( ) he likes and cut it from the band. So he will create a new band that can be represented as a string .
After that Ildar will play the following game: he cuts the band into some new bands and counts the number of different bands among them. Formally, Ildar chooses indexes and cuts to bands-strings $ t_1 t_2 \ldots t_{i_1}, t_{i_1 + 1} \ldots t_{i_2}, \ldots, {t_{i_{k-1} + 1}} \ldots t_{i_k} $ and counts the number of different bands among them. He wants to know the minimal possible number of different bands he can get under the constraint that at least one band repeats at least two times. The result of the game is this number. If it is impossible to cut in such a way, the result of the game is -1.
Unfortunately Ildar hasn't yet decided which segment he likes, but he has segments-candidates , , ..., . Your task is to calculate the result of the game for each of them.
输入格式
The first line contains one integer ( ) — the length of the band Ildar has.
The second line contains a string consisting of lowercase English letters — the band Ildar has.
The third line contains a single integer ( ) — the number of segments Ildar has chosen as candidates.
Each of the next lines contains two integer integers and ( ) denoting the ends of the -th segment.
输出格式
Output lines, where the -th of them should contain the result of the game on the segment .
样例 #1
样例输入 #1
9
abcabcdce
7
1 6
4 7
5 9
6 9
1 9
3 6
4 4
样例输出 #1
1
-1
4
3
2
2
-1
提示
Consider the first example.
If Ildar chooses the segment , he cuts a string . If he cuts into two bands and , the band repeats two times and the number of different tapes is . So, the result of this game is .
If Ildar chooses the segment , he cuts a string . It is impossible to cut this band in such a way that there is at least one band repeating at least two times. So, the result of this game is .
If Ildar chooses the segment , he cuts a string . If he cuts into three bands , and , the band repeats two times and the number of different bands is . So, the result of this game is .