#P13772. [CERC 2021] Repetitions

[CERC 2021] Repetitions

题目描述

Bob is an aspiring avant-garde writer. He disdains the use of spaces, punctuation, capital letters and the like; hence, his stories are nothing but long strings of lowercase letters of the English alphabet. Critics have also noted that his style is marked by a certain fondness for repetitions, in the sense that it sometimes happens that two instances of the same substring appear in his story twice in a row, without any other intervening characters.

Bob has submitted his latest masterpiece, a string which happens to be nn characters long, to qq different literary magazines in the hopes that at least one of them might be willing to publish it. The response was more favourable than he had dared to hope. The editors of all qq magazines have expressed willingness to publish some part (i.e. a substring) of his story, but on the condition that he identify the longest repetition (i.e. a shorter substring appearing twice in a row) within that part of the story. The editors intend to remove that part to prevent the story from being too boring. Now Bob needs your help to answer these queries from the editors.

Write a program that, given a string of nn letters, s[1]s[2]s[n]s[1]s[2] \ldots s[n], answers qq queries of the form "given aia_i and bib_i, how long is the longest string tt for which tttt appears as a substring of s[ai]s[ai+1]s[bi1]s[bi]s[a_i]s[a_i + 1] \ldots s[b_i - 1]s[b_i], and where does the leftmost such occurrence begin?"

输入格式

The first line contains two integers, nn and qq. The second line contains the string ss, which is nn characters long; all these characters are lowercase letters of the English alphabet. The remaining qq lines describe the queries; the ii-th of these lines contains the integers aia_i and bib_i, separated by a space.

输出格式

Output qq lines; the ii-th of these lines must contain two space-separated integers i\ell_i and cic_i. i\ell_i should be the length of the longest string tt for which tttt appears as a substring in s[ai]s[ai+1]s[bi1]s[bi]s[a_i]s[a_i + 1] \ldots s[b_i - 1]s[b_i], and cic_i should be the index at which the leftmost repetition of this length begins, i.e. the smallest integer such that aicia_i \leq c_i, ci+2i1bic_i + 2\ell_i - 1 \leq b_i and $s[c_i] \ldots s[c_i + \ell_i - 1] = s[c_i + \ell_i] \ldots s[c_i + 2\ell_i - 1]$. (If i=0\ell_i = 0, then ci=aic_i = a_i by definition.)

10 4
cabaabaaca
4 8
1 9
5 9
8 10
1 4
3 2
1 7
0 8

提示

Comment

The four queries in the above example refer to the substrings aabaa\textbf{\underline{a}abaa}, cabaabaac\textbf{c\underline{aba}abaac}, abaac\textbf{ab\underline{a}ac}, and aca\textbf{aca}; the part shown in bold is the substring referred to by the result of that query (a substring of length i\ell_i, beginning at index cic_i). In the last query there is no repetition, so 4=0\ell_4 = 0.

Input limits

  • 1n1061 \leq n \leq 10^6
  • 1q1001 \leq q \leq 100
  • 1aibin1 \leq a_i \leq b_i \leq n for each i=1,2,,qi = 1, 2, \ldots, q