結果

問題 No.22 括弧の対応
ユーザー kaoetaya
提出日時 2016-11-08 01:50:58
言語 Java
(openjdk 23)
結果
AC  
実行時間 149 ms / 5,000 ms
コード長 644 bytes
コンパイル時間 3,944 ms
コンパイル使用メモリ 75,336 KB
実行使用メモリ 41,608 KB
最終ジャッジ日時 2024-07-20 07:18:31
合計ジャッジ時間 6,887 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class pre {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
		
		int n,k;
		n = sc.nextInt();
		k = sc.nextInt();
		
        String s = sc.next();
		
		int count = 0;
		int i;
		
		if(s.charAt(k-1) == '('){
			for(i=k-1;i<n;i++){
				if(s.charAt(i) == '(')
					count++;
				else
					count--;
					
				if(count == 0){
					System.out.println(i+1);
					break;
				}
			}
			
		}
		else{
			for(i=k-1;i>=0;i--){
				if(s.charAt(i) == ')')
					count++;
				else
					count--;
					
				if(count == 0){
					System.out.println(i+1);
					break;
				}
			}
		}

    }
}
0