結果

問題 No.2106 Wild Cacco
ユーザー lam6er
提出日時 2025-04-16 15:30:58
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,221 bytes
コンパイル時間 184 ms
コンパイル使用メモリ 82,140 KB
実行使用メモリ 77,024 KB
最終ジャッジ日時 2025-04-16 15:35:33
合計ジャッジ時間 4,947 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 7 TLE * 1 -- * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353

s = input().strip()
n = len(s)
if n % 2 != 0:
    print(0)
    exit()

from collections import defaultdict

dp = defaultdict(int)
dp[(0, 0)] = 1

for c in s:
    new_dp = defaultdict(int)
    for (min_prev, max_prev), cnt in dp.items():
        possible_chars = []
        if c == '.':
            possible_chars = ['(', ')', '?']
        else:
            possible_chars = [c]
        for char in possible_chars:
            if char == '(':
                new_min = min_prev + 1
                new_max = max_prev + 1
            elif char == ')':
                new_min = max(min_prev - 1, 0)
                new_max = max_prev - 1
                if new_max < 0:
                    continue
            elif char == '?':
                new_min = max(min_prev - 1, 0)
                new_max = max_prev + 1
            else:
                assert False, "Invalid character"
            if new_min > new_max:
                continue
            new_dp[(new_min, new_max)] = (new_dp[(new_min, new_max)] + cnt) % MOD
    dp = new_dp
    if not dp:
        break

result = 0
for (min_b, max_b), cnt in dp.items():
    if min_b <= 0 <= max_b:
        result = (result + cnt) % MOD

print(result % MOD)
0