Codeforces Global Round 30 (Div. 1 + Div. 2)
H
PalindromePalindrome
Codeforces
3400
原题
← G→
翻译中…

We define the humor value of a string $t$ as the length of the longest palindrome string which occurs at least twice in it.

翻译中…

Formally, a string $p$ is humor with respect to $t$, if and only if both of the following conditions are met:

翻译中…
  1. $p$ is a palindrome, and
  2. there exist at least two different indices $i \in [1, |t| - |p| + 1]$, such that $t[i, i + |p| - 1] = p$.
翻译中…

The humor value of $t$ is defined as the maximum length among all humor strings with respect to $t$.

翻译中…

You are given a string $s$ of length $n$ which only contains lowercase Latin letters and $q$ queries. Each query contains two integers $l_i$, $r_i$, and you need to find the humor value of the string $s[l_i, r_i]$.

翻译中…

Here, $a[l, r]$ is defined as the string $a_l,a_{l+1},\ldots,a_r$.

翻译中…

Input

翻译中…

The first line contains two integers $n$, $q$ ($1\le n,q\le 5\cdot10^5$).

翻译中…

The second line contains a string $s$.

翻译中…

The next $q$ lines contain the description of queries. The $i$-th line contains two integers $l_i$, $r_i$ ($1\le l_i\le r_i\le n$).

翻译中…

Output

翻译中…

On the $i$-th line, output the answer to the $i$-th query.

翻译中…

Note

翻译中…

In the first query, the following palindrome strings occur at least twice: a,aa,aaa,aaaa,b,bb,bbb. The longest one is aaaa, so the humor value is $4$.

翻译中…

In the second query, one of the longest humor strings with respect to $s[2, 7]$ is aa.

翻译中…

样例

Input #1
12 6
aaaabbbbaaaa
1 12
2 7
3 10
4 7
5 9
4 5
Output #1
4
2
3
2
3
0
InputCopy OutputCopy
题目信息
题号
2164H
时间限制
5 s
内存限制
512 MB
标签
数据结构
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。