結果

問題 No.22 括弧の対応
ユーザー fkwnw3_1243fkwnw3_1243
提出日時 2017-04-23 13:51:40
言語 Java21
(openjdk 21)
結果
AC  
実行時間 81 ms / 5,000 ms
コード長 1,398 bytes
コンパイル時間 3,285 ms
コンパイル使用メモリ 76,236 KB
実行使用メモリ 52,144 KB
最終ジャッジ日時 2023-09-27 13:15:29
合計ジャッジ時間 4,539 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,596 KB
testcase_01 AC 40 ms
49,548 KB
testcase_02 AC 41 ms
49,488 KB
testcase_03 AC 80 ms
49,508 KB
testcase_04 AC 63 ms
51,532 KB
testcase_05 AC 60 ms
51,860 KB
testcase_06 AC 74 ms
51,752 KB
testcase_07 AC 67 ms
51,632 KB
testcase_08 AC 62 ms
51,516 KB
testcase_09 AC 65 ms
51,508 KB
testcase_10 AC 65 ms
52,004 KB
testcase_11 AC 62 ms
51,556 KB
testcase_12 AC 64 ms
51,252 KB
testcase_13 AC 79 ms
52,028 KB
testcase_14 AC 49 ms
49,924 KB
testcase_15 AC 81 ms
49,980 KB
testcase_16 AC 81 ms
52,144 KB
testcase_17 AC 42 ms
49,540 KB
testcase_18 AC 42 ms
49,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;


public class Main {
    public static void main(String[] args) throws IOException {

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        String[] firstLine = reader.readLine().split(" ");
        int N = Integer.parseInt(firstLine[0]);
        int K = Integer.parseInt(firstLine[1]);

        String[] S = reader.readLine().split("");
        Stack<Parenthesis> stack = new Stack<>();
        for (int i = 0; i < N; i++) {

            if (S[i].equals("(")) {
                stack.add(new Parenthesis(S[i], i + 1));
            } else {
                Parenthesis parenthesis = stack.pop();
                if (i + 1 == K) {
                    System.out.println(parenthesis.index);
                    break;
                } else if (parenthesis.index == K) {
                    System.out.println(i + 1);
                    break;
                }
            }
        }
    }
}

class Parenthesis {
    String value;
    int index;

    Parenthesis(String value, int index) {
        this.value = value;
        this.index = index;
    }

    @Override
    public String toString() {
        return "Parenthesis{" +
                "value='" + value + '\'' +
                ", index=" + index +
                '}';
    }
}
0