結果

問題 No.22 括弧の対応
ユーザー t8m8⛄️t8m8⛄️
提出日時 2015-04-03 04:12:14
言語 Java21
(openjdk 21)
結果
AC  
実行時間 130 ms / 5,000 ms
コード長 1,139 bytes
コンパイル時間 3,578 ms
コンパイル使用メモリ 75,676 KB
実行使用メモリ 57,888 KB
最終ジャッジ日時 2023-09-27 12:51:16
合計ジャッジ時間 6,401 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
56,004 KB
testcase_01 AC 110 ms
55,636 KB
testcase_02 AC 111 ms
56,088 KB
testcase_03 AC 128 ms
55,596 KB
testcase_04 AC 124 ms
55,672 KB
testcase_05 AC 121 ms
56,112 KB
testcase_06 AC 126 ms
56,072 KB
testcase_07 AC 127 ms
55,612 KB
testcase_08 AC 126 ms
57,888 KB
testcase_09 AC 126 ms
55,892 KB
testcase_10 AC 123 ms
55,924 KB
testcase_11 AC 125 ms
55,932 KB
testcase_12 AC 130 ms
56,048 KB
testcase_13 AC 126 ms
56,000 KB
testcase_14 AC 121 ms
55,888 KB
testcase_15 AC 126 ms
55,996 KB
testcase_16 AC 126 ms
55,560 KB
testcase_17 AC 110 ms
56,224 KB
testcase_18 AC 107 ms
55,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//No.22 括弧の対応

import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.lang.Math.*;

public class No22 {
    
    static final Scanner sc = new Scanner(System.in);
    static final PrintWriter out = new PrintWriter(System.out,false);

    static void solve() {
        int n = sc.nextInt();
        int k = sc.nextInt()-1;
        String s = sc.next();
        ArrayDeque<Integer> stack = new ArrayDeque<Integer>();
        for (int i=0; i<n; i++) {
            if (s.charAt(i) == '(') {
                stack.add(i);
            }else {
                int x = stack.pollLast();
                if (x == k) {
                    out.println((i+1));
                }else if (i == k) {
                    out.println((x+1));
                }
            }
        }
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();

        solve();
        out.flush();

        long end = System.currentTimeMillis();
        //trace(end-start + "ms");
        sc.close();
    }

    static void trace(Object... o) { System.out.println(deepToString(o));}
}
0