AtCoder Grand Contest 074
E
Delete AB
AtCoder
未评级
原题
← D→
翻译中…

Problem Statement

翻译中…

For a string $X = X_1X_2\dots X_{|X|}$ and positive integers $l,r~(1 \leq l \leq r \leq |X|)$, let $X_{l,r} = X_l X_{l+1}\dots X_r$.

翻译中…

You are given a string $S=S_1S_2\dots S_{|S|}$ consisting of A and B. You are given $Q$ queries. For each query, answer the following question:

翻译中…
  • Starting from the state $T = S_{l,r}$, find the minimum possible length of a palindrome that can be obtained by performing the operation "choose one occurrence of AB that appears as a contiguous substring in $T$ and delete it" zero or more times. If no palindrome can be obtained regardless of how the operation is performed, output -1.
翻译中…

Constraints

翻译中…
  • $1 \leq |S| \leq 4 \times 10^5$
  • $1 \leq Q \leq 5 \times 10^4$
  • $1 \leq l \leq r \leq |S|$
  • $S$ is a string consisting of A and B.
  • $Q,l,r$ are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

S
Q
\mathrm{Query}_1
\mathrm{Query}_2
\vdots
\mathrm{Query}_Q

翻译中…

$\mathrm{Query}_q$ represents the $q$-th query and is given in the following format:

翻译中…

l r

翻译中…

Output

翻译中…

Output $Q$ lines. The $q$-th line should contain the answer for $\mathrm{Query}_q$.

翻译中…

样例

Input #1
ABBABB
5
1 6
4 5
3 5
2 3
3 4
Output #1
2
0
1
2
-1
For the first, second, and third queries, by performing the operation as follows, you can obtain a palindrome with the minimum length among those obtained by the operation. - `ABBABB` $\rightarrow$ `ABBB` $\rightarrow$ `BB` - `AB` $\rightarrow$ (empty string) - `BAB` $\rightarrow$ `B`
题目信息
题号
agc074_e
时间限制
4 s
内存限制
1024 MB

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