結果

問題 No.22 括弧の対応
ユーザー chiho_miyako
提出日時 2015-04-10 12:58:57
言語 Java
(openjdk 23)
結果
AC  
実行時間 137 ms / 5,000 ms
コード長 1,049 bytes
コンパイル時間 1,919 ms
コンパイル使用メモリ 74,508 KB
実行使用メモリ 41,416 KB
最終ジャッジ日時 2024-07-20 06:59:30
合計ジャッジ時間 5,155 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception {
        Scanner koko = new Scanner(System.in);
        int n = koko.nextInt();
        int x = koko.nextInt();
        String a = koko.next();
        char[] aa = a.toCharArray();
        int count = 0;
        if(aa[x-1]=='('){
            for(int i=0; i<n-x+1; i++){
                if(aa[x-1+i]=='('){
                    count = count + 1;
                }else{
                    count = count - 1;
                    if(count==0){
                        System.out.println(x+i);
                        break;
                    }
                }
            }
        }else{
            for(int i=x-1; i>=0; i--){
                if(aa[i]==')'){
                    count = count + 1;
                }else{
                    count = count - 1;
                    if(count==0){
                        System.out.println(i+1);
                        break;
                    }
                }
            }
        }
    }
}
0