QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#11567. Reversal ABC

統計

给定一个仅由字符 $\texttt{A}$、$\texttt{B}$ 和 $\texttt{C}$ 组成的字符串 $s$。

在一次操作中,你可以选择字符串中两个相邻的元素 $s_is_{i+1}$,前提是它们的顺序是以下之一:$\texttt{AB}$、$\texttt{BC}$ 或 $\texttt{CA}$,并交换它们。

请找出字符串 $s$ 上最多可以连续进行多少次这样的操作。

本题中,每组测试包含若干组输入数据。你需要分别独立地解决每组输入数据。

输入

第一行包含一个整数 $T$ $(1\le T\le 10^5)$~--- 表示输入数据的组数。接下来是每组输入数据的描述。

每组输入数据的第一行包含一个整数 $n$ $(1 \le n \le 10^6)$~--- 字符串 $s$ 的长度。

每组输入数据的第二行包含一个长度为 $n$ 的字符串 $s$,由字符 $\texttt{A}$、$\texttt{B}$ 和 $\texttt{C}$ 组成。

保证每组测试中所有输入数据的 $n$ 之和不超过 $10^6$。

输出

对于每组输入数据,在单独的一行输出一个整数~--- 字符串 $s$ 上最多可以连续进行的操作次数。

示例

输入

2
5
ABCCA
19
CCAABBBABBAAABBCCAA

输出

3
28

说明

在第一个样例的第一组输入中,字符串 $\texttt{ABCCA}$ 最多只能进行 $3$ 次连续操作。以下是一个能进行 $3$ 次操作的例子:[$\textbf{AB}\texttt{CCA}$ $\rightarrow$ $\texttt{BACCA}$, $\texttt{BAC}\textbf{CA}$ $\rightarrow$ $\texttt{BACAC}$, $\texttt{BA}\textbf{CA}\texttt{C}$ $\rightarrow$ $\texttt{BAACC}$]。

评分

设 $K$ 是一组测试中所有输入数据的 $n$ 之和。

  1. ($2$ 分):答案等于 $0$ 或 $1$;
  2. ($3$ 分):$n \le 3$;
  3. ($5$ 分):对所有 $1 \le i \le n$,有 $s_i \ne \texttt{C}$;
  4. ($5$ 分):$s$ 的形式为 $\texttt{AA}\ldots \texttt{AABB}\ldots \texttt{BBCC}\ldots \texttt{CC}$(即由若干个 $\texttt{A}$、若干个 $\texttt{B}$ 和若干个 $\texttt{C}$ 拼接而成);
  5. ($9$ 分):对所有 $1 \le i < n$,有 $s_is_{i+1} \ne \texttt{CA}$;
  6. ($10$ 分):$T = 1$ 且 $n \le 12$;
  7. ($8$ 分):$n \le 12$;
  8. ($28$ 分):$K \le 2000$;
  9. ($30$ 分):无额外限制。
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.