Problem Solving/알고리즘

[백준][파이썬] 1662 - 압축

Supersett 2022. 7. 13. 13:27

배열에는 튜플도 추가할 수 있다! 애용하자

stack.append((temp,length-1))
length=0
multi,preL=stack.pop()

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import sys
 
s=sys.stdin.readline().rstrip()
stack=[]
length=0
temp=""
for c in s:
    if c.isdigit():
        length+=1
        temp=c
    elif c=="(":
        stack.append((temp,length-1))
        length=0
    else:
        multi,preL=stack.pop()
        length=(int(multi)*length)+preL
print(length)
cs