結果

問題 No.22 括弧の対応
ユーザー kaoetayakaoetaya
提出日時 2016-11-08 01:50:58
言語 Java21
(openjdk 21)
結果
AC  
実行時間 143 ms / 5,000 ms
コード長 644 bytes
コンパイル時間 3,128 ms
コンパイル使用メモリ 72,612 KB
実行使用メモリ 56,352 KB
最終ジャッジ日時 2023-09-27 13:10:39
合計ジャッジ時間 6,730 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,656 KB
testcase_01 AC 123 ms
55,764 KB
testcase_02 AC 124 ms
55,764 KB
testcase_03 AC 139 ms
56,100 KB
testcase_04 AC 138 ms
56,040 KB
testcase_05 AC 139 ms
55,840 KB
testcase_06 AC 141 ms
56,016 KB
testcase_07 AC 140 ms
56,352 KB
testcase_08 AC 139 ms
55,868 KB
testcase_09 AC 138 ms
55,788 KB
testcase_10 AC 139 ms
56,012 KB
testcase_11 AC 136 ms
55,784 KB
testcase_12 AC 137 ms
56,204 KB
testcase_13 AC 143 ms
56,096 KB
testcase_14 AC 131 ms
56,136 KB
testcase_15 AC 141 ms
56,124 KB
testcase_16 AC 142 ms
55,832 KB
testcase_17 AC 124 ms
56,248 KB
testcase_18 AC 122 ms
55,960 KB
権限があれば一括ダウンロードができます

ソースコード

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