結果

問題 No.2031 Colored Brackets
ユーザー ks2mks2m
提出日時 2022-08-05 22:44:03
言語 Java21
(openjdk 21)
結果
AC  
実行時間 205 ms / 2,000 ms
コード長 1,041 bytes
コンパイル時間 2,042 ms
コンパイル使用メモリ 74,172 KB
実行使用メモリ 132,396 KB
最終ジャッジ日時 2023-10-14 00:17:15
合計ジャッジ時間 7,356 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,396 KB
testcase_01 AC 43 ms
49,184 KB
testcase_02 AC 44 ms
49,232 KB
testcase_03 AC 44 ms
49,308 KB
testcase_04 AC 110 ms
77,084 KB
testcase_05 AC 78 ms
60,768 KB
testcase_06 AC 90 ms
64,888 KB
testcase_07 AC 44 ms
49,660 KB
testcase_08 AC 141 ms
87,968 KB
testcase_09 AC 163 ms
115,548 KB
testcase_10 AC 188 ms
132,396 KB
testcase_11 AC 167 ms
115,936 KB
testcase_12 AC 187 ms
132,084 KB
testcase_13 AC 124 ms
86,128 KB
testcase_14 AC 190 ms
132,288 KB
testcase_15 AC 191 ms
130,828 KB
testcase_16 AC 171 ms
131,740 KB
testcase_17 AC 205 ms
131,748 KB
testcase_18 AC 110 ms
129,152 KB
testcase_19 AC 88 ms
112,432 KB
testcase_20 AC 71 ms
83,072 KB
testcase_21 AC 76 ms
85,520 KB
testcase_22 AC 79 ms
89,592 KB
testcase_23 AC 105 ms
130,028 KB
testcase_24 AC 43 ms
49,272 KB
testcase_25 AC 44 ms
49,432 KB
testcase_26 AC 42 ms
49,392 KB
testcase_27 AC 42 ms
49,300 KB
testcase_28 AC 43 ms
49,220 KB
testcase_29 AC 43 ms
49,204 KB
testcase_30 AC 43 ms
49,328 KB
testcase_31 AC 44 ms
49,312 KB
testcase_32 AC 43 ms
49,156 KB
testcase_33 AC 45 ms
49,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		char[] s = br.readLine().toCharArray();
		br.close();

		int mod = 998244353;
		int[] a = new int[n + 1];
		for (int i = 0; i < n; i++) {
			if (s[i] == '(') {
				a[i + 1] = a[i] + 1;
			} else {
				a[i + 1] = a[i] - 1;
			}
		}

		long[][] dp = new long[n + 1][n + 1];
		dp[0][0] = 1;
		for (int i = 0; i < n; i++) {
			if (s[i] == '(') {
				for (int j = 0; j <= a[i]; j++) {
					dp[i + 1][j + 1] += dp[i][j];
					dp[i + 1][j + 1] %= mod;
					dp[i + 1][j] += dp[i][j];
					dp[i + 1][j] %= mod;
				}
			} else {
				for (int j = 1; j <= a[i]; j++) {
					dp[i + 1][j - 1] += dp[i][j];
					dp[i + 1][j - 1] %= mod;
				}
				for (int j = 0; j < a[i]; j++) {
					dp[i + 1][j] += dp[i][j];
					dp[i + 1][j] %= mod;
				}
			}
		}
		System.out.println(dp[n][0]);
	}
}
0