結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

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) {System.out.println(i+1); break;}
			}
		}
		else {
			for (int i=k-1; i>=0; i--) {
				if (ar[i]==c1) {System.out.println(i+1); break;}
			}
		}
	} 
}
0