結果

問題 No.592 括弧の対応 (2)
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-05-20 16:33:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,096 ms / 5,000 ms
コード長 748 bytes
コンパイル時間 2,618 ms
コンパイル使用メモリ 81,612 KB
実行使用メモリ 74,500 KB
最終ジャッジ日時 2023-10-17 08:06:21
合計ジャッジ時間 7,258 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 137 ms
57,524 KB
testcase_01 AC 1,096 ms
74,500 KB
testcase_02 AC 988 ms
70,868 KB
testcase_03 AC 1,067 ms
71,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        int n = stdin.nextInt();
        String s = stdin.next();
        
        Deque<Integer> stack = new ArrayDeque<>();
        int[] ans = new int[n];
        for (int i = 0; i < n; i++) {
            char ch = s.charAt(i);
            if (ch == '(') {
                stack.addFirst(i);
            } else {
                int j = stack.removeFirst();
                ans[i] = j;
                ans[j] = i;
            }
        }
        
        Arrays.stream(ans).map(i -> i + 1).forEach(System.out::println);
    }
}
0