結果

問題 No.22 括弧の対応
ユーザー dazydazy
提出日時 2016-09-08 14:49:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 133 ms / 5,000 ms
コード長 1,237 bytes
コンパイル時間 2,989 ms
コンパイル使用メモリ 74,332 KB
実行使用メモリ 58,112 KB
最終ジャッジ日時 2023-09-27 13:08:21
合計ジャッジ時間 6,267 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
56,040 KB
testcase_01 AC 115 ms
58,112 KB
testcase_02 AC 110 ms
55,772 KB
testcase_03 AC 127 ms
55,872 KB
testcase_04 AC 130 ms
55,860 KB
testcase_05 AC 129 ms
55,956 KB
testcase_06 AC 126 ms
55,808 KB
testcase_07 AC 126 ms
56,320 KB
testcase_08 AC 125 ms
56,064 KB
testcase_09 AC 130 ms
55,804 KB
testcase_10 AC 126 ms
56,356 KB
testcase_11 AC 123 ms
55,996 KB
testcase_12 AC 127 ms
55,768 KB
testcase_13 AC 129 ms
56,180 KB
testcase_14 AC 121 ms
56,284 KB
testcase_15 AC 133 ms
55,696 KB
testcase_16 AC 129 ms
56,364 KB
testcase_17 AC 116 ms
55,964 KB
testcase_18 AC 112 ms
56,132 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();
        if (N < 1||N > 10000||K < 1||K > N) System.exit(1);
        String S = scan.next();
        if (S.length()!=N) System.exit(1);
        Stack stackS = new Stack();
        int count = 0;
        K--;
        boolean flag;
        if (S.charAt(K)=='(') flag = true;
        else flag = false;
        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