AtCoder Grand Contest 066
C
Delete AAB or BAA
AtCoder
⌀ 3036
原题
← BD →
翻译中…

Problem Statement

翻译中…

You are given a string $S$ consisting of characters A and B.

翻译中…

On this string, you can repeatedly perform the following operation:

翻译中…
  • Choose three consecutive characters in $S$ that are equal to AAB or BAA, and delete those three characters from $S$ (after deletion, the remaining characters are concatenated).
翻译中…

Find the maximum number of times you can perform this operation.

翻译中…

$T$ test cases are given; solve each of them.

翻译中…

Constraints

翻译中…
  • $1\leq T\leq 10^5$
  • $S$ is a string consisting of A and B.
  • $1\leq |S|\leq 10^6$
  • The sum of $|S|$ over all test cases in a single input is at most $10^6$.
翻译中…

Input

翻译中…

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

翻译中…

T
\text{case}_1
\vdots
\text{case}_T

翻译中…

Each test case is given in the following format:

翻译中…

S

翻译中…

Output

翻译中…

Print $T$ lines. The $i$-th line should contain the maximum number of operations that can be performed for the $i$-th test case.

翻译中…

样例

Input #1
10
AABAAAB
BAAAAABBA
A
B
ABA
BAA
AAAAAA
AAAABB
AABABBAABBABAAAABBAA
BBAAAAABAAAAABABAABA
Output #1
2
3
0
0
0
1
0
2
5
6
For each of the first and second test cases, here is a possible way to perform the maximum number of operations: - `AABAAAB` → `AAAB` → `A` - `BAAAAABBA` → `BAAABA` → `BAA` → (empty string)
题目信息
题号
agc066_c
时间限制
2 s
内存限制
1024 MB

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