結果

問題 No.2106 Wild Cacco
ユーザー 👑 ygussanyygussany
提出日時 2022-09-29 22:03:21
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,580 bytes
コンパイル時間 399 ms
コンパイル使用メモリ 68,832 KB
実行使用メモリ 69,672 KB
最終ジャッジ日時 2023-08-28 21:58:04
合計ジャッジ時間 9,236 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
65,708 KB
testcase_01 AC 18 ms
65,680 KB
testcase_02 AC 18 ms
65,680 KB
testcase_03 AC 19 ms
65,748 KB
testcase_04 AC 19 ms
65,712 KB
testcase_05 AC 19 ms
65,756 KB
testcase_06 AC 19 ms
65,700 KB
testcase_07 AC 18 ms
65,684 KB
testcase_08 AC 18 ms
65,656 KB
testcase_09 AC 18 ms
65,736 KB
testcase_10 AC 18 ms
65,688 KB
testcase_11 AC 1,532 ms
65,744 KB
testcase_12 TLE -
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int Mod = 998244353;

int naive(char s[])
{
	int l;
	for (l = 0; s[l] != 0; l++);
	if (l % 2 != 0) return 0;
	
	long long dp[2003][4003] = {}, tmp;
	int i, j, k, ll = l / 2;
	for (k = 0, dp[0][0] = 1; k < l; k++) {
		if (s[k] == '(') {
			for (i = 0; i <= k / 2; i++) {
				for (j = i * 2 + k % 2; j <= k; j += 2) {
					if (dp[i][j] == 0) continue;
					tmp = dp[i][j] % Mod;
					dp[i][j] = 0;

					dp[i][j+1] += tmp;
				}
			}
		} else if (s[k] == ')') {
			for (i = 0; i <= k / 2; i++) {
				for (j = i * 2 + k % 2; j <= k; j += 2) {
					if (dp[i][j] == 0) continue;
					tmp = dp[i][j] % Mod;
					dp[i][j] = 0;
					
					if (j > i * 2) dp[i][j-1] += tmp;
					else if (i > 0) dp[i-1][j-1] += tmp;
				}
			}
		} else if (s[k] == '?') {
			for (i = 0; i <= k / 2; i++) {
				for (j = i * 2 + k % 2; j <= k; j += 2) {
					if (dp[i][j] == 0) continue;
					tmp = dp[i][j] % Mod;
					dp[i][j] = 0;
					
					if (j > i * 2) dp[i+1][j+1] += tmp;
					else dp[i][j+1] += tmp;
				}
			}
		} else {
			for (i = 0; i <= k / 2; i++) {
				for (j = i * 2 + k % 2; j <= k; j += 2) {
					if (dp[i][j] == 0) continue;
					tmp = dp[i][j] % Mod;
					dp[i][j] = 0;
					
					dp[i][j+1] += tmp;
					if (j > i * 2) dp[i][j-1] += tmp;
					else if (i > 0) dp[i-1][j-1] += tmp;
					if (j > i * 2) dp[i+1][j+1] += tmp;
					else dp[i][j+1] += tmp;
				}
			}
		}
	}
	
	long long ans = 0;
	for (i = 0; i <= ll; i++) ans += dp[i][i*2];
	return ans % Mod;
}

int main()
{
	char s[4001];
	scanf("%s", s);
	printf("%d\n", naive(s));
	fflush(stdout);
	return 0;
}
0