https://www.acmicpc.net/problem/3986
#include <iostream>
#include <algorithm>
#include <stack>
#include <string>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
int ret = 0;
while (n-- > 0) {
string s; cin >> s;
bool IsGood = true;
int count[2] = { 0,0 };
stack<char> stack;
if (s.length() % 2 == 1) continue;
for (int i = 0; i < s.length(); i++) {
count[s[i] - 'A']++;
if (!stack.empty()) {
if (stack.top() == s[i]) {
stack.pop();
continue;
}
else {
stack.push(s[i]);
}
}
else stack.push(s[i]);
}
if (!stack.empty()) IsGood = false;
if (count[0] % 2 != 0 || count[1] % 2 != 0) IsGood = false;
if (IsGood) ret++;
}
cout << ret << "\n";
return 0;
}