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?
输入格式
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’.
输出格式
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
题意翻译
有一个只包含’A’与’B’的字符串,每次可以消掉一个"AB"或一个"BB",并把剩下的拼在一起,求字符串最短的长度。
输入输出样例
输入
3
AAA
BABA
AABBBABBBB
输出
3
2
0
这个题简单的说,只要有BB和AB就可以消,那么只要出现一个B,肯定能消去它前面的字母,不用考虑组合的问题,只要有B出现肯定能消除东西。顺序在这个题里没有用。
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
int main()
{
int testcase;
cin >> testcase;
while (testcase--) {
string temp;
cin >> temp;
int ans = 0;
for (int i = 0; i < temp.size(); i++) {
if (temp[i] == 'B'&&ans != 0) {
ans--;
}
else {
ans++;
}
}
cout << ans << endl;
}
return 0;
}
return code;