NHN_백준_1662 (압축_stack 스택)

RostoryT·2022년 5월 28일
0

Corporation_Coding Test

목록 보기
4/19
post-thumbnail


  • 문제가 무슨 말인지 이해를 못해서 구글링한 내용

입력 모음

33(562(71(9)))
123
10342(76)
0(0)
1(1(1(1(1(1(1(0(1234567890))))))))
1()66(5)

''' 블로그 솔루션 '''
''' => 앞에서부터 진행해서 length - 1은 더해주고, '''
def sol(s):
    # 시간 절약을 위해
    if '(' not in s:
        print(len(s))
        return
    
    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:
            num, pre_len = stack.pop()
            length = (int(num) * length) + pre_len
            
    
    return length

sol(input())

profile
Do My Best

0개의 댓글