CodeForces - 1428C ABBB

本文介绍了解决 CodeForces 1428C 问题的方法,通过使用栈来模拟游戏过程,实现对字符串中特定模式的消除,最终确定最短可能的字符串长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

codeforces - 1428c ABBB
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters ‘A’ and ‘B’. He can use bombs to bomb a substring which is either “AB” or “BB”. When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.

For example, Zookeeper can use two such operations: AABABBA \to→ AABBA \to→ AAA.

Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?

Input
Each test contains multiple test cases. The first line contains a single integer tt (1 \leq t \leq 20000)(1≤t≤20000) — the number of test cases. The description of the test cases follows.

Each of the next tt lines contains a single test case each, consisting of a non-empty string ss: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of ss are either ‘A’ or ‘B’.

It is guaranteed that the sum of |s|∣s∣ (length of ss) among all test cases does not exceed 2 \cdot 10^52⋅10
5
.

Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.

Sample 1
Inputcopy Outputcopy
3
AAA
BABA
AABBBABBBB
3
2
0
Note
For the first test case, you can’t make any moves, so the answer is 33.

For the second test case, one optimal sequence of moves is BABA \to→ BA. So, the answer is 22.

For the third test case, one optimal sequence of moves is AABBBABBBB \to→ AABBBABB \to→ AABBBB \to→ ABBB \to→ AB \to→ (empty string). So, the answer is 00.
消消乐写法

#include<bits/stdc++.h>
using namespace std;
void solve(){
    stack<char>s;
    string a;
    cin>>a;
    for(int i=0;i<a.length();i++){
        if(s.empty()){
            s.push(a[i]);
        }else{
            if(a[i]=='B')s.pop();
            else s.push(a[i]);
        }
    }
    cout<<s.size()<<endl;
}
int main(void){
    int k;
    cin>>k;
    while(k--){
        solve();
    }
}
//code by 01100_10111;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值