結果

問題 No.22 括弧の対応
ユーザー kashiwagi513
提出日時 2019-02-23 20:57:57
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 213 ms / 5,000 ms
コード長 757 bytes
コンパイル時間 310 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 12,288 KB
最終ジャッジ日時 2024-07-20 07:40:25
合計ジャッジ時間 3,102 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-

from queue import LifoQueue

def index(l):
    return l[0]

def value(l):
    return l[1]

def match(l, r): return value(l) == '(' and value(r) == ')'

def reduce(ls, goal):
    ls = ls
    stack = LifoQueue()
    while len(ls) >= 2:
        hd, hd_, rest = ls[0], ls[1], ls[2:]
        if match(hd, hd_):
            if index(hd) == goal:
                return index(hd_)
            if index(hd_) == goal:
                return index(hd)
            ls = [stack.get()] + rest if not stack.empty() else rest
        else:
            stack.put(hd)
            ls = [hd_] + rest

def solve(n, k, s):
    ls = [(i+1, c) for i, c in enumerate(s)]
    print(reduce(ls, k))

n, k = input().split()
s = input()
solve(int(n), int(k), s)
0