結果

問題 No.22 括弧の対応
ユーザー AoiroFukurou
提出日時 2016-05-22 16:11:00
言語 Java
(openjdk 23)
結果
AC  
実行時間 206 ms / 5,000 ms
コード長 879 bytes
コンパイル時間 3,940 ms
コンパイル使用メモリ 77,436 KB
実行使用メモリ 54,412 KB
最終ジャッジ日時 2024-07-20 07:13:50
合計ジャッジ時間 8,052 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.Scanner;

public class No22 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		sc.nextLine();
		String S = sc.nextLine();
		sc.close();
		int c = 1;
		int K2 = K - 1;
		String[] str = new String[N];
		for (int i = 0; i < N; i++) {
			str[i] = S.substring(i, i + 1);
		}
		if (str[K2].equals("(")) {
			while (true) {
				if (c == 0) {
					System.out.println(K2 + 1);
					break;
				}else if (str[K2 + 1].equals("(")) {
				c++;
				K2++;
			} else if (str[K2 + 1].equals(")")) {
				c--;
				K2++;
			}
			}
		}else if(str[K2].equals(")")){
			while(true){
				if(c == 0){
					System.out.println(K2 + 1);
					break;
				}else if(str[K2 - 1].equals(")")){
					c++;
					K2--;
				}else if(str[K2 - 1].equals("(")){
					c--;
					K2--;
				}
			}
		}
	}
}
0