結果

問題 No.592 括弧の対応 (2)
ユーザー neko_the_shadow
提出日時 2019-05-20 16:33:08
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,148 ms / 5,000 ms
コード長 748 bytes
コンパイル時間 2,612 ms
コンパイル使用メモリ 81,000 KB
実行使用メモリ 59,564 KB
最終ジャッジ日時 2024-09-17 06:39:42
合計ジャッジ時間 6,775 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 3
権限があれば一括ダウンロードができます

ソースコード

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