結果

問題 No.22 括弧の対応
ユーザー jimanojimano
提出日時 2017-05-06 16:05:46
言語 Java19
(openjdk 21)
結果
AC  
実行時間 95 ms / 5,000 ms
コード長 1,029 bytes
コンパイル時間 2,101 ms
コンパイル使用メモリ 74,932 KB
実行使用メモリ 54,400 KB
最終ジャッジ日時 2023-09-27 13:16:04
合計ジャッジ時間 4,403 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,588 KB
testcase_01 AC 45 ms
49,912 KB
testcase_02 AC 45 ms
49,860 KB
testcase_03 AC 95 ms
54,400 KB
testcase_04 AC 73 ms
52,328 KB
testcase_05 AC 71 ms
52,432 KB
testcase_06 AC 77 ms
52,392 KB
testcase_07 AC 91 ms
51,612 KB
testcase_08 AC 74 ms
52,400 KB
testcase_09 AC 73 ms
52,124 KB
testcase_10 AC 90 ms
51,952 KB
testcase_11 AC 72 ms
52,848 KB
testcase_12 AC 76 ms
51,948 KB
testcase_13 AC 90 ms
52,344 KB
testcase_14 AC 55 ms
51,348 KB
testcase_15 AC 93 ms
52,484 KB
testcase_16 AC 82 ms
51,624 KB
testcase_17 AC 45 ms
49,576 KB
testcase_18 AC 45 ms
49,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class Main{
	public static void main(String[] args) {
		try (BufferedReader br = 
				new BufferedReader(new InputStreamReader(System.in))) {
	        String[] N = br.readLine().split(" ");
	        int cntString = Integer.parseInt(N[0]);
	        int index = Integer.parseInt(N[1]);
	        String[] str = br.readLine().split("");

	        Deque<Integer> deq = new LinkedList<>();
	        Map<Integer, Integer> map = new HashMap<>();
	        
	        for (int i = 0; i < cntString; i++) {
	        	if (str[i].equals("(")) {
	        		deq.addFirst(i);
	        	}
	        	else {
	        		int j = deq.removeFirst();
	        		map.put(i, j);
	        		map.put(j, i);
	        	}
	        }
	        
	        System.out.println(map.get(index - 1) + 1);
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
}
0