結果

問題 No.22 括弧の対応
ユーザー spaciaspacia
提出日時 2015-12-23 13:36:38
言語 Java19
(openjdk 21)
結果
AC  
実行時間 40 ms / 5,000 ms
コード長 1,061 bytes
コンパイル時間 1,823 ms
コンパイル使用メモリ 73,780 KB
実行使用メモリ 49,612 KB
最終ジャッジ日時 2023-09-27 12:59:45
合計ジャッジ時間 3,241 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
49,312 KB
testcase_01 AC 38 ms
49,396 KB
testcase_02 AC 37 ms
49,316 KB
testcase_03 AC 40 ms
49,324 KB
testcase_04 AC 37 ms
49,612 KB
testcase_05 AC 36 ms
49,352 KB
testcase_06 AC 38 ms
49,428 KB
testcase_07 AC 39 ms
49,304 KB
testcase_08 AC 39 ms
49,132 KB
testcase_09 AC 38 ms
49,352 KB
testcase_10 AC 39 ms
49,312 KB
testcase_11 AC 39 ms
49,144 KB
testcase_12 AC 38 ms
49,424 KB
testcase_13 AC 38 ms
49,228 KB
testcase_14 AC 38 ms
49,308 KB
testcase_15 AC 40 ms
49,228 KB
testcase_16 AC 40 ms
49,328 KB
testcase_17 AC 39 ms
49,208 KB
testcase_18 AC 39 ms
49,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

class Main22 {
	
	public static void out (Object o) {
		System.out.println(o);
	}
	
	public static void main (String[] args) throws IOException {
		BufferedReader br = 
			new BufferedReader(new InputStreamReader(System.in));
		
		String[] line1 = br.readLine().split(" ");
		int n = Integer.parseInt(line1[0]);
		int k = Integer.parseInt(line1[1]);
		String s = br.readLine();
		
		char startChar = s.charAt(k - 1);
		int end = startChar == '(' ? n - 1 : 0;
		int i = startChar == '(' ? k : k - 2;
		int cnt = 0;
		
		//out("i : " + i + "\tend : " + end);
		
		while (true) {
			char c = s.charAt(i);
			if (c == '(') {
				if (startChar == '(') {
					cnt++;
				} else if (cnt == 0) {
					out(i + 1);
					break;
				} else {
					cnt--;
				}
			} else {
				if (startChar == '(') {
					if (cnt == 0) {
						out(i + 1);
						break;
					} else {
						cnt--;
					}
				} else {
					cnt++;
				}
			}
			i = i < end ? i + 1 : i - 1;
			//out("cnt : " + cnt + "\ti : " + i);
			if (i < 0 || i >= n) break;
		}
	}
}
0