結果

問題 No.2106 Wild Cacco
ユーザー gew1fw
提出日時 2025-06-12 15:56:37
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,043 bytes
コンパイル時間 187 ms
コンパイル使用メモリ 82,452 KB
実行使用メモリ 280,244 KB
最終ジャッジ日時 2025-06-12 15:56:42
合計ジャッジ時間 4,796 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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 char in s:
    new_dp = defaultdict(int)
    for (min_prev, max_prev), cnt in dp.items():
        chars = []
        if char == '.':
            chars = ['(', ')', '?']
        else:
            chars = [char]
        for c in chars:
            if c == '(':
                new_min = min_prev + 1
                new_max = max_prev + 1
            elif c == ')':
                new_min = min_prev - 1
                new_max = max_prev - 1
            else:  # '?'
                new_min = min_prev - 1
                new_max = max_prev + 1
            new_min = max(new_min, 0)
            if new_max < 0:
                continue
            new_dp[(new_min, new_max)] = (new_dp[(new_min, new_max)] + cnt) % MOD
    dp = new_dp

total = 0
for (min_bal, max_bal), cnt in dp.items():
    if min_bal <= 0 <= max_bal:
        total = (total + cnt) % MOD

print(total % MOD)
0