結果

問題 No.22 括弧の対応
ユーザー YamaKasaYamaKasa
提出日時 2018-09-25 15:57:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 125 ms / 5,000 ms
コード長 601 bytes
コンパイル時間 4,131 ms
コンパイル使用メモリ 71,280 KB
実行使用メモリ 56,276 KB
最終ジャッジ日時 2023-09-27 13:31:20
合計ジャッジ時間 5,069 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 105 ms
55,992 KB
testcase_01 AC 104 ms
56,088 KB
testcase_02 AC 107 ms
55,932 KB
testcase_03 AC 123 ms
56,268 KB
testcase_04 AC 121 ms
56,024 KB
testcase_05 AC 120 ms
56,140 KB
testcase_06 AC 123 ms
55,868 KB
testcase_07 AC 119 ms
56,044 KB
testcase_08 AC 118 ms
56,116 KB
testcase_09 AC 117 ms
54,460 KB
testcase_10 AC 119 ms
56,152 KB
testcase_11 AC 122 ms
56,180 KB
testcase_12 AC 123 ms
54,048 KB
testcase_13 AC 125 ms
56,276 KB
testcase_14 AC 111 ms
56,036 KB
testcase_15 AC 120 ms
56,148 KB
testcase_16 AC 121 ms
56,088 KB
testcase_17 AC 104 ms
55,972 KB
testcase_18 AC 104 ms
56,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

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

		scan.close();
		Deque<Integer> stack = new ArrayDeque<Integer>();
		K--;
		for(int i = 0; i < N; i++) {
			char c = S.charAt(i);
			if(c == '(') {
				stack.push(i);
			}else {
				int k = stack.pop();
				if(i == K) {
					System.out.println(k + 1);
					break;
				}else if(k == K) {
					System.out.println(i + 1);
				}
			}
		}
	}
}
0