티스토리 뷰

# 2504 괄호의 값
str = input()

# 맞는지 검증
stack = []
for index, char in enumerate(str):
    if char == '(':
        stack.append('(')
    elif char == '[':
        stack.append('[')
    elif char == ')':
        if len(stack) > 0:
            ret = stack.pop()
            if ret != '(':
                print("0")
                exit(0)
        else:
            print("0")
            exit(0)
    elif char == ']':
        if len(stack) > 0:
            ret = stack.pop()
            if ret != '[':
                print("0")
                exit(0)
        else:
            print("0")
            exit(0)

if len(stack) !=0:
    print("0")
    exit(0)

# count 계산하기
out = ""
index = 0
char = ""
while True:
    if index >= len(str):
        # print(out)
        print(eval(out))
        exit(0)

    char = str[index]
    if char == "(" and (str[index + 1] == "(" or str[index + 1] == "["):
        out += "+2*("
        index += 1
    elif char == "(" and str[index + 1] == ")":
        out += "+2"
        index += 2

    elif char == "[" and (str[index + 1] == "(" or str[index + 1] == "["):
        out += "+3*("
        index += 1
    elif char == "[" and str[index + 1] == "]":
        out += "+3"
        index += 2

    elif char == ")":
        out += ")"
        index += 1
    elif char == "]":
        out += ")"
        index += 1

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함