AtCoder Regular Contest-- 224
E
ABC|AB|A
AtCoder
⌀ 1352
原题
← DF →

ABC|AB|AAI (CI翻译)

Problem Statement

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

You can perform the following operation zero or more times.

  • Choose a substring (contiguous subsequence) of $S$ that is A, AB, or ABC, and delete it. The remaining characters are concatenated, preserving their order.

Find the minimum possible length of $S$ in the end.

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

Constraints

  • $T$ is an integer between $1$ and $10^5$, inclusive.
  • $S$ is a string consisting of A, B, and C with length between $1$ and $10^6$, inclusive.
  • The sum of $|S|$ in each input is at most $10^6$.

Input

The input is given from Standard Input in the following format, where $\mathrm{case}_i$ denotes the $i$-th test case:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

S

Output

Output $T$ lines. The $i$-th line should contain the answer for the $i$-th test case.

{"titleZh":"ABC|AB|A","bodyZh":"## 题目描述\n\n给定一个仅由 A、B 和 C 组成的字符串 $S$。\n\n你可以执行以下操作零次或多次:\n\n- 选择 $S$ 的一个子串(连续子序列),该子串为 A、AB 或 ABC,并将其删除。剩余字符按原顺序拼接起来。\n\n求最终 $S$ 的长度最小可能是多少。\n\n给定 $T$ 组测试数据,请分别求解。\n\n## 约束条件\n\n- $T$ 是一个介于 $1$ 和 $10^5$ 之间(含)的整数。\n- $S$ 是一个仅由 A、B 和 C 组成的字符串,长度介于 $1$ 和 $10^6$ 之间(含)。\n- 每个输入中所有 $|S|$ 之和不超过 $10^6$。\n\n## 输入格式\n\n输入从标准输入中按以下格式给出,其中 $\mathrm{case}_i$ 表示第 $i$ 组测试数据:\n\n\nT\n\\mathrm{case}_1\n\\mathrm{case}_2\n\\vdots\n\\mathrm{case}_T\n`\n\n每组测试数据的格式如下:\n\n`\nS\n\n\n## 输出格式\n\n输出 $T$ 行。第 $i$ 行应包含第 $i$ 组测试数据的答案。"}AI (CI翻译)

样例

Input #1
5
CAABCB
B
AAABBC
AABBCC
CABCCBCBAABCBBBAABAABACABBC
Output #1
1
1
0
1
7
This input contains five test cases. For the first test case, we can perform the operations as follows, for example. - Choose and delete the third through fifth characters of $S=$ `CAABCB`. After the operation, $S$ becomes `CAB`. - Choose and delete the second through third characters of $S=$ `CAB`. After the operation, $S$ becomes `C`. - No further operations can be performed. $S=$ `C` has length $1$, which is the minimum achievable.
题目信息
题号
arc224_e
时间限制
2 s
内存限制
1024 MB

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