【刷题】字符串展开

44 阅读1分钟
import sys

line = sys.stdin.readline().strip()
# line = "abc2{de3[fg]}"
stack = []
left_flag = {"{", "[", "("}
right_flag = {"}", "]", ")"}
for char in line:
    if char in right_flag:
        cur = ""
        top = stack.pop(-1)
        while top not in left_flag:
            cur = top + cur
            top = stack.pop(-1)
        top = stack.pop(-1)
        num = ""
        while top.isdigit():
            num = top + num
            top = stack.pop(-1)
        stack.append(top)
        cur_res = int(num) * cur
        stack.append(cur_res)
    else:
        stack.append(char)
answer = "".join(stack)
print(answer)