結果

問題 No.22 括弧の対応
ユーザー shinshin
提出日時 2022-05-17 07:12:58
言語 Java21
(openjdk 21)
結果
AC  
実行時間 50 ms / 5,000 ms
コード長 1,033 bytes
コンパイル時間 7,353 ms
コンパイル使用メモリ 76,556 KB
実行使用メモリ 49,680 KB
最終ジャッジ日時 2023-10-13 03:01:12
合計ジャッジ時間 9,368 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,336 KB
testcase_01 AC 44 ms
49,496 KB
testcase_02 AC 44 ms
49,324 KB
testcase_03 AC 49 ms
49,384 KB
testcase_04 AC 46 ms
49,508 KB
testcase_05 AC 46 ms
49,516 KB
testcase_06 AC 49 ms
49,252 KB
testcase_07 AC 47 ms
49,236 KB
testcase_08 AC 44 ms
49,316 KB
testcase_09 AC 48 ms
49,452 KB
testcase_10 AC 45 ms
49,248 KB
testcase_11 AC 46 ms
47,772 KB
testcase_12 AC 48 ms
49,224 KB
testcase_13 AC 50 ms
49,416 KB
testcase_14 AC 44 ms
49,480 KB
testcase_15 AC 50 ms
49,400 KB
testcase_16 AC 50 ms
49,680 KB
testcase_17 AC 44 ms
49,252 KB
testcase_18 AC 45 ms
49,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;

public class No22 {

	public static void main(String[] args) throws IOException {
		//括弧の対応
		String[] text = readStr();
		int N = Integer.parseInt(text[0].split(" ")[0]) , K = Integer.parseInt(text[0].split(" ")[1]) , r = 0;
		String S = text[1];
		Deque<Integer> dq = new ArrayDeque<>();
		
		for(int i = 0;i < N;i++) {
			if(S.charAt(i) == '(') {
				dq.push(i+1);
			}else {
				r = dq.pop();
				if(i+1 == K || r == K) {
					r = i + 1 + r - K;
					break;
				}
			}
		}
		System.out.println(r);
		

	}
	
	public static String[] readStr() throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		ArrayList<String> list = new ArrayList<>();

		do {
			list.add(br.readLine());
		}while(br.ready());

		br.close();

		String[] text = new String[list.size()];
		list.toArray(text);

		return text;

	}

}
0