結果

問題 No.22 括弧の対応
ユーザー GrenacheGrenache
提出日時 2015-09-22 22:56:14
言語 Java21
(openjdk 21)
結果
AC  
実行時間 142 ms / 5,000 ms
コード長 935 bytes
コンパイル時間 3,164 ms
コンパイル使用メモリ 72,316 KB
実行使用メモリ 56,236 KB
最終ジャッジ日時 2023-09-27 12:57:18
合計ジャッジ時間 6,768 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,840 KB
testcase_01 AC 126 ms
55,592 KB
testcase_02 AC 126 ms
53,568 KB
testcase_03 AC 141 ms
55,884 KB
testcase_04 AC 141 ms
55,824 KB
testcase_05 AC 138 ms
55,560 KB
testcase_06 AC 141 ms
55,828 KB
testcase_07 AC 140 ms
55,820 KB
testcase_08 AC 142 ms
55,812 KB
testcase_09 AC 139 ms
56,068 KB
testcase_10 AC 142 ms
55,536 KB
testcase_11 AC 140 ms
56,120 KB
testcase_12 AC 142 ms
55,532 KB
testcase_13 AC 139 ms
55,648 KB
testcase_14 AC 130 ms
55,884 KB
testcase_15 AC 141 ms
56,236 KB
testcase_16 AC 141 ms
56,072 KB
testcase_17 AC 125 ms
55,764 KB
testcase_18 AC 125 ms
55,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;


public class Main_yukicoder22 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int k = sc.nextInt() - 1;
        char[] s = sc.next().toCharArray();
        
        if (s[k] == '(') {
        	int tmp = 0;
        	for (int i = k + 1; i < n; i++) {
        		if (s[i] == ')') {
        			if (tmp == 0) {
        				System.out.println(i + 1);
        				break;
        			} else {
        				tmp--;
        			}
        		} else {
        			tmp++;
        		}
        	}
        } else {
        	int tmp = 0;
        	for (int i = k - 1; i >= 0; i--) {
        		if (s[i] == '(') {
        			if (tmp == 0) {
        				System.out.println(i + 1);
        				break;
        			} else {
        				tmp--;
        			}
        		} else {
        			tmp++;
        		}
        	}
        }

        sc.close();
    }
}
0