結果

問題 No.22 括弧の対応
ユーザー kano_townkano_town
提出日時 2014-11-19 21:41:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 144 ms / 5,000 ms
コード長 713 bytes
コンパイル時間 3,593 ms
コンパイル使用メモリ 72,012 KB
実行使用メモリ 56,120 KB
最終ジャッジ日時 2023-09-27 12:46:33
合計ジャッジ時間 6,992 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
53,448 KB
testcase_01 AC 124 ms
55,776 KB
testcase_02 AC 126 ms
56,084 KB
testcase_03 AC 143 ms
56,120 KB
testcase_04 AC 139 ms
55,880 KB
testcase_05 AC 140 ms
55,940 KB
testcase_06 AC 142 ms
55,436 KB
testcase_07 AC 141 ms
55,800 KB
testcase_08 AC 143 ms
55,944 KB
testcase_09 AC 139 ms
56,052 KB
testcase_10 AC 144 ms
55,608 KB
testcase_11 AC 139 ms
55,840 KB
testcase_12 AC 142 ms
56,088 KB
testcase_13 AC 139 ms
56,064 KB
testcase_14 AC 129 ms
55,832 KB
testcase_15 AC 140 ms
55,964 KB
testcase_16 AC 142 ms
55,844 KB
testcase_17 AC 125 ms
55,448 KB
testcase_18 AC 126 ms
55,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Yukicoder22 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt() - 1;
		String s = sc.next();

		int i = 0, count = 0;
		if (s.charAt(k) == '(') {
			for (i = k + 1; i < s.length(); i++) {
				if (s.charAt(i) == '(') {
					count++;
				} else if (s.charAt(i) == ')') {
					if (count == 0) break;
					count--;
				}
			}
		} else if (s.charAt(k) == ')') {
			for (i = k - 1; i >= 0; i--) {
				if (s.charAt(i) == '(') {
					if (count == 0) break;
					count++;
				} else if (s.charAt(i) == ')') {
					count--;
				}
			}
		}
		System.out.println(i + 1);
	}
}
0