Repetitions

아직 제출이 없습니다시간 제한10초메모리 제한512 MB

문제

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]\dots s\[n], answers qq queries of the form “given a_ia\_i and b_ib\_i, how long is the longest string tt for which tttt appears as a substring of s\[a_i]s\[a_i+1]s\[b_i1]s\[b_i]s\[a\_i]s\[a\_i + 1]\dots 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 a_ia\_i and b_ib\_i, separated by a space.

출력

Output qq lines; the ii-th of these lines must contain two space-separated integers _iℓ\_i and c_ic\_i. _iℓ\_i should be the length of the longest string tt for which tttt appears as a substring in s\[a_i]s\[a_i+1]s\[b_i1]s\[b_i]s\[a\_i ]s\[a\_i + 1] \dots s\[b\_i - 1]s\[b\_i ], and c_ic\_i should be the index at which the leftmost repetition of this length begins, i.e. the smallest integer such that a_ic_ia\_i ≤ c\_i, c_i+2_i1b_ic\_i + 2ℓ\_i - 1 ≤ b\_i and s\[c_i]s\[c_i+_i1]=s\[c_i+_i]s\[c_i+2_i1]s\[c\_i ] \dots s\[c\_i + ℓ\_i - 1] = s\[c\_i + ℓ\_i ] \dots s\[c\_i + 2ℓ\_i - 1]. (If _i=0ℓ\_i = 0, then c_i=a_ic\_i = a\_i by definition.)

제한

  • 1n1061 ≤ n ≤ 10^6
  • 1q1001 ≤ q ≤ 100
  • 1a_ib_in1 ≤ a\_i ≤ b\_i ≤ n for each i=1,2,,qi = 1, 2, \dots , q

힌트

The four queries in the above example refer to the substrings aabaa, cabaabaac, abaac, and aca; the part shown in bold is the substring referred to by the result of that query (a substring of length _iℓ\_i, beginning at index c_ic\_i). In the last query there is no repetition, so _4=0ℓ\_4 = 0.