結果

問題 No.2031 Colored Brackets
ユーザー tentententen
提出日時 2023-02-07 17:10:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,525 ms / 2,000 ms
コード長 3,075 bytes
コンパイル時間 2,566 ms
コンパイル使用メモリ 81,780 KB
実行使用メモリ 70,432 KB
最終ジャッジ日時 2023-09-18 23:56:49
合計ジャッジ時間 22,050 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,888 KB
testcase_01 AC 41 ms
49,528 KB
testcase_02 AC 41 ms
49,604 KB
testcase_03 AC 41 ms
49,920 KB
testcase_04 AC 309 ms
58,016 KB
testcase_05 AC 153 ms
55,608 KB
testcase_06 AC 203 ms
55,268 KB
testcase_07 AC 41 ms
49,356 KB
testcase_08 AC 547 ms
62,376 KB
testcase_09 AC 824 ms
66,532 KB
testcase_10 AC 1,246 ms
66,780 KB
testcase_11 AC 768 ms
66,536 KB
testcase_12 AC 1,376 ms
70,248 KB
testcase_13 AC 482 ms
62,356 KB
testcase_14 AC 1,464 ms
70,140 KB
testcase_15 AC 1,429 ms
70,088 KB
testcase_16 AC 1,395 ms
70,280 KB
testcase_17 AC 1,481 ms
70,136 KB
testcase_18 AC 1,525 ms
70,432 KB
testcase_19 AC 991 ms
66,696 KB
testcase_20 AC 544 ms
63,132 KB
testcase_21 AC 575 ms
62,660 KB
testcase_22 AC 668 ms
62,536 KB
testcase_23 AC 1,325 ms
67,892 KB
testcase_24 AC 41 ms
49,564 KB
testcase_25 AC 40 ms
49,660 KB
testcase_26 AC 40 ms
49,604 KB
testcase_27 AC 41 ms
49,556 KB
testcase_28 AC 42 ms
49,400 KB
testcase_29 AC 40 ms
49,724 KB
testcase_30 AC 40 ms
49,516 KB
testcase_31 AC 63 ms
51,340 KB
testcase_32 AC 40 ms
49,408 KB
testcase_33 AC 64 ms
50,908 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[][] current = new int[n / 2 + 1][n / 2 + 1];
        int[][] next = new int[n / 2 + 1][n / 2 + 1];
        current[1][0] = 1;
        current[0][1] = 1;
        for (int i = 1; i < n; i++) {
            for (int j = 0; j <= maxes[i - 1]; j++) {
                if (s[i] == '(') {
                    next[j + 1][maxes[i - 1] - j] += current[j][maxes[i - 1] - j];
                    next[j + 1][maxes[i - 1] - j] %= MOD;
                    next[j][maxes[i - 1] - j + 1] += current[j][maxes[i - 1] - j];
                    next[j][maxes[i - 1] - j + 1] %= MOD;
                } else {
                    if (j > 0) {
                        next[j - 1][maxes[i - 1] - j] += current[j][maxes[i - 1] - j];
                        next[j - 1][maxes[i - 1] - j] %= MOD;
                    }
                    if (maxes[i - 1] - j > 0) {
                        next[j][maxes[i - 1] - j - 1] += current[j][maxes[i - 1] - j];
                        next[j][maxes[i - 1] - j - 1] %= MOD;
                    }
                }
            }
            int[][] tmp = current;
            current = next;
            next = tmp;
            for (int[] arr : next) {
                Arrays.fill(arr, 0);
            }
        }
        System.out.println(current[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