結果

問題 No.22 括弧の対応
ユーザー AoiroFukurouAoiroFukurou
提出日時 2016-05-22 16:11:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 153 ms / 5,000 ms
コード長 879 bytes
コンパイル時間 3,468 ms
コンパイル使用メモリ 79,556 KB
実行使用メモリ 56,296 KB
最終ジャッジ日時 2023-09-27 13:06:29
合計ジャッジ時間 6,288 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 111 ms
56,296 KB
testcase_01 AC 107 ms
55,828 KB
testcase_02 AC 110 ms
55,704 KB
testcase_03 AC 153 ms
56,108 KB
testcase_04 AC 135 ms
56,164 KB
testcase_05 AC 147 ms
55,748 KB
testcase_06 AC 145 ms
56,028 KB
testcase_07 AC 145 ms
56,076 KB
testcase_08 AC 148 ms
54,156 KB
testcase_09 AC 146 ms
55,856 KB
testcase_10 AC 148 ms
55,704 KB
testcase_11 AC 121 ms
55,776 KB
testcase_12 AC 140 ms
55,920 KB
testcase_13 AC 134 ms
56,004 KB
testcase_14 AC 118 ms
56,112 KB
testcase_15 AC 148 ms
55,688 KB
testcase_16 AC 143 ms
56,040 KB
testcase_17 AC 114 ms
55,852 KB
testcase_18 AC 111 ms
55,696 KB
権限があれば一括ダウンロードができます

ソースコード

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