题目:
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?
https://codeforces.com/contest/1428/problem/C
题意:
给出一个字符串,相邻的AB可以消掉,相邻的BB可以消掉,问最少可以剩下多少个字母。
思路:
直接字符串模拟就行了。不过应该优先消除AB,再消除BB。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN = 200020;
int t;
long long int num = 0;
int vis[MAXN];
int b[MAXN];
int main()
{
int t;
cin >> t;
while (t--)
{
string s;
cin >> s;
for (int i = 0; i < s.size() - 1 && s.size(); i++)
{
if (s[i] == 'A' && s[i + 1] == 'B')
{
s.erase(s.begin() + i, s.begin() + i + 2);
i -= 2;
if (i == -2)
i += 1;
}
else if (s[i] == 'B' && s[i + 1] == 'B')
{
s.erase(s.begin() + i, s.begin() + i + 2);
i -= 2;
if (i == -2)
i += 1;
}
}
if (t > 0)
cout << s.size() << '\n';
else
cout << s.size();
}
return 0;
}