結果

問題 No.22 括弧の対応
ユーザー jp_stejp_ste
提出日時 2016-02-19 08:44:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 136 ms / 5,000 ms
コード長 681 bytes
コンパイル時間 2,001 ms
コンパイル使用メモリ 71,668 KB
実行使用メモリ 56,572 KB
最終ジャッジ日時 2023-09-27 13:02:24
合計ジャッジ時間 4,910 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 107 ms
55,796 KB
testcase_01 AC 112 ms
56,572 KB
testcase_02 AC 113 ms
56,256 KB
testcase_03 AC 136 ms
56,200 KB
testcase_04 AC 128 ms
55,872 KB
testcase_05 AC 127 ms
55,996 KB
testcase_06 AC 126 ms
55,968 KB
testcase_07 AC 127 ms
56,368 KB
testcase_08 AC 127 ms
56,132 KB
testcase_09 AC 125 ms
56,316 KB
testcase_10 AC 129 ms
55,832 KB
testcase_11 AC 124 ms
56,244 KB
testcase_12 AC 124 ms
55,804 KB
testcase_13 AC 128 ms
56,340 KB
testcase_14 AC 112 ms
55,836 KB
testcase_15 AC 121 ms
56,252 KB
testcase_16 AC 124 ms
56,220 KB
testcase_17 AC 111 ms
56,528 KB
testcase_18 AC 108 ms
56,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		try (Scanner scan = new Scanner(System.in)) {
			int N = scan.nextInt();
			int K = scan.nextInt();
			char S[] = scan.next().toCharArray();
			
			int count = 0;
			if(S[K-1] == '(') {
				for(int i=K-1; i<N; i++) {
					if(S[i] == '(') {
						count++;
					} else  {
						count--;
						if(count == 0) {
							System.out.println(i+1);
							break;
						}
					}
				}
			} else {
				for(int i=K-1; i>=0; i--) {
					if(S[i] == ')') {
						count++;
					} else  {
						count--;
						if(count == 0) {
							System.out.println(i+1);
							break;
						}
					}
				}
			}
		}
	}
}
0