알고리즘 문제풀이

좋은 단어[백준 3986]

wiojfe 2024. 6. 24. 18:34

 

 

스택을 사용하는 대표적인 문제이다. 

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

 

 

n =int(input())

cnt = 0 
for _ in range(n):
    stack = []
    word = input().rstrip()
    if len(word) %2== 1 :
        cnt += 0 
        continue 
    
    for i in range(len(word)) :
        if len(stack)==0 :
            stack.append(word[i]) 
            continue
        
        if stack and stack[-1] == word[i] :
            stack.pop() 
        else:
            stack.append(word[i]) 
    if len(stack)==0 :
        cnt += 1 
    
print(cnt)