luogu#P4482. [BJWC2018] Border 的四种求法

[BJWC2018] Border 的四种求法

Background

As soon as Scape walked into the computer lab, everyone solving problems looked at him and laughed. Someone shouted, "Scape, you must have been crushed by the standard score again!" He did not answer; he said, "Run two programs, check the score sheet," and then he copied out two programs. They deliberately shouted again, "How did you get WA on both the Euler circuit and inversions?" ...

Problem Description

Scape knows that the story above is just an accident in his OI career. To prove himself, he decides to teach you four methods to compute Border\text{Border}.

Given a lowercase-letter string SS, there are qq queries. Each query gives l,rl, r, and asks for the Border\text{Border} of slrs_{l\ldots r}.

Border\text{Border}: For a given string ss, the largest ii such that s1i=ssi+1ss_{1\ldots i} = s_{|s|-i+1\ldots |s|}. Here s|s| is the length of ss.

Input Format

The first line contains a string SS.

The second line contains an integer qq denoting the number of queries.

Each of the next qq lines contains two integers l,rl, r denoting a query.

Output Format

For each query, output the answer.

abbabbaa
3
1 8
1 7
2 7
1
4
3

Hint

  • For 30%30\% of the testdata, n,q1000n, q \le 1000.
  • For 50%50\% of the testdata, n,q2×104n, q \le 2\times 10^4.
  • For another 30%30\% of the testdata, the answer is at least half of rl+1r - l + 1.
  • For 100%100\% of the testdata, n,q2×105n, q \le 2\times 10^5.

Translated by ChatGPT 5