結果

問題 No.22 括弧の対応
ユーザー soujikisoujiki
提出日時 2015-04-24 19:56:46
言語 Java19
(openjdk 19.0.1)
結果
AC  
実行時間 147 ms / 5,000 ms
コード長 703 bytes
コンパイル時間 3,175 ms
コンパイル使用メモリ 71,908 KB
実行使用メモリ 56,100 KB
最終ジャッジ日時 2023-07-06 11:43:42
合計ジャッジ時間 6,923 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
56,040 KB
testcase_01 AC 127 ms
55,632 KB
testcase_02 AC 127 ms
55,672 KB
testcase_03 AC 145 ms
55,424 KB
testcase_04 AC 140 ms
56,100 KB
testcase_05 AC 139 ms
55,936 KB
testcase_06 AC 142 ms
55,796 KB
testcase_07 AC 145 ms
55,956 KB
testcase_08 AC 141 ms
56,076 KB
testcase_09 AC 140 ms
55,920 KB
testcase_10 AC 140 ms
55,996 KB
testcase_11 AC 139 ms
55,888 KB
testcase_12 AC 143 ms
55,492 KB
testcase_13 AC 147 ms
56,004 KB
testcase_14 AC 135 ms
55,880 KB
testcase_15 AC 140 ms
55,612 KB
testcase_16 AC 140 ms
55,768 KB
testcase_17 AC 121 ms
55,888 KB
testcase_18 AC 126 ms
55,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Yukicoder_22{
	public static void main(String[] args){
		Scanner stdIn = new Scanner(System.in);
		int N = stdIn.nextInt();
		int K = stdIn.nextInt();
		String str = stdIn.next();

		char par = str.charAt(K-1);

		if(par == (char)'('){
			int count = 1;
			for(int i=K;i<N;i++){
				if(str.charAt(i) == (char)'('){
					count++;
				}
				else{
					count--;
				}

				if(count == 0){
					System.out.println(i+1);
					break;
				}
			}
		}
		else{
			int count = 1;
			for(int i=K-2;i>=0;i--){
				if(str.charAt(i) == (char)'('){
					count--;
				}
				else{
					count++;
				}

				if(count == 0){
					System.out.println(i+1);
					break;
				}
			}

		}



	}
}
0