백준

백준 1662 압축(python)

콩순이냉장고 2025. 1. 1. 23:42

문제 URL : https://www.acmicpc.net/problem/1662

 

 

 

문제 접근법 : 

스택 사용문제입니다.

먼저봤던것들을 (,)가아니면 cnt값을 증가해주고

(라면 cnt를 0으로 초기화해주기전 이전의값이랑 cnt-1을 저장해줍니다.

)라면 스택에 저장되어있는값들을 곱하고 더해줍니다.

소스코드:

 

 

from collections import Counter
s = input()
cnt = 0
st = []
before =0
for c in s:
    if c=='(':
        st.append((cnt-1,before))
        cnt=0
    elif c==')':
        cnt =cnt*st[-1][1]+st[-1][0]
        st.pop()
    else:
        cnt+=1
        before = int(c)
print(cnt)

 

궁금한점 혹은 모르는점 어떤질문이든 댓글은 언제나환영입니다.