CF1896B.AB Flipping
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a string s of length n consisting of characters A and B . You are allowed to do the following operation:
- Choose an index 1≤i≤n−1 such that si=A and si+1=B . Then, swap si and si+1 .
You are only allowed to do the operation at most once for each index 1≤i≤n−1 . However, you can do it in any order you want. Find the maximum number of operations that you can carry out.
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t ( 1≤t≤1000 ). Description of the test cases follows.
The first line of each test case contains a single integer n ( 2≤n≤2⋅105 ) — the length of string s .
The second line of each test case contains the string s ( si=A or si=B ).
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 .
输出格式
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=1 as s1=A and s2=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 ABAB , then another operation on i=3 to form ABBA , and finally another operation on i=1 to form BABA . Note that even though at the end, s2=A and s3=B , we cannot do an operation on i=2 again as we can only do the operation at most once for each index.