#include <stack>
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
int n, cnt = 0;
cin >> n;
while (n--) {
stack<char> st;
cin >> str;
for (char c: str) {
if (!st.empty()) {
if (st.top() == c) {
st.pop();
continue;
}
}
st.push(c);
}
if (st.empty()) cnt++;
}
cout << cnt << endl;
}
스택의 대표문제일 괄호문제와 매우 유사하다. 스택의 top과 그 다음 문자가 동일하면 스택을 비워주고 그렇지 않다면 push 해준다.
최종적으로 스택이 비어있으면 이는 좋은 단어인 것이다.