結果

問題 No.1677 mæx
ユーザー tktk_snsntktk_snsn
提出日時 2021-09-10 22:46:08
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 441 ms / 2,000 ms
コード長 1,064 bytes
コンパイル時間 97 ms
コンパイル使用メモリ 11,008 KB
実行使用メモリ 22,676 KB
最終ジャッジ日時 2023-09-02 19:50:46
合計ジャッジ時間 7,172 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,628 KB
testcase_01 AC 21 ms
8,748 KB
testcase_02 AC 21 ms
8,612 KB
testcase_03 AC 21 ms
8,616 KB
testcase_04 AC 333 ms
19,208 KB
testcase_05 AC 332 ms
19,244 KB
testcase_06 AC 320 ms
19,132 KB
testcase_07 AC 323 ms
19,152 KB
testcase_08 AC 325 ms
19,348 KB
testcase_09 AC 322 ms
19,348 KB
testcase_10 AC 323 ms
19,324 KB
testcase_11 AC 327 ms
19,436 KB
testcase_12 AC 325 ms
19,320 KB
testcase_13 AC 323 ms
19,292 KB
testcase_14 AC 437 ms
19,416 KB
testcase_15 AC 436 ms
19,308 KB
testcase_16 AC 431 ms
19,268 KB
testcase_17 AC 441 ms
19,216 KB
testcase_18 AC 436 ms
19,372 KB
testcase_19 AC 19 ms
8,560 KB
testcase_20 AC 19 ms
8,624 KB
testcase_21 AC 440 ms
22,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 7)
mod = 998244353
S = input()
N = len(S)
K = int(input())

next = [-1] * N
stack = []
for i, s in enumerate(S):
    if s == "(":
        stack.append(i)
    elif s == ",":
        next[stack[-1]] = i
    elif s == ")":
        stack.pop()


def mex(a, b):
    for i in range(3):
        if not i in [a, b]:
            return i


def rec(L, R):
    if L + 1 >= R:
        if S[L] == "?":
            return [1, 1, 1]
        res = [0, 0, 0]
        res[int(S[L])] += 1
        return res

    R -= 1
    L += 1
    oper = []
    if S[L] == "?" or S[L] == "a":
        oper.append(max)
    if S[L] == "?" or S[L] == "e":
        oper.append(mex)
    L += 2

    M = next[L]
    A = rec(L+1, M)
    B = rec(M+1, R)
    res = [0, 0, 0]
    for op in oper:
        for i, a in enumerate(A):
            for j, b in enumerate(B):
                idx = op(i, j)
                res[idx] += a * b % mod
                res[idx] %= mod
    return res


ans = rec(0, N)[K]
print(ans)
0