結果

問題 No.2031 Colored Brackets
ユーザー tenten
提出日時 2022-08-08 18:07:43
言語 Java
(openjdk 23)
結果
MLE  
実行時間 -
コード長 1,895 bytes
コンパイル時間 2,238 ms
コンパイル使用メモリ 77,712 KB
実行使用メモリ 742,108 KB
最終ジャッジ日時 2024-09-19 03:24:22
合計ジャッジ時間 12,822 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 11 MLE * 19
権限があれば一括ダウンロードができます

ソースコード

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