python求助

P1241 括号序列

asdfjkd @ 2024-04-01 09:55:06

stack = []
ans = [0 for i in range(101)]
dicts = {'(':'()', '[':'[]', ')':'()', ']':'[]'}

li = input()
for i in range(len(li)):
    if li[i] == ')':
        if not stack:
            continue
        if li[stack[-1]] == '(':
            ans[i] = ans[stack[-1]] = 1
            stack.pop()
    elif li[i] == ']':
        if not stack:
            continue
        if li[stack[-1]] == '[':
            ans[i] = ans[stack[-1]] = 1
            stack.pop()
    else:
        stack.append(i)
for i in range(len(li)):
    if ans[i]:
        print(li[i], end='')
    else:
        print(dicts[li[i]], end=' ')

感觉没问题,但就是过不了


|