結果

問題 No.22 括弧の対応
ユーザー spacia
提出日時 2015-12-23 13:36:38
言語 Java
(openjdk 23)
結果
AC  
実行時間 55 ms / 5,000 ms
コード長 1,061 bytes
コンパイル時間 2,508 ms
コンパイル使用メモリ 76,776 KB
実行使用メモリ 36,992 KB
最終ジャッジ日時 2024-07-20 07:06:48
合計ジャッジ時間 3,891 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

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