結果

問題 No.22 括弧の対応
ユーザー Tsukasa_TypeTsukasa_Type
提出日時 2018-02-15 19:23:37
言語 Java21
(openjdk 21)
結果
AC  
実行時間 156 ms / 5,000 ms
コード長 937 bytes
コンパイル時間 2,509 ms
コンパイル使用メモリ 79,180 KB
実行使用メモリ 57,824 KB
最終ジャッジ日時 2023-09-27 13:24:00
合計ジャッジ時間 6,272 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
56,304 KB
testcase_01 AC 126 ms
55,896 KB
testcase_02 AC 126 ms
55,848 KB
testcase_03 AC 156 ms
57,824 KB
testcase_04 AC 150 ms
55,604 KB
testcase_05 AC 146 ms
55,548 KB
testcase_06 AC 148 ms
55,336 KB
testcase_07 AC 147 ms
55,944 KB
testcase_08 AC 147 ms
55,748 KB
testcase_09 AC 148 ms
55,844 KB
testcase_10 AC 147 ms
55,604 KB
testcase_11 AC 147 ms
55,608 KB
testcase_12 AC 147 ms
56,192 KB
testcase_13 AC 150 ms
55,760 KB
testcase_14 AC 131 ms
55,724 KB
testcase_15 AC 149 ms
55,612 KB
testcase_16 AC 150 ms
56,052 KB
testcase_17 AC 125 ms
55,748 KB
testcase_18 AC 127 ms
55,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int n = sc.nextInt();
		int k = sc.nextInt();
		String s = sc.next();
		
		//解説にあったimos法を使ってみる
		//配列に深さを代入
		int[] ar = new int[n];
		int depth = 1;
		int c1 = 1;
		ar[0] = depth;
		for (int i=1; i<n; i++) {
			String s1 = s.substring(i-1,i);
			String s2 = s.substring(i,i+1);
			if (s1.equals("(") && s2.equals("(")) {
				depth++;
				ar[i] = depth;
			}
			else if (s1.equals(")") && s2.equals(")")) {
				depth--;
				ar[i] = depth;
			}
			else {
				ar[i] = depth;
			}
			if (i+1 == k) {c1 = depth;}
		}
		
		if (s.substring(k-1,k).equals("(")) {
			for (int i=k; i<s.length(); i++) {
				if (ar[i]==c1) {System.out.println(i+1); break;}
			}
		}
		else {
			for (int i=k-2; i>=0; i--) {
				if (ar[i]==c1) {System.out.println(i+1); break;}
			}
		}
	} 
}
0