結果

問題 No.22 括弧の対応
ユーザー sekiya9311
提出日時 2016-08-02 01:19:20
言語 Java
(openjdk 23)
結果
AC  
実行時間 193 ms / 5,000 ms
コード長 723 bytes
コンパイル時間 3,680 ms
コンパイル使用メモリ 74,324 KB
実行使用メモリ 41,656 KB
最終ジャッジ日時 2024-07-20 07:14:53
合計ジャッジ時間 7,954 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Yuki022 {
	static int N, K;
	static String s;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		N = sc.nextInt();
		K = sc.nextInt() - 1;
		sc.nextLine();
		s = sc.nextLine();
		System.out.println(s.charAt(K) == '(' ? solve('(') : solve(')'));
		sc.close();
	}

	static int solve(char c) {
		int cnt = 1;
		if (c == '(') {
			for (int i = K + 1; i < N; i++) {
				if (s.charAt(i) == c)
					cnt++;
				else
					cnt--;
				if (cnt == 0) {
					return i + 1;
				}
			}
		} else {
			for (int i = K - 1; i >= 0; i--) {
				if (s.charAt(i) == c)
					cnt++;
				else
					cnt--;
				if (cnt == 0) {
					return i + 1;
				}
			}
		}
		return -1;
	}
}
0