結果

問題 No.22 括弧の対応
ユーザー papipen
提出日時 2017-04-06 17:14:05
言語 Java
(openjdk 23)
結果
AC  
実行時間 159 ms / 5,000 ms
コード長 685 bytes
コンパイル時間 2,771 ms
コンパイル使用メモリ 74,964 KB
実行使用メモリ 41,984 KB
最終ジャッジ日時 2024-07-20 07:21:05
合計ジャッジ時間 6,182 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
class Kakko{
	public static void main(String args[]){
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		int k = s.nextInt();
		String str = s.next();
		int count = 0;
		
		if(str.charAt(k - 1) == '('){
			for(int i = k; i < n; i++){
				char a = str.charAt(i);
				if(a == '('){
					count++;
				}else{
					count--;
				}
				if(count == -1){
					System.out.println(i + 1);
					System.exit(0);
				}
			}
		}else{
			for(int i = k - 2; i >= 0; i--){
				char a = str.charAt(i);
				if(a == ')'){
					count++;
				}else{
					count--;
				}        
				if(count == -1){
					System.out.println(i + 1);
					System.exit(0);
				}
			}
		}
	}
}
0