結果

問題 No.22 括弧の対応
ユーザー dazydazy
提出日時 2016-09-08 14:43:39
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 1,228 bytes
コンパイル時間 3,357 ms
コンパイル使用メモリ 78,772 KB
実行使用メモリ 54,380 KB
最終ジャッジ日時 2024-04-27 17:58:01
合計ジャッジ時間 6,846 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
54,200 KB
testcase_01 AC 129 ms
54,308 KB
testcase_02 AC 128 ms
54,100 KB
testcase_03 AC 146 ms
54,280 KB
testcase_04 AC 141 ms
54,112 KB
testcase_05 AC 145 ms
54,136 KB
testcase_06 AC 140 ms
54,128 KB
testcase_07 AC 151 ms
54,188 KB
testcase_08 AC 129 ms
52,956 KB
testcase_09 AC 130 ms
53,172 KB
testcase_10 AC 132 ms
52,888 KB
testcase_11 AC 136 ms
54,248 KB
testcase_12 AC 141 ms
54,256 KB
testcase_13 AC 142 ms
54,200 KB
testcase_14 AC 132 ms
54,380 KB
testcase_15 AC 143 ms
53,992 KB
testcase_16 AC 143 ms
53,940 KB
testcase_17 RE -
testcase_18 AC 127 ms
54,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Run {
    public static void main (String arg[]) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        int K = scan.nextInt() - 1;
        if (N < 1||N > 10000||K < 1||K > N) System.exit(1);
        String S = scan.next();
        if (S.length()!=N) System.exit(1);
        boolean flag;
        if (S.charAt(K)=='(') flag = true;
        else flag = false;
        Stack stackS = new Stack();
        int count = 0;
        if (flag) {
            while (true) {
                if (S.charAt(K) == '(') {
                    stackS.push(S.charAt(K));
                    count++;
                } else {
                    stackS.pop();
                    count--;
                    if (count==0) break;
                }
                K++;
            }
        } else {
            while (true) {
                if (S.charAt(K) == ')') {
                    stackS.push(S.charAt(K));
                    count++;
                } else {
                    stackS.pop();
                    count--;
                    if (count == 0) break;
                }
                K--;
            }
        }

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