結果

問題 No.3503 Brackets Stack Query 2
コンテスト
ユーザー praddumna12
提出日時 2026-04-18 04:29:15
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
WA  
実行時間 -
コード長 1,368 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 834 ms
コンパイル使用メモリ 20,696 KB
実行使用メモリ 54,232 KB
最終ジャッジ日時 2026-04-18 04:30:04
合計ジャッジ時間 42,560 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 20 WA * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
input = sys.stdin.readline

Q = int(input())

stack = []
history = []   # store actions
invalid = 0    # count of invalid conditions

for _ in range(Q):
    query = input().split()

    if query[0] == '1':
        c = query[1]

        if c == '(':
            stack.append(False)
            history.append(('push',))

        elif c == '|':
            if stack:
                prev = stack[-1]
                stack[-1] = True
                history.append(('bar', prev))
            else:
                history.append(('bar_empty',))

        else:  # ')'
            if not stack:
                invalid += 1
                history.append(('bad_close',))
            else:
                has_bar = stack.pop()
                if not has_bar:
                    invalid += 1
                history.append(('pop', has_bar))

    else:
        op = history.pop()

        if op[0] == 'push':
            stack.pop()

        elif op[0] == 'bar':
            stack[-1] = op[1]

        elif op[0] == 'bar_empty':
            pass

        elif op[0] == 'bad_close':
            invalid -= 1

        elif op[0] == 'pop':
            has_bar = op[1]
            if not has_bar:
                invalid -= 1
            stack.append(has_bar)

    # check validity
    if invalid == 0 and not stack:
        print("Yes")
    else:
        print("No")
0