310833: CF1896B. AB Flipping

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

B. AB Flippingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

You are given a string $s$ of length $n$ consisting of characters $\texttt{A}$ and $\texttt{B}$. You are allowed to do the following operation:

  • Choose an index $1 \le i \le n - 1$ such that $s_i = \texttt{A}$ and $s_{i + 1} = \texttt{B}$. Then, swap $s_i$ and $s_{i+1}$.

You are only allowed to do the operation at most once for each index $1 \le i \le n - 1$. However, you can do it in any order you want. Find the maximum number of operations that you can carry out.

Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). Description of the test cases follows.

The first line of each test case contains a single integer $n$ ($2 \le n \le 2\cdot 10^5$) — the length of string $s$.

The second line of each test case contains the string $s$ ($s_i=\texttt{A}$ or $s_i=\texttt{B}$).

It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.

Output

For each test case, print a single integer containing the maximum number of operations that you can carry out.

ExampleInput
3
2
AB
4
BBBA
4
AABB
Output
1
0
3
Note

In the first test case, we can do the operation exactly once for $i=1$ as $s_1=\texttt{A}$ and $s_2=\texttt{B}$.

In the second test case, it can be proven that it is not possible to do an operation.

In the third test case, we can do an operation on $i=2$ to form $\texttt{ABAB}$, then another operation on $i=3$ to form $\texttt{ABBA}$, and finally another operation on $i=1$ to form $\texttt{BABA}$. Note that even though at the end, $s_2 = \texttt{A}$ and $s_3 = \texttt{B}$, we cannot do an operation on $i=2$ again as we can only do the operation at most once for each index.

Output

题目大意:给定一个由字符 'A' 和 'B' 组成的字符串 s,长度为 n。你可以进行以下操作:

选择一个索引 i(1 ≤ i ≤ n - 1)使得 s_i = 'A' 并且 s_{i + 1} = 'B',然后交换 s_i 和 s_{i+1}。

对于每个索引 1 ≤ i ≤ n - 1,你最多只能进行一次这样的操作,并且你可以以任意顺序进行。求你能执行的最大操作次数。

输入数据格式:第一行包含一个整数 t(1 ≤ t ≤ 1000),表示测试用例的数量。接下来每个测试用例包含两行,第一行是一个整数 n(2 ≤ n ≤ 2·10^5),表示字符串 s 的长度。第二行是字符串 s(s_i = 'A' 或 s_i = 'B')。所有测试用例的 n 之和不超过 2·10^5。

输出数据格式:对于每个测试用例,输出一个整数,表示你能执行的最大操作次数。

示例:

输入:
```
3
2
AB
4
BBBA
4
AABB
```
输出:
```
1
0
3
```
注意:在第一个测试用例中,我们可以对 i=1 执行一次操作,因为 s_1 = 'A' 且 s_2 = 'B'。在第二个测试用例中,可以证明不可能进行操作。在第三个测试用例中,我们可以对 i=2 进行操作以形成 'ABAB',然后对 i=3 进行操作以形成 'ABBA',最后对 i=1 进行操作以形成 'BABA'。注意,即使最后 s_2 = 'A' 且 s_3 = 'B',我们也不能再次对 i=2 进行操作,因为对于每个索引我们最多只能进行一次操作。题目大意:给定一个由字符 'A' 和 'B' 组成的字符串 s,长度为 n。你可以进行以下操作: 选择一个索引 i(1 ≤ i ≤ n - 1)使得 s_i = 'A' 并且 s_{i + 1} = 'B',然后交换 s_i 和 s_{i+1}。 对于每个索引 1 ≤ i ≤ n - 1,你最多只能进行一次这样的操作,并且你可以以任意顺序进行。求你能执行的最大操作次数。 输入数据格式:第一行包含一个整数 t(1 ≤ t ≤ 1000),表示测试用例的数量。接下来每个测试用例包含两行,第一行是一个整数 n(2 ≤ n ≤ 2·10^5),表示字符串 s 的长度。第二行是字符串 s(s_i = 'A' 或 s_i = 'B')。所有测试用例的 n 之和不超过 2·10^5。 输出数据格式:对于每个测试用例,输出一个整数,表示你能执行的最大操作次数。 示例: 输入: ``` 3 2 AB 4 BBBA 4 AABB ``` 输出: ``` 1 0 3 ``` 注意:在第一个测试用例中,我们可以对 i=1 执行一次操作,因为 s_1 = 'A' 且 s_2 = 'B'。在第二个测试用例中,可以证明不可能进行操作。在第三个测试用例中,我们可以对 i=2 进行操作以形成 'ABAB',然后对 i=3 进行操作以形成 'ABBA',最后对 i=1 进行操作以形成 'BABA'。注意,即使最后 s_2 = 'A' 且 s_3 = 'B',我们也不能再次对 i=2 进行操作,因为对于每个索引我们最多只能进行一次操作。

加入题单

上一题 下一题 算法标签: