結果

問題 No.22 括弧の対応
ユーザー discoNekodiscoNeko
提出日時 2016-04-29 00:58:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 45 ms / 5,000 ms
コード長 908 bytes
コンパイル時間 3,259 ms
コンパイル使用メモリ 74,156 KB
実行使用メモリ 49,812 KB
最終ジャッジ日時 2023-09-27 13:04:38
合計ジャッジ時間 3,936 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
49,392 KB
testcase_01 AC 39 ms
49,480 KB
testcase_02 AC 39 ms
49,244 KB
testcase_03 AC 41 ms
49,400 KB
testcase_04 AC 40 ms
49,428 KB
testcase_05 AC 45 ms
49,112 KB
testcase_06 AC 41 ms
49,188 KB
testcase_07 AC 40 ms
49,240 KB
testcase_08 AC 39 ms
49,200 KB
testcase_09 AC 41 ms
49,456 KB
testcase_10 AC 41 ms
49,244 KB
testcase_11 AC 39 ms
49,420 KB
testcase_12 AC 40 ms
49,300 KB
testcase_13 AC 40 ms
49,456 KB
testcase_14 AC 40 ms
49,308 KB
testcase_15 AC 42 ms
49,812 KB
testcase_16 AC 43 ms
49,444 KB
testcase_17 AC 39 ms
49,360 KB
testcase_18 AC 39 ms
49,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.math.*;
 
public class Main {
	public static void main(String[] args)throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		String[] str = br.readLine().split(" ");
		int n = Integer.parseInt(str[0]);
		int k = Integer.parseInt(str[1]);
		String a = br.readLine();
		int[] f = new int[n/2];
		int[] l = new int[n/2];
		int cnt = 0;
		int pos = -1;
		for(int i = 0; i < n; i++){
			char c = a.charAt(i);
			if(c=='('){
				f[cnt]=i+1;
				if(i+1==k)pos = cnt;
				cnt++;
			}else{
				cnt--;
				l[cnt]=i+1;
				if(i+1==k){
					sb.append(f[cnt]);
					System.out.println(sb);
					return;
				}
				if(pos==cnt){
					sb.append(l[cnt]);
					System.out.println(sb);
					return;
				}
			}
		}
	}
}
0