結果

問題 No.2031 Colored Brackets
ユーザー tentententen
提出日時 2022-08-08 18:07:43
言語 Java21
(openjdk 21)
結果
MLE  
実行時間 -
コード長 1,895 bytes
コンパイル時間 4,374 ms
コンパイル使用メモリ 78,012 KB
実行使用メモリ 745,188 KB
最終ジャッジ日時 2023-10-19 07:04:54
合計ジャッジ時間 28,192 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
53,460 KB
testcase_01 AC 57 ms
53,448 KB
testcase_02 AC 56 ms
51,400 KB
testcase_03 AC 58 ms
52,376 KB
testcase_04 MLE -
testcase_05 MLE -
testcase_06 MLE -
testcase_07 AC 58 ms
53,432 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 MLE -
testcase_19 MLE -
testcase_20 MLE -
testcase_21 MLE -
testcase_22 MLE -
testcase_23 MLE -
testcase_24 AC 56 ms
53,428 KB
testcase_25 AC 58 ms
52,452 KB
testcase_26 AC 58 ms
51,380 KB
testcase_27 AC 57 ms
52,308 KB
testcase_28 AC 59 ms
53,440 KB
testcase_29 AC 59 ms
53,432 KB
testcase_30 AC 57 ms
52,312 KB
testcase_31 AC 145 ms
77,880 KB
testcase_32 AC 58 ms
52,324 KB
testcase_33 AC 145 ms
78,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static final int MOD = 998244353;
    static int[][][] dp;
    static char[] parens;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        parens = sc.next().toCharArray();
        dp = new int[n][n / 2 + 1][n / 2 + 1];
        for (int[][] arr1 : dp) {
            for (int[] arr2 : arr1) {
                Arrays.fill(arr2, -1);
            }
        }
        System.out.println(dfw(n - 1, 0, 0));
    }
    
    static int dfw(int idx, int red, int blue) {
        if (red < 0 || blue < 0) {
            return 0;
        }
        if (idx < 0) {
            if (red == 0 && blue == 0) {
                return 1;
            } else {
                return 0;
            }
        }
        if (dp[idx][red][blue] < 0) {
            if (parens[idx] == '(') {
                dp[idx][red][blue] = (dfw(idx - 1, red - 1, blue) + dfw(idx - 1, red, blue - 1)) % MOD;
            } else {
                dp[idx][red][blue] = (dfw(idx - 1, red + 1, blue) + dfw(idx - 1, red, blue + 1)) % MOD;
            }
        }
        return dp[idx][red][blue];
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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