結果

問題 No.22 括弧の対応
ユーザー めうめう🎒めうめう🎒
提出日時 2016-05-08 23:08:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 126 ms / 5,000 ms
コード長 1,044 bytes
コンパイル時間 4,708 ms
コンパイル使用メモリ 73,664 KB
実行使用メモリ 56,088 KB
最終ジャッジ日時 2023-09-27 13:05:07
合計ジャッジ時間 4,886 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 109 ms
55,808 KB
testcase_01 AC 106 ms
56,024 KB
testcase_02 AC 110 ms
55,668 KB
testcase_03 AC 123 ms
55,416 KB
testcase_04 AC 122 ms
55,768 KB
testcase_05 AC 120 ms
55,708 KB
testcase_06 AC 124 ms
55,424 KB
testcase_07 AC 116 ms
55,612 KB
testcase_08 AC 120 ms
56,084 KB
testcase_09 AC 122 ms
55,800 KB
testcase_10 AC 123 ms
55,412 KB
testcase_11 AC 116 ms
55,916 KB
testcase_12 AC 123 ms
55,732 KB
testcase_13 AC 119 ms
53,956 KB
testcase_14 AC 112 ms
55,980 KB
testcase_15 AC 126 ms
56,020 KB
testcase_16 AC 125 ms
56,088 KB
testcase_17 AC 108 ms
55,412 KB
testcase_18 AC 107 ms
55,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	private String str;
	private int a;
	private int n;
	
	public void setStr(String str){
		this.str = str;
	}
	
	public void setA(int a){
		this.a = a;
	}
	
	public void setN(int n){
		this.n = n;
	}
	
	public int run(){
		int now_r = 0;
		int now_l = 0;
		int ans = 0;
		char[] s = str.toCharArray();
		if(s[a] == '('){
			now_l = 1;
			for(int i = a+1;i <= n;i++){
				if(s[i] == '('){
					now_l++;
				}else if(s[i] == ')'){
					now_r++;
				}
				if(now_r == now_l){
					ans = i;
					break;
				}
			}
		}else{
			now_r = 1;
			for(int i = a-1;i >= 0;i--){
				if(s[i] == '('){
					now_l++;
				}else if(s[i] == ')'){
					now_r++;
				}
				if(now_r == now_l){
					ans = i;
					break;
				}
			}
		}
		return ans + 1;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Main m = new Main();
		int n = sc.nextInt();
		m.setN(n);
		int a = sc.nextInt();
		a--;
		m.setA(a);
		String str = sc.next();
		m.setStr(str);
		System.out.println(m.run());
	}
}
0