結果

問題 No.592 括弧の対応 (2)
ユーザー TsubasaTsubasa
提出日時 2018-02-07 20:58:42
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 4,642 ms / 5,000 ms
コード長 773 bytes
コンパイル時間 88 ms
コンパイル使用メモリ 11,840 KB
実行使用メモリ 22,680 KB
最終ジャッジ日時 2023-10-19 02:32:07
合計ジャッジ時間 10,447 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
10,148 KB
testcase_01 AC 4,622 ms
20,364 KB
testcase_02 AC 4,642 ms
22,680 KB
testcase_03 AC 309 ms
15,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import array

def main():
    N = int(input())
    S = input()

    D = []
    d = 1
    for s in S:
        if s == "(":
            D.append(d)
            d += 1
        else:
            d -= 1
            D.append(d)

    idx_stack = array.array("q", [])
    depth_stack = array.array("q", [-1])
    correspond = array.array("q", [-1] * N)

    for idx, depth in enumerate(D):
        idx_stack.append(idx)
        depth_stack.append(depth)

        if depth_stack[-1] == depth_stack[-2]:
            start = idx_stack[-2]
            end = idx_stack[-1]

            correspond[start] = end
            correspond[end] = start

            idx_stack = idx_stack[:-2]
            depth_stack = depth_stack[:-2]

    for c in correspond:
        print(c+1)





main()
0