結果

問題 No.22 括弧の対応
ユーザー htensaihtensai
提出日時 2020-02-13 18:57:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 127 ms / 5,000 ms
コード長 954 bytes
コンパイル時間 2,015 ms
コンパイル使用メモリ 72,536 KB
実行使用メモリ 56,428 KB
最終ジャッジ日時 2023-09-27 13:45:20
合計ジャッジ時間 4,734 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
55,968 KB
testcase_01 AC 108 ms
56,224 KB
testcase_02 AC 108 ms
56,208 KB
testcase_03 AC 127 ms
56,428 KB
testcase_04 AC 125 ms
55,760 KB
testcase_05 AC 122 ms
56,032 KB
testcase_06 AC 121 ms
56,048 KB
testcase_07 AC 120 ms
56,200 KB
testcase_08 AC 118 ms
55,784 KB
testcase_09 AC 120 ms
56,116 KB
testcase_10 AC 125 ms
55,616 KB
testcase_11 AC 120 ms
56,408 KB
testcase_12 AC 122 ms
55,752 KB
testcase_13 AC 123 ms
56,056 KB
testcase_14 AC 114 ms
55,860 KB
testcase_15 AC 118 ms
55,812 KB
testcase_16 AC 119 ms
55,768 KB
testcase_17 AC 107 ms
56,240 KB
testcase_18 AC 107 ms
55,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		char[] arr = sc.next().toCharArray();
		int x = -1;
		int current = 0;
		if (arr[k - 1] == '(') {
    		for (int i = 0; i < n; i++) {
    		    if (i + 1 == k) {
    		        x = current;
    		    }
    		    if (arr[i] == '(') {
    		        current++;
    		    } else {
    		        current--;
    		    }
    		    if (current == x) {
    		        System.out.println(i + 1);
    		        return;
    		    }
    		}
		} else {
		    for (int i = n - 1; i >= 0; i--) {
    		    if (i + 1 == k) {
    		        x = current;
    		    }
    		    if (arr[i] == ')') {
    		        current++;
    		    } else {
    		        current--;
    		    }
    		    if (current == x) {
    		        System.out.println(i + 1);
    		        return;
    		    }
		    }
		}
   }
}
0