結果

問題 No.22 括弧の対応
ユーザー tentententen
提出日時 2022-09-12 11:00:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 46 ms / 5,000 ms
コード長 1,515 bytes
コンパイル時間 3,248 ms
コンパイル使用メモリ 74,544 KB
実行使用メモリ 49,632 KB
最終ジャッジ日時 2023-08-19 14:58:07
合計ジャッジ時間 5,626 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
49,480 KB
testcase_01 AC 37 ms
49,316 KB
testcase_02 AC 36 ms
49,556 KB
testcase_03 AC 43 ms
49,356 KB
testcase_04 AC 41 ms
49,260 KB
testcase_05 AC 40 ms
49,252 KB
testcase_06 AC 44 ms
49,612 KB
testcase_07 AC 41 ms
49,328 KB
testcase_08 AC 40 ms
49,328 KB
testcase_09 AC 43 ms
49,476 KB
testcase_10 AC 42 ms
49,308 KB
testcase_11 AC 41 ms
49,212 KB
testcase_12 AC 43 ms
49,500 KB
testcase_13 AC 45 ms
49,432 KB
testcase_14 AC 40 ms
47,592 KB
testcase_15 AC 45 ms
49,632 KB
testcase_16 AC 46 ms
49,616 KB
testcase_17 AC 39 ms
49,304 KB
testcase_18 AC 37 ms
49,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int idx = sc.nextInt();
        char[] values = sc.next().toCharArray();
        ArrayDeque<Integer> stack = new ArrayDeque<>();
        for (int i = 1; i <= n; i++) {
            if (values[i - 1] == '(') {
                stack.push(i);
            } else {
                int current = stack.pop();
                if (current == idx) {
                    System.out.println(i);
                    return;
                } else if (i == idx) {
                    System.out.println(current);
                    return;
                }
            }
        }
    }
}
    
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0