Delete AAB or BAA

AtCoder
IDagc066_c
Time2000ms
Memory256MB
Difficulty
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$ [samples]
Samples
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)
API Response (JSON)
{
  "problem": {
    "name": "Delete AAB or BAA",
    "description": {
      "content": "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 `",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "agc066_c"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a string $S$ consisting of characters `A` and `B`.\nOn this string, you can repeatedly perform the following operation:\n\n*   Choose three consecutive characters in $S$ that are equal to `...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments