結果

問題 No.22 括弧の対応
ユーザー yagi2
提出日時 2017-04-26 12:16:05
言語 Java
(openjdk 23)
結果
AC  
実行時間 179 ms / 5,000 ms
コード長 693 bytes
コンパイル時間 2,285 ms
コンパイル使用メモリ 76,808 KB
実行使用メモリ 43,384 KB
最終ジャッジ日時 2024-07-20 07:23:14
合計ジャッジ時間 5,448 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder.No22;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int N = Integer.parseInt(sc.next());
        int K = Integer.parseInt(sc.next());

        String[] S = sc.next().split("");

        Map<Integer, Integer> dp = new HashMap<>();
        Stack<Integer> stack = new Stack<>();

        for (int i = 0; i < N; i++) {
            if (S[i].equals("(")) {
                stack.push(i);
            } else {
                int n = stack.pop();
                dp.put(i, n);
                dp.put(n, i);
            }
        }

        System.out.println(dp.get(K - 1) + 1);
    }
}
0