Logo
(追記) (追記ここまで)

30423번 - String 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB23114.348%

문제

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] (1 \leq l \leq r \leq n)$ as the new string obtained by selecting $s[l], s[l + 1], \dots , 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[n - 1], \dots , 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 1ドル \leq i \leq n$ such that for any 1ドル \leq j < 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:

  • 1ドル \leq l \leq r$.
  • $s[i : i + l - 1]$ is lexicographically smaller than $R(s[i + l : i + 2l - 1])$.

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

입력

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+2r -1 \leq n$.

출력

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

제한

For all test data, it is guaranteed that: 1ドル \leq t\leq 5,1\leq n\leq 10^5,1\leq q\leq 10^5,1\leq i+2r-1\leq n$. The string $s$ only consists of lowercase letters.

예제 입력 1

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

예제 출력 1

4
0
3
2
0
2

For the first set of test data in the sample:

  • When $l = 1,ドル $s[i:i+l-1] = \textsf{a},ドル $R(s[i+l:i+l+l-1]) = \textsf{b}$.
  • When $l = 2,ドル $s[i:i+l-1] = \textsf{ab},ドル $R(s[i+l:i+l+l-1]) = \textsf{ca}$.
  • When $l = 3,ドル $s[i:i+l-1] = \textsf{aba},ドル $R(s[i+l:i+l+l-1]) = \textsf{bac}$.
  • When $l = 4,ドル $s[i:i+l-1] = \textsf{abac},ドル $R(s[i+l:i+l+l-1]) = \textsf{baba}$.

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

힌트

추가 예제

출처

Olympiad > National Olympiad in Informatics (China) > NOI 2023 5번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /