#P15146. [SWERC 2025] LFS

[SWERC 2025] LFS

题目描述

You are designing the new videogame Live Fight Simulator. A level is defined by a string ss of length nn consisting of lowercase English letters, where each letter represents a type of enemy that appears in sequence.

To analyze the gameplay balance, you need to measure how repetitive different parts of the level are. You will consider qq specific contiguous segments s[l,r]s[l, r] of the level, with 1lrn1 \le l \le r \le n.

For each of these queries, you want to compute the length of the LFS (Longest Frequent Substring). Formally, for any string tt:

  • let f(t)f(t) be the maximum frequency of any substring in tt;
  • let LFS(t)|\text{LFS}(t)| be the maximum length of a substring of tt that appears exactly f(t)f(t) times.

For each query (l,r)(l, r), you must compute LFS(s[l,r])|\text{LFS}(s[l, r])|, which represents the maximum length among the most repetitive patterns of enemy spawns in that part of the level.

A string xx is a substring of a string yy if xx can be obtained from yy by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

输入格式

The first line contains two integers n,qn, q (1n51051 \le n \le 5 \cdot 10^5, 1q51051 \le q \le 5 \cdot 10^5) — the length of the sequence and the number of level segments to analyze.

The second line contains a string ss of length nn consisting of lowercase English letters.

Each of the next qq lines contains two integers l,rl, r (1lrn1 \le l \le r \le n), representing a query on the substring s[l,r]s[l, r].

输出格式

Print qq lines. The ii-th line must contain a single integer: the value of LFS(s[l,r])|\text{LFS}(s[l, r])| for the pair (l,r)(l, r).

5 4
ababa
1 1
1 5
1 4
2 5
1
1
2
2
10 1
aaaaaaaaaa
1 10
1
17 5
deabcabcabdeadede
1 8
1 10
4 9
2 16
1 17
3
2
3
1
2

提示

Explanation of sample 1.

In the first example:

  • In the first query, the substring is t=s[1,1]="a"t = s[1, 1] = \text{"a"}. The maximum frequency of a substring inside "a"\text{"a"} is 1, reached by "a"\text{"a"} itself, whose length is 1. Therefore, the answer is 1.
  • In the second query, the substring is t=s[1,5]="ababa"t = s[1, 5] = \text{"ababa"}. The maximum frequency of a substring inside "ababa"\text{"ababa"} is 3, reached by "a"\text{"a"}, whose length is 1. Therefore, the answer is 1.
  • In the third query, the substring is t=s[1,4]="abab"t = s[1, 4] = \text{"abab"}. The maximum frequency of a substring inside "abab"\text{"abab"} is 2, reached by "a"\text{"a"}, "b"\text{"b"} and "ab"\text{"ab"}. Among these strings, the one with maximum length is "ab"\text{"ab"}, whose length is 2. Therefore, the answer is 2.
  • In the fourth query, the substring is t=s[2,5]="baba"t = s[2, 5] = \text{"baba"}. The maximum frequency of a substring inside "baba"\text{"baba"} is 2, reached by "a"\text{"a"}, "b"\text{"b"} and "ba"\text{"ba"}. Among these strings, the one with maximum length is "ba"\text{"ba"}, whose length is 2. Therefore, the answer is 2.