CF1873G.ABBC or BACB

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a string ss made up of characters A\texttt{A} and B\texttt{B}. Initially you have no coins. You can perform two types of operations:

  • Pick a substring†^\dagger AB\texttt{AB}, change it to BC\texttt{BC}, and get a coin.
  • Pick a substring†^\dagger BA\texttt{BA}, change it to CB\texttt{CB}, and get a coin.

What is the most number of coins you can obtain?

†^\dagger A substring of length 22 is a sequence of two adjacent characters of a string.

给你一个仅由字符 A 和 B 构成的字符串 ss。初始时你没有硬币。你可以执行以下两种操作:

  • 选取一个子串†^\dagger AB\texttt{AB},将其改为 BC\texttt{BC},并获得一枚硬币;
  • 选取一个子串†^\dagger BA\texttt{BA},将其改为 CB\texttt{CB},并获得一枚硬币。

你最多能获得多少枚硬币?

†^\dagger 长度为 22 的子串是指字符串中两个相邻的字符所组成的序列。

输入格式

The input consists of multiple test cases. The first line of the input contains a single integer tt (1≤t≤10001 \leq t \leq 1000) — the number of test cases.

The only line of each test case contains the string ss (1≤∣s∣≤2⋅1051 \leq |s| \leq 2 \cdot 10^5). All characters of ss are either A\texttt{A} or B\texttt{B}.

The sum of the lengths of ss over all test cases does not exceed 2⋅1052 \cdot 10^5.

输入包含多个测试用例。输入的第一行包含一个整数 tt(1≤t≤10001 \leq t \leq 1000),表示测试用例的数量。

每个测试用例仅有一行,包含字符串 ss(1≤∣s∣≤2⋅1051 \leq |s| \leq 2 \cdot 10^5)。字符串 ss 中的所有字符均为 A\texttt{A} 或 B\texttt{B}。

所有测试用例中字符串 ss 的长度总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output a single integer — the maximum number of coins you can obtain.

对于每个测试用例,输出一个整数——你能获得的最多硬币数量。

输入输出样例

  • 输入#1

    8
    ABBA
    ABA
    BAABA
    ABB
    AAAAAAB
    BABA
    B
    AAA

    输出#1

    2
    1
    3
    1
    6
    2
    0
    0

说明/提示

In the first test case you can perform the following operations to get 22 coins: $$\color{red}{\texttt{AB}}\texttt{BA} \to \texttt{BC}\color{red}{\texttt{BA}} \to \texttt{BCCB}$$

In the second test case you can perform the following operation to get 11 coin: $$\color{red}{\texttt{AB}}\texttt{A} \to \texttt{BCA}$$

In the third test case you can perform the following operations to get 33 coins: $$\color{red}{\texttt{BA}}\texttt{ABA} \to \texttt{CBA}\color{red}{\texttt{BA}} \to \texttt{C}\color{red}{\texttt{BA}}\texttt{CB} \to \texttt{CCBCB}$$

在第一个测试用例中,你可以执行以下操作来获得 22 枚硬币:

ABBA→BCBA→BCCB\color{red}{\texttt{AB}}\texttt{BA} \to \texttt{BC}\color{red}{\texttt{BA}} \to \texttt{BCCB}

在第二个测试用例中,你可以执行以下操作来获得 11 枚硬币:

ABA→BCA\color{red}{\texttt{AB}}\texttt{A} \to \texttt{BCA}

在第三个测试用例中,你可以执行以下操作来获得 33 枚硬币:

BAABA→CBABA→CBACB→CCBCB\color{red}{\texttt{BA}}\texttt{ABA} \to \texttt{CBA}\color{red}{\texttt{BA}} \to \texttt{C}\color{red}{\texttt{BA}}\texttt{CB} \to \texttt{CCBCB}

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

首页