結果

問題 No.22 括弧の対応
ユーザー Tsukasa_TypeTsukasa_Type
提出日時 2018-02-15 19:18:23
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 962 bytes
コンパイル時間 2,209 ms
コンパイル使用メモリ 74,116 KB
実行使用メモリ 58,112 KB
最終ジャッジ日時 2023-08-27 06:57:08
合計ジャッジ時間 5,856 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,496 KB
testcase_01 AC 123 ms
55,852 KB
testcase_02 AC 123 ms
55,420 KB
testcase_03 AC 149 ms
58,112 KB
testcase_04 AC 147 ms
55,568 KB
testcase_05 AC 148 ms
55,844 KB
testcase_06 AC 150 ms
55,744 KB
testcase_07 AC 151 ms
55,340 KB
testcase_08 AC 150 ms
55,680 KB
testcase_09 AC 149 ms
56,148 KB
testcase_10 AC 149 ms
55,492 KB
testcase_11 AC 147 ms
53,592 KB
testcase_12 AC 150 ms
55,664 KB
testcase_13 AC 149 ms
55,564 KB
testcase_14 AC 132 ms
55,664 KB
testcase_15 AC 154 ms
55,596 KB
testcase_16 AC 151 ms
55,584 KB
testcase_17 WA -
testcase_18 AC 127 ms
55,492 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 = 0;
		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-1; i<s.length(); i++) {
				if (ar[i]==c1 && i+1!=k) {System.out.println(i+1); break;}
			}
		}
		else {
			for (int i=k-1; i>=0; i--) {
				if (ar[i]==c1 && i+1!=k) {System.out.println(i+1); break;}
			}
		}
	} 
}
0