結果

問題 No.2031 Colored Brackets
ユーザー tentententen
提出日時 2023-02-07 17:00:46
言語 Java21
(openjdk 21)
結果
MLE  
実行時間 -
コード長 2,930 bytes
コンパイル時間 4,456 ms
コンパイル使用メモリ 86,152 KB
実行使用メモリ 747,232 KB
最終ジャッジ日時 2023-09-18 23:49:04
合計ジャッジ時間 17,286 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
49,500 KB
testcase_01 AC 41 ms
49,436 KB
testcase_02 AC 40 ms
49,628 KB
testcase_03 AC 41 ms
49,416 KB
testcase_04 AC 546 ms
471,624 KB
testcase_05 AC 169 ms
132,324 KB
testcase_06 AC 293 ms
236,600 KB
testcase_07 AC 41 ms
49,468 KB
testcase_08 MLE -
testcase_09 MLE -
testcase_10 MLE -
testcase_11 MLE -
testcase_12 MLE -
testcase_13 MLE -
testcase_14 MLE -
testcase_15 MLE -
testcase_16 MLE -
testcase_17 MLE -
testcase_18 AC 44 ms
49,496 KB
testcase_19 AC 44 ms
49,408 KB
testcase_20 AC 43 ms
49,428 KB
testcase_21 AC 44 ms
51,888 KB
testcase_22 AC 43 ms
49,480 KB
testcase_23 AC 43 ms
49,612 KB
testcase_24 AC 41 ms
49,840 KB
testcase_25 AC 40 ms
49,496 KB
testcase_26 AC 40 ms
49,764 KB
testcase_27 AC 40 ms
49,956 KB
testcase_28 AC 41 ms
49,492 KB
testcase_29 AC 41 ms
49,540 KB
testcase_30 AC 41 ms
49,400 KB
testcase_31 AC 48 ms
54,016 KB
testcase_32 AC 41 ms
49,628 KB
testcase_33 AC 46 ms
51,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static final int MOD = 998244353;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        char[] s = sc.next().toCharArray();
        int[] maxes = new int[n];
        maxes[0] = 1;
        for (int i = 1; i < s.length; i++) {
            if (s[i] == '(') {
                maxes[i] = maxes[i - 1] + 1;
            } else {
                maxes[i] = maxes[i - 1] - 1;
            }
        }
        int[][][] dp = new int[n][][];
        dp[0] = new int[2][2];
        dp[0][1][0] = 1;
        dp[0][0][1] = 1;
        for (int i = 1; i < n; i++) {
            dp[i] = new int[maxes[i] + 1][maxes[i] + 1];
            for (int j = 0; j <= maxes[i - 1]; j++) {
                if (s[i] == '(') {
                    dp[i][j + 1][maxes[i - 1] - j] += dp[i - 1][j][maxes[i - 1] - j];
                    dp[i][j + 1][maxes[i - 1] - j] %= MOD;
                    dp[i][j][maxes[i - 1] - j + 1] += dp[i - 1][j][maxes[i - 1] - j];
                    dp[i][j][maxes[i - 1] - j + 1] %= MOD;
                } else {
                    if (j > 0) {
                        dp[i][j - 1][maxes[i - 1] - j] += dp[i - 1][j][maxes[i - 1] - j];
                        dp[i][j - 1][maxes[i - 1] - j] %= MOD;
                    }
                    if (maxes[i - 1] - j > 0) {
                        dp[i][j][maxes[i - 1] - j - 1] += dp[i - 1][j][maxes[i - 1] - j];
                        dp[i][j][maxes[i - 1] - j - 1] %= MOD;
                    }
                }
            }
        }
        System.out.println(dp[n - 1][0][0]);
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0