結果

問題 No.22 括弧の対応
ユーザー lrf141lrf141
提出日時 2016-11-27 10:15:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 131 ms / 5,000 ms
コード長 639 bytes
コンパイル時間 3,127 ms
コンパイル使用メモリ 72,200 KB
実行使用メモリ 56,584 KB
最終ジャッジ日時 2023-09-27 13:10:47
合計ジャッジ時間 6,079 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
55,988 KB
testcase_01 AC 111 ms
56,036 KB
testcase_02 AC 112 ms
55,656 KB
testcase_03 AC 126 ms
55,608 KB
testcase_04 AC 127 ms
55,880 KB
testcase_05 AC 124 ms
55,932 KB
testcase_06 AC 127 ms
56,584 KB
testcase_07 AC 125 ms
56,312 KB
testcase_08 AC 124 ms
56,160 KB
testcase_09 AC 124 ms
55,864 KB
testcase_10 AC 128 ms
56,216 KB
testcase_11 AC 126 ms
56,112 KB
testcase_12 AC 128 ms
55,900 KB
testcase_13 AC 128 ms
55,844 KB
testcase_14 AC 115 ms
55,652 KB
testcase_15 AC 127 ms
56,112 KB
testcase_16 AC 131 ms
55,648 KB
testcase_17 AC 113 ms
56,156 KB
testcase_18 AC 114 ms
55,904 KB
権限があれば一括ダウンロードができます

ソースコード

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