CF1896B.AB Flipping

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

  • Choose an index 1≤i≤n−11 \le i \le n - 1 such that si=As_i = \texttt{A} and si+1=Bs_{i + 1} = \texttt{B}. Then, swap sis_i and si+1s_{i+1}.

You are only allowed to do the operation at most once for each index 1≤i≤n−11 \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.

给你一个长度为 nn 的字符串 ss,其中仅包含字符 A\texttt{A} 和 B\texttt{B}。你可以执行以下操作:

  • 选择一个下标 1≤i≤n−11 \le i \le n - 1,满足 si=As_i = \texttt{A} 且 si+1=Bs_{i + 1} = \texttt{B};然后交换 sis_i 和 si+1s_{i+1}。

对每个下标 1≤i≤n−11 \le i \le n - 1,你最多只能执行一次该操作。但你可以按任意顺序执行这些操作。求你能执行的操作的最大次数。

输入格式

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

The first line of each test case contains a single integer nn (2≤n≤2⋅1052 \le n \le 2\cdot 10^5) — the length of string ss.

The second line of each test case contains the string ss (si=As_i=\texttt{A} or si=Bs_i=\texttt{B}).

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤10001 \le t \le 1000)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2\cdot 10^5)—— 字符串 ss 的长度。

每个测试用例的第二行包含字符串 ss(其中每个字符 sis_i 为 A\texttt{A} 或 B\texttt{B})。

保证所有测试用例的 nn 之和不超过 2⋅1052\cdot 10^5。

输出格式

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

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

输入输出样例

  • 输入#1

    3
    2
    AB
    4
    BBBA
    4
    AABB

    输出#1

    1
    0
    3

说明/提示

In the first test case, we can do the operation exactly once for i=1i=1 as s1=As_1=\texttt{A} and s2=Bs_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=2i=2 to form ABAB\texttt{ABAB}, then another operation on i=3i=3 to form ABBA\texttt{ABBA}, and finally another operation on i=1i=1 to form BABA\texttt{BABA}. Note that even though at the end, s2=As_2 = \texttt{A} and s3=Bs_3 = \texttt{B}, we cannot do an operation on i=2i=2 again as we can only do the operation at most once for each index.

在第一个测试用例中,我们可以恰好对 i=1i=1 执行一次操作,因为 s1=As_1=\texttt{A} 且 s2=Bs_2=\texttt{B}。

在第二个测试用例中,可以证明无法执行任何操作。

在第三个测试用例中,我们首先对 i=2i=2 执行一次操作,得到 ABAB\texttt{ABAB};然后对 i=3i=3 执行一次操作,得到 ABBA\texttt{ABBA};最后对 i=1i=1 执行一次操作,得到 BABA\texttt{BABA}。注意:尽管最终 s2=As_2 = \texttt{A} 且 s3=Bs_3 = \texttt{B},但我们不能再对 i=2i=2 执行操作,因为每个下标最多只能执行一次操作。

输入解题思路,AI测评打分。不知道怎么写?

首页