본문 바로가기

알고리즘 문제풀이

좋은 단어[백준 3986]

 

 

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

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)

'알고리즘 문제풀이' 카테고리의 다른 글

반지[백준 5555]  (0) 2024.06.29
압축[백준 1662]  (0) 2024.06.26
탑[백준 2493]  (0) 2024.06.24
소수&팰린드롬[백준 1747]  (0) 2024.05.30
물병[백준 1052]  (0) 2024.05.27