結果

問題 No.22 括弧の対応
ユーザー bigbear90560bigbear90560
提出日時 2015-09-17 18:21:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 139 ms / 5,000 ms
コード長 933 bytes
コンパイル時間 2,977 ms
コンパイル使用メモリ 75,620 KB
実行使用メモリ 56,208 KB
最終ジャッジ日時 2023-09-27 12:56:59
合計ジャッジ時間 6,281 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
55,912 KB
testcase_01 AC 116 ms
56,096 KB
testcase_02 AC 116 ms
55,752 KB
testcase_03 AC 139 ms
53,680 KB
testcase_04 AC 131 ms
55,860 KB
testcase_05 AC 130 ms
55,780 KB
testcase_06 AC 128 ms
56,044 KB
testcase_07 AC 126 ms
55,872 KB
testcase_08 AC 127 ms
55,912 KB
testcase_09 AC 131 ms
56,068 KB
testcase_10 AC 129 ms
56,064 KB
testcase_11 AC 126 ms
55,712 KB
testcase_12 AC 128 ms
55,840 KB
testcase_13 AC 126 ms
55,872 KB
testcase_14 AC 120 ms
55,440 KB
testcase_15 AC 128 ms
55,984 KB
testcase_16 AC 134 ms
56,208 KB
testcase_17 AC 113 ms
55,464 KB
testcase_18 AC 114 ms
55,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class No022_SupprotBrackets {

	public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();	// 文字数
        int b = sc.nextInt();	// 対象番号
        String s = sc.next();	// 文字列

        final String LB = "(";
        final String RB = ")";

        int cnt = 0;

        if (LB.equals(s.substring(b-1,b))) {
	        for (int i=b; i<a; i++) {
	        	if (LB.equals(s.substring(i, i+1))) cnt++;
	        	if (RB.equals(s.substring(i, i+1))) cnt--;
	        	if (cnt < 0) {
	        		System.out.println(i+1);
	        		break;
	        	}
	        }
        } else {
	        for (int i=b; i>0; i--) {
	        	if (LB.equals(s.substring(i-1, i))) cnt--;
	        	if (RB.equals(s.substring(i-1, i))) cnt++;
	        	if (cnt <= 0) {
	        		System.out.println(i);
	        		break;
	        	}
	        }
        }
	}

}
0