結果

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

ソースコード

diff #

MOD = 998244353

s = input().strip()

from collections import defaultdict

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

for c in s:
    new_dp = defaultdict(int)
    for (prev_min, prev_max), cnt in dp.items():
        if c == '.':
            # Replace with '('
            new_min = prev_min + 1
            new_max = prev_max + 1
            new_min_clamped = new_min
            new_max_clamped = new_max
            if new_max_clamped >= 0:
                new_dp[(new_min_clamped, new_max_clamped)] = (new_dp[(new_min_clamped, new_max_clamped)] + cnt) % MOD
            
            # Replace with ')'
            new_min = prev_min - 1
            new_max = prev_max - 1
            new_min_clamped = max(new_min, 0)
            new_max_clamped = new_max
            if new_max_clamped >= 0:
                new_dp[(new_min_clamped, new_max_clamped)] = (new_dp[(new_min_clamped, new_max_clamped)] + cnt) % MOD
            
            # Replace with '?'
            new_min = prev_min - 1
            new_max = prev_max + 1
            new_min_clamped = max(new_min, 0)
            new_max_clamped = new_max
            if new_max_clamped >= 0:
                new_dp[(new_min_clamped, new_max_clamped)] = (new_dp[(new_min_clamped, new_max_clamped)] + cnt) % MOD
        else:
            if c == '(':
                new_min = prev_min + 1
                new_max = prev_max + 1
            elif c == ')':
                new_min = prev_min - 1
                new_max = prev_max - 1
            else:  # '?'
                new_min = prev_min - 1
                new_max = prev_max + 1
            
            new_min_clamped = max(new_min, 0)
            new_max_clamped = new_max
            if new_max_clamped >= 0:
                new_dp[(new_min_clamped, new_max_clamped)] = (new_dp[(new_min_clamped, new_max_clamped)] + cnt) % MOD
    dp = new_dp

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

print(result)
0