結果

問題 No.22 括弧の対応
ユーザー takeya_okinotakeya_okino
提出日時 2017-06-11 22:41:52
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 765 bytes
コンパイル時間 2,121 ms
コンパイル使用メモリ 74,872 KB
実行使用メモリ 57,724 KB
最終ジャッジ日時 2023-10-24 21:24:58
合計ジャッジ時間 5,729 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
56,040 KB
testcase_01 AC 133 ms
57,340 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 150 ms
57,304 KB
testcase_07 AC 150 ms
57,492 KB
testcase_08 AC 150 ms
57,556 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 148 ms
55,452 KB
testcase_13 AC 149 ms
55,472 KB
testcase_14 WA -
testcase_15 AC 149 ms
57,500 KB
testcase_16 AC 152 ms
57,556 KB
testcase_17 AC 132 ms
57,496 KB
testcase_18 AC 134 ms
57,228 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[] coresspond = new int[N];
    int a = 0;
    int b = N - 1;
    while(a < b) {
      boolean flg = true;
      if(s.charAt(a + 1) == ')') {
        coresspond[a] = a + 1;
        coresspond[a + 1] = a;
        a += 2;
        flg = false;
      } 
      if(s.charAt(b - 1) == '(') {
        coresspond[b] = b - 1;
        coresspond[b - 1] = b;
        b -= 2;
        flg = false;
      }
      if(flg) {
        coresspond[a] = b;
        coresspond[b] = a;
        a++;
        b--;
      } 
    }
    System.out.println(coresspond[K - 1] + 1);
  }
}
0