結果

問題 No.22 括弧の対応
ユーザー takeya_okinotakeya_okino
提出日時 2017-06-11 22:51:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 141 ms / 5,000 ms
コード長 755 bytes
コンパイル時間 1,943 ms
コンパイル使用メモリ 71,180 KB
実行使用メモリ 56,328 KB
最終ジャッジ日時 2023-09-27 13:16:33
合計ジャッジ時間 5,548 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,744 KB
testcase_01 AC 125 ms
55,608 KB
testcase_02 AC 126 ms
55,820 KB
testcase_03 AC 141 ms
55,688 KB
testcase_04 AC 139 ms
55,612 KB
testcase_05 AC 139 ms
55,448 KB
testcase_06 AC 139 ms
55,920 KB
testcase_07 AC 140 ms
55,896 KB
testcase_08 AC 138 ms
55,736 KB
testcase_09 AC 137 ms
55,620 KB
testcase_10 AC 134 ms
55,916 KB
testcase_11 AC 139 ms
55,520 KB
testcase_12 AC 136 ms
55,532 KB
testcase_13 AC 139 ms
55,708 KB
testcase_14 AC 128 ms
55,768 KB
testcase_15 AC 141 ms
55,812 KB
testcase_16 AC 141 ms
55,700 KB
testcase_17 AC 125 ms
56,328 KB
testcase_18 AC 123 ms
55,828 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();
    String s = sc.next();
    int ans = 0;
    if(s.charAt(K - 1) == '(') {
      int p = 1;
      for(int i = K; i < N; i++) {
        if(s.charAt(i) == '(') {
          p++;
        } else {
          p--;
        }
        if(p == 0) {
          ans = i + 1;
          break;
        }
      }
    } else {
      int p = -1;
      for(int i = K - 2; i >= 0; i--) {
        if(s.charAt(i) == '(') {
          p++;
        } else {
          p--;
        }
        if(p == 0) {
          ans = i + 1;
          break;
        }
      }
    }
    System.out.println(ans);
  }
}
0