結果

問題 No.22 括弧の対応
コンテスト
ユーザー AoiroFukurou
提出日時 2016-05-22 16:11:00
言語 Java
(openjdk 26.0.2.1)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 91 ms / 5,000 ms
+ 835µs
コード長 879 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,276 ms
コンパイル使用メモリ 85,064 KB
実行使用メモリ 45,140 KB
最終ジャッジ日時 2026-08-19 17:18:45
合計ジャッジ時間 5,340 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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