結果

問題 No.2031 Colored Brackets
ユーザー tentententen
提出日時 2022-08-08 18:18:07
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,366 bytes
コンパイル時間 2,340 ms
コンパイル使用メモリ 79,512 KB
実行使用メモリ 377,384 KB
最終ジャッジ日時 2023-10-19 07:12:03
合計ジャッジ時間 17,294 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
53,512 KB
testcase_01 AC 57 ms
53,512 KB
testcase_02 AC 55 ms
53,512 KB
testcase_03 AC 59 ms
53,544 KB
testcase_04 AC 375 ms
99,688 KB
testcase_05 AC 209 ms
68,736 KB
testcase_06 AC 342 ms
85,892 KB
testcase_07 AC 57 ms
52,404 KB
testcase_08 AC 564 ms
125,584 KB
testcase_09 AC 782 ms
153,564 KB
testcase_10 AC 1,160 ms
213,328 KB
testcase_11 AC 859 ms
166,016 KB
testcase_12 AC 1,169 ms
211,096 KB
testcase_13 AC 555 ms
126,340 KB
testcase_14 AC 1,161 ms
208,984 KB
testcase_15 AC 1,149 ms
210,552 KB
testcase_16 AC 1,157 ms
210,512 KB
testcase_17 TLE -
testcase_18 AC 207 ms
56,108 KB
testcase_19 AC 92 ms
56,016 KB
testcase_20 AC 89 ms
54,668 KB
testcase_21 AC 87 ms
54,640 KB
testcase_22 AC 78 ms
54,944 KB
testcase_23 AC 95 ms
56,568 KB
testcase_24 AC 57 ms
53,504 KB
testcase_25 AC 58 ms
52,436 KB
testcase_26 AC 58 ms
52,444 KB
testcase_27 AC 57 ms
53,508 KB
testcase_28 AC 56 ms
53,512 KB
testcase_29 AC 57 ms
53,500 KB
testcase_30 AC 57 ms
52,444 KB
testcase_31 AC 97 ms
57,804 KB
testcase_32 AC 57 ms
53,512 KB
testcase_33 AC 103 ms
56,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static final int MOD = 998244353;
    static ArrayList<HashMap<Integer, HashMap<Integer, Integer>>> dp = new ArrayList<>();
    static char[] parens;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        parens = sc.next().toCharArray();
        for (int i = 0; i < n; i++) {
            dp.add(new HashMap<>());
        }
        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.get(idx).containsKey(red)) {
            dp.get(idx).put(red, new HashMap<>());
        }
        if (!dp.get(idx).get(red).containsKey(blue)) {
            if (parens[idx] == '(') {
                if (red == blue) {
                    dp.get(idx).get(red).put(blue, dfw(idx - 1, red - 1, blue) * 2 % MOD);
                } else {
                    dp.get(idx).get(red).put(blue, (dfw(idx - 1, red - 1, blue) + dfw(idx - 1, red, blue - 1)) % MOD);
                }
            } else {
                if (red == blue) {
                    dp.get(idx).get(red).put(blue, dfw(idx - 1, red, blue + 1) * 2 % MOD);
                } else {
                    dp.get(idx).get(red).put(blue, (dfw(idx - 1, red + 1, blue) + dfw(idx - 1, red, blue + 1)) % MOD);
                }
            }
        }
        return dp.get(idx).get(red).get(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