結果

問題 No.22 括弧の対応
ユーザー Tsukasa_TypeTsukasa_Type
提出日時 2018-02-15 19:11:40
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 889 bytes
コンパイル時間 3,343 ms
コンパイル使用メモリ 73,456 KB
実行使用メモリ 57,980 KB
最終ジャッジ日時 2023-08-27 06:43:26
合計ジャッジ時間 7,179 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
56,124 KB
testcase_01 AC 116 ms
55,964 KB
testcase_02 AC 112 ms
55,852 KB
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;}
		}
		
		//配列を回して、深さがc1かつk番目でない、ただ一つの値を出力して終了
		for (int i=0; i<n; i++) {
			if (ar[i]==c1 && i+1!=k) {System.out.println(i+1); break;}
		}
	} 
}
0