結果

問題 No.22 括弧の対応
ユーザー lrf141lrf141
提出日時 2016-11-27 10:15:22
言語 Java
(openjdk 23)
結果
AC  
実行時間 181 ms / 5,000 ms
コード長 639 bytes
コンパイル時間 3,414 ms
コンパイル使用メモリ 74,952 KB
実行使用メモリ 41,404 KB
最終ジャッジ日時 2024-07-20 07:18:42
合計ジャッジ時間 7,374 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
public class no22{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int k = scan.nextInt();
		
        String s = scan.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