NOI '23 P5 - String

View as PDF

Submit solution

Points: 25 (partial)
Time limit: 2.0s
Memory limit: 512M

Problem types

Little Y is a college student who is currently doing researches related to strings. Little Y learned about the following definitions regarding strings:

  • Given a string s[1:n] of length n, we define its substring s[l:r](1lrn) as the new string obtained by selecting s[l],s[l+1],,s[r] in order and concatenating them.
  • Given a string s[1:n] of length n, we define its reversed result R(s) as the string obtained by concatenating s[n],s[n1],,s[1] in order, which is the string obtained by reversing the original string.
  • Given two strings a[1:n] and b[1:n] of equal length n, we define a to be lexicographically smaller than b if and only if there exists 1in such that for any 1j<i,a[j]=b[j], and a[i]<b[i].

After understanding the above definitions, Little Y came up with the following problem:

Given a string s[1:n] of length n, there are q queries, each query giving two parameters i and r. You need to find out how many values of l satisfy the following conditions:

  • 1lr.
  • s[i:i+l1] is lexicographically smaller than R(s[i+l:i+2l1]).

Little Y would like to ask for your help in solving this problem.

Input Specification

This problem has multiple test data sets.

The first line of the input contains two integers c and t, which represent the test case number and the number of test data sets. c=0 represents that this test case is a sample test.

Then, each set of test data is given as input in order. For each set of test data:

The first line of input contains two positive integers, n and q, which represent the length of the string and the number of queries respectively.

The second line of input contains a string s of length n that only consists of lowercase letters.

Then q lines follow, each containing two positive integers, i and r, representing a query. It is guaranteed that i+2r1n.

Output Specification

For each query of each set of test data, output a line containing an integer, representing the number of ls satisfying the requirements.

Sample Input 1

Copy
0 2
9 3
abacababa
1 4
2 4
3 3
9 3
abaabaaba
1 4
2 4
3 3

Sample Output 1

Copy
4
0
3
2
0
2

Explanation for Sample Output 1

For the first set of test data in the sample:

  • When l=1, s[i:i+l1]=a, R(s[i+l:i+l+l1])=b.
  • When l=2, s[i:i+l1]=ab, R(s[i+l:i+l+l1])=ca.
  • When l=3, s[i:i+l1]=aba, R(s[i+l:i+l+l1])=bac.
  • When l=4, s[i:i+l1]=abac, R(s[i+l:i+l+l1])=baba.

In all four cases, s[i:i+l1] is lexcicographically smaller than R(s[i+l:i+2l1]), so the answer is 4.

Additional Samples

Sample inputs and outputs can be found here.

  • Sample 2 (ex_string2.in and ex_string2.ans) corresponds to test case 5.
  • Sample 3 (ex_string3.in and ex_string3.ans).
  • Sample 4 (ex_string4.in and ex_string4.ans) corresponds to test cases 24-25.

Constraints

For all test data, it is guaranteed that: 1t5,1n105,1q105,1i+2r1n. The string s only consists of lowercase letters.

Test IDnqAdditional Constraints
155A
21010
32020
45050
5102102
6103103None
720002000
830003000
940004000
10 23333 23333A
115×1045×104
12 7500075000
139×1049×104
14105105
152333323333B
167500075000
179×1049×104
18105105
192333323333None
205×1045×104
21 75000 75000
22 9×104 9×104
239500095000
24105 105
25

Additional Constraint A: It is guaranteed that the input string only consists of a and b, and each character is uniformly chosen from a and b at random.

Additional Constraint B: It is guaranteed that every pair of adjacent characters in the input string are distinct.


Comments

There are no comments at the moment.