結果

問題 No.22 括弧の対応
ユーザー matsuyoshi30matsuyoshi30
提出日時 2017-03-01 22:54:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 144 ms / 5,000 ms
コード長 671 bytes
コンパイル時間 3,103 ms
コンパイル使用メモリ 78,500 KB
実行使用メモリ 57,924 KB
最終ジャッジ日時 2023-09-27 13:12:14
合計ジャッジ時間 6,775 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,532 KB
testcase_01 AC 124 ms
55,804 KB
testcase_02 AC 125 ms
56,344 KB
testcase_03 AC 143 ms
57,924 KB
testcase_04 AC 142 ms
56,040 KB
testcase_05 AC 138 ms
56,116 KB
testcase_06 AC 139 ms
55,824 KB
testcase_07 AC 142 ms
55,524 KB
testcase_08 AC 140 ms
55,780 KB
testcase_09 AC 142 ms
56,272 KB
testcase_10 AC 140 ms
55,804 KB
testcase_11 AC 138 ms
55,668 KB
testcase_12 AC 140 ms
56,032 KB
testcase_13 AC 140 ms
55,680 KB
testcase_14 AC 131 ms
55,668 KB
testcase_15 AC 144 ms
55,888 KB
testcase_16 AC 142 ms
55,984 KB
testcase_17 AC 125 ms
55,884 KB
testcase_18 AC 123 ms
56,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        String s = in.next();
        char[] c = s.toCharArray();

        int mv = 1;
        k--;
        if(c[k] == ')') mv = -1;
        int cnt = 0;
        for(int i=k+mv; cnt!=1; i+=mv) {
            if(c[i] == c[k]) cnt--;
            else cnt++;
            if(cnt == 1) {
                System.out.println(i+1);
                break;
            }
        }

        in.close();
    }
}
0