結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,540 KB
testcase_01 AC 127 ms
55,756 KB
testcase_02 AC 125 ms
55,736 KB
testcase_03 AC 155 ms
58,256 KB
testcase_04 AC 148 ms
55,944 KB
testcase_05 AC 149 ms
55,824 KB
testcase_06 AC 154 ms
55,868 KB
testcase_07 AC 153 ms
55,972 KB
testcase_08 AC 153 ms
55,904 KB
testcase_09 AC 151 ms
55,572 KB
testcase_10 AC 152 ms
55,936 KB
testcase_11 AC 149 ms
55,972 KB
testcase_12 AC 150 ms
56,024 KB
testcase_13 AC 153 ms
56,016 KB
testcase_14 AC 131 ms
55,748 KB
testcase_15 AC 151 ms
55,528 KB
testcase_16 AC 154 ms
55,576 KB
testcase_17 WA -
testcase_18 AC 125 ms
56,000 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; 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