#P7361. 「JZOI-1」拜神

「JZOI-1」拜神

Description

There are a total of nn gods that Xiao Xi wants to worship. Xiao Xi’s belief for each god can be represented by a lowercase letter. Putting them together forms a string ss indexed starting from 11.

Xiao Xi needs prayer words to worship the gods. A prayer word is defined as a non-empty contiguous substring of ss, and its length is the length of this substring. Since worshipping only requires Xiao Xi to be serious enough, a prayer word is considered valid as long as it appears at least twice.

Note: as long as the starting positions of the two occurrences are different, it is fine even if the two substrings overlap.

However, because her parents are also present, Xiao Xi’s prayer words may sometimes be interrupted, so only the string within the interval [l,r][l,r] (i.e., s[lr]s[l\dots r]) is valid. To be prepared, Xiao Xi will carefully prepare for all possible situations.

He will give qq queries. In each query, l,rl,r are given, and you need to ask for the length of the longest valid prayer word within the interval [l,r][l,r].

Input Format

The first line contains two positive integers n,qn,q.
The second line contains a string ss consisting of lowercase letters.
The next qq lines each contain two numbers l,rl,r.

Output Format

Output qq lines. Each line outputs a non-negative integer, representing the length of the longest valid prayer word. If there is no valid prayer word, output 00.

10 5
cdabababdc
3 7
2 8
5 10
6 10
1 4

3
4
2
1
0

Hint

Sample Explanation:

For the first query: the string in the interval is ababa\texttt{ababa}, where the substring aba\texttt{aba} appears twice, with length 33.
For the second query: the string in the interval is dababab\texttt{dababab}, where the substring abab\texttt{abab} appears twice, with length 44.
For the third query: the string in the interval is ababdc\texttt{ababdc}, where the substring ab\texttt{ab} appears twice, with length 22.
For the fourth query: the string in the interval is babdc\texttt{babdc}, where the substring b\texttt{b} appears twice, with length 11.
For the fifth query: the string in the interval is cdab\texttt{cdab}, and no substring appears at least twice, so the answer is 00.

Constraints:

For 5%5\% of the data, n,q50n,q\le50.
For 15%15\% of the data, n,q200n,q\le200.
For 30%30\% of the data, n,q2×103n,q\le2\times10^3.
For 40%40\% of the data, n,q5×103n,q\le5\times10^3.
For 65%65\% of the data, n,q2×104n,q\le2\times 10^4.
For another 5%5\% of the data, all characters are the same.
For 100%100\% of the data, 1n5×1041 \le n\le5\times10^4 and 1q1051 \le q \le 10^5.

Translated by ChatGPT 5