https://codeforces.ml/contest/1428/problem/C
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 →→ AABBA →→ 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≤t≤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⋅1052⋅105.
Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
Example
input
Copy
3 AAA BABA AABBBABBBB
output
Copy
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 →→ BA. So, the answer is 22.
For the third test case, one optimal sequence of moves is AABBBABBBB →→ AABBBABB →→ AABBBB →→ ABBB →→ AB →→ (empty string). So, the answer is 00.
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e6+5;
const ll mod=998244353;
ll n,t,k,s;
ll a[maxn];
ll poww(ll a,ll b)
{
ll ans=1,base=a;
while(b)
{
if(b&1)
{
ans*=base;
ans%=mod;
}
base*=base;
base%=mod;
b>>=1;
}
return ans;
}
char x[maxn];
map<ll,ll>m;
int main()
{
cin>>t;
while(t--)
{
cin>>x;
n=strlen(x);
k=n;
ll s1=0,s2=0;
for(int i=0;i<n;i++)
{
if(x[i]=='A')
s1++;
else
{
if(s1)
{
s1--;
k-=2;
}
else
{
if(s2)
{
s2--;
k-=2;
}
else
s2++;
}
}
}
cout<<k<<endl;
}
return 0;
}

Zookeeper正在玩一个使用炸弹删除包含'A'和'B'的字符串的游戏。每次操作可以删除'AB'或'BB'子串。求解通过一系列操作后,字符串能缩短到的最短长度。
522

被折叠的 条评论
为什么被折叠?



