문제: https://www.acmicpc.net/problem/3986

풀이

스택을 이용해 풀이했다.
스택에 원소가 없으면 단어의 알파벳을 넣고, 있으면 현재 알파벳과 스택의 마지막 원소를 비교했다.
마지막 원소가 같으면 쌍이 지어지므로 stack의 원소를 pop했다.
원소가 다르면 알파벳을 stack에 append하고 다음 알파벳으로 넘어간다.
이 과정을 끝내고 stack에 원소가 있으면 쌍이 지어지지 않은 알파벳이 있는 것이므로 좋은 단어가 아니다.
좋은 단어일 경우만 카운트해 출력한다.

소스코드

import sys
input = sys.stdin.readline

n = int(input())
words = [input().strip() for _ in range(n)]
cnt = 0

for word in words:
    fixed_word = ""

    stack = []
    for alphabet in word:
        if not stack:
            stack.append(alphabet)
        else:
            if stack[-1] == alphabet:
                stack.pop()
            else:
                stack.append(alphabet)
    else:
        if not stack:
            cnt += 1

print(cnt)