結果

問題 No.592 括弧の対応 (2)
ユーザー tentententen
提出日時 2022-10-06 18:40:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 223 ms / 5,000 ms
コード長 1,475 bytes
コンパイル時間 5,171 ms
コンパイル使用メモリ 74,872 KB
実行使用メモリ 60,508 KB
最終ジャッジ日時 2023-09-01 23:32:37
合計ジャッジ時間 3,877 ms
ジャッジサーバーID
(参考情報)
judge11 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,332 KB
testcase_01 AC 223 ms
59,096 KB
testcase_02 AC 216 ms
60,508 KB
testcase_03 AC 202 ms
58,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        char[] values = sc.next().toCharArray();
        ArrayDeque<Integer> stack = new ArrayDeque<>();
        int[] ans = new int[n];
        for (int i = 0; i < n; i++) {
            if (values[i] == '(') {
                stack.push(i);
            } else {
                int x = stack.pop();
                ans[i] = x + 1;
                ans[x] = i + 1;
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int x : ans) {
            sb.append(x).append("\n");
        }
        System.out.print(sb);
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0