結果

問題 No.22 括弧の対応
ユーザー yagi2yagi2
提出日時 2017-04-26 12:16:05
言語 Java19
(openjdk 21)
結果
AC  
実行時間 158 ms / 5,000 ms
コード長 693 bytes
コンパイル時間 2,155 ms
コンパイル使用メモリ 74,628 KB
実行使用メモリ 58,464 KB
最終ジャッジ日時 2023-09-27 13:15:46
合計ジャッジ時間 5,993 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
55,896 KB
testcase_01 AC 106 ms
55,248 KB
testcase_02 AC 107 ms
55,732 KB
testcase_03 AC 158 ms
58,112 KB
testcase_04 AC 144 ms
56,184 KB
testcase_05 AC 142 ms
56,464 KB
testcase_06 AC 152 ms
56,100 KB
testcase_07 AC 155 ms
58,464 KB
testcase_08 AC 155 ms
56,256 KB
testcase_09 AC 147 ms
56,380 KB
testcase_10 AC 151 ms
58,344 KB
testcase_11 AC 139 ms
54,584 KB
testcase_12 AC 144 ms
56,132 KB
testcase_13 AC 153 ms
58,152 KB
testcase_14 AC 132 ms
56,700 KB
testcase_15 AC 155 ms
58,416 KB
testcase_16 AC 155 ms
58,288 KB
testcase_17 AC 109 ms
56,028 KB
testcase_18 AC 106 ms
55,780 KB
権限があれば一括ダウンロードができます

ソースコード

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