【題解】ZeroJudge b304: 00673 – Parentheses Balance

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=b304

while True:
    try:
        n = int(input())
        for i in range(n):
            string = input()
            lst = []
            for c in string:
                if c == '(' or c == '[':
                    lst.append(c)
                elif lst:
                    if lst.pop() == '(':
                        if c != ')':
                            print('No')
                            break
                    else:
                        if c != ']':
                            print('No')
                            break
                else:
                    print('No')
                    break
            else:
                if lst:
                    print('No')
                else:
                    print('Yes')
    except:
        break
分享本文 Share with friends