結果
| 問題 |
No.2031 Colored Brackets
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2022-08-08 18:18:07 |
| 言語 | Java (openjdk 23) |
| 結果 |
TLE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 2,366 bytes |
| コンパイル時間 | 2,060 ms |
| コンパイル使用メモリ | 79,232 KB |
| 実行使用メモリ | 374,744 KB |
| 最終ジャッジ日時 | 2024-09-19 03:35:05 |
| 合計ジャッジ時間 | 14,884 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 29 TLE * 1 |
ソースコード
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();
}
}
tenten