結果

問題 No.22 括弧の対応
ユーザー tentententen
提出日時 2020-11-05 18:46:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 136 ms / 5,000 ms
コード長 783 bytes
コンパイル時間 3,894 ms
コンパイル使用メモリ 71,028 KB
実行使用メモリ 56,376 KB
最終ジャッジ日時 2023-09-29 17:13:01
合計ジャッジ時間 7,513 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
56,232 KB
testcase_01 AC 117 ms
56,036 KB
testcase_02 AC 116 ms
55,756 KB
testcase_03 AC 134 ms
56,048 KB
testcase_04 AC 132 ms
56,368 KB
testcase_05 AC 134 ms
55,908 KB
testcase_06 AC 136 ms
56,104 KB
testcase_07 AC 132 ms
56,292 KB
testcase_08 AC 132 ms
56,084 KB
testcase_09 AC 131 ms
56,376 KB
testcase_10 AC 131 ms
55,976 KB
testcase_11 AC 131 ms
56,084 KB
testcase_12 AC 132 ms
56,000 KB
testcase_13 AC 134 ms
56,040 KB
testcase_14 AC 122 ms
56,212 KB
testcase_15 AC 133 ms
56,092 KB
testcase_16 AC 133 ms
56,004 KB
testcase_17 AC 116 ms
55,960 KB
testcase_18 AC 117 ms
55,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		char[] arr = sc.next().toCharArray();
		int count = 0;
		if (arr[k - 1] == '(') {
		    for (int i = k - 1; ; i++) {
		        if (arr[i] == '(') {
		            count++;
		        } else {
		            count--;
		        }
		        if (count == 0) {
		            System.out.println(i + 1);
		            return;
		        }
		    }
		} else {
		    for (int i = k - 1; ; i--) {
		        if (arr[i] == '(') {
		            count++;
		        } else {
		            count--;
		        }
		        if (count == 0) {
		            System.out.println(i + 1);
		            return;
		        }
		    }
		}
	}
}
0