結果

問題 No.22 括弧の対応
ユーザー めうめう🎒
提出日時 2016-05-08 23:08:05
言語 Java
(openjdk 23)
結果
AC  
実行時間 179 ms / 5,000 ms
コード長 1,044 bytes
コンパイル時間 2,350 ms
コンパイル使用メモリ 77,780 KB
実行使用メモリ 41,808 KB
最終ジャッジ日時 2024-07-20 07:12:45
合計ジャッジ時間 6,390 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

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