結果

問題 No.2106 Wild Cacco
ユーザー lam6er
提出日時 2025-04-16 15:35:29
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,127 bytes
コンパイル時間 503 ms
コンパイル使用メモリ 81,536 KB
実行使用メモリ 276,140 KB
最終ジャッジ日時 2025-04-16 15:40:23
合計ジャッジ時間 4,726 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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()

current_dp = {(0, 0): 1}

for i in range(n):
    next_dp = {}
    char = s[i]
    possible_chars = ['(', ')', '?'] if char == '.' else [char]
    
    for (min_b, max_b), cnt in current_dp.items():
        for c in possible_chars:
            if c == '(':
                new_min_p = min_b + 1
                new_max_p = max_b + 1
            elif c == ')':
                new_min_p = min_b - 1
                new_max_p = max_b - 1
            else:  # '?'
                new_min_p = min_b - 1
                new_max_p = max_b + 1
            
            new_min = max(new_min_p, 0)
            new_max = new_max_p
            
            if new_max < 0 or new_min > new_max:
                continue
            
            key = (new_min, new_max)
            next_dp[key] = (next_dp.get(key, 0) + cnt) % MOD
    
    current_dp = next_dp
    if not current_dp:
        break  # No possible ways left

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

print(ans)
0