結果

問題 No.1677 mæx
ユーザー convexineqconvexineq
提出日時 2021-09-10 22:48:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 213 ms / 2,000 ms
コード長 2,613 bytes
コンパイル時間 487 ms
コンパイル使用メモリ 87,024 KB
実行使用メモリ 115,556 KB
最終ジャッジ日時 2023-09-02 20:02:28
合計ジャッジ時間 5,466 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,064 KB
testcase_01 AC 77 ms
71,408 KB
testcase_02 AC 74 ms
71,496 KB
testcase_03 AC 73 ms
71,468 KB
testcase_04 AC 213 ms
115,556 KB
testcase_05 AC 203 ms
115,256 KB
testcase_06 AC 204 ms
115,216 KB
testcase_07 AC 207 ms
115,512 KB
testcase_08 AC 203 ms
115,516 KB
testcase_09 AC 207 ms
115,552 KB
testcase_10 AC 208 ms
115,092 KB
testcase_11 AC 206 ms
115,512 KB
testcase_12 AC 208 ms
115,500 KB
testcase_13 AC 208 ms
115,492 KB
testcase_14 AC 202 ms
115,248 KB
testcase_15 AC 202 ms
115,036 KB
testcase_16 AC 201 ms
115,368 KB
testcase_17 AC 201 ms
115,420 KB
testcase_18 AC 200 ms
115,388 KB
testcase_19 AC 73 ms
71,360 KB
testcase_20 AC 74 ms
71,288 KB
testcase_21 AC 197 ms
115,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def Cartesian_tree(A):
    n = len(A)
    par = [-1]*n
    order = []
    for i,ai in enumerate(A):
        cur = i-1
        pre = -1
        while cur != -1 and A[cur] > ai:
            order.append(cur)
            cur, pre = par[cur], cur
        par[i] = cur
        if pre != -1:
            par[pre] = i
    cur = n-1
    while cur != -1:
        order.append(cur)
        cur = par[cur]
    return par, order

def find_root_and_children(par):
    n = len(par)
    left_child = [-1]*n
    right_child = [-1]*n
    for v,p in enumerate(par):
        if p != -1:
            if v < p:
                left_child[p] = v
            else:
                right_child[p] = v
        else:
            root = v
    return root, left_child, right_child

def getLR(a,par,order):
    n = len(a)
    L = list(range(n))
    R = list(range(n))
    for v in order[:-1]:
        p = par[v]
        L[p] = min(L[p],L[v])
        R[p] = max(R[p],R[v])
    return L,R

s = input()
n = len(s)
k = int(input())
if len(s) == 1:
    if s == "?":
        ans = 1
    else:
        ans = int(k==int(s))
    print(ans)
    exit()


INF = 1<<30
depth = [1<<30]*n
cnt = 0
for i in range(n):
    si = s[i]
    if si == "(":
        cnt += 1
    elif si == ")":
        cnt -= 1
    elif si == ",":
        depth[i] = cnt

par,order = Cartesian_tree(depth)
root, left_child, right_child = find_root_and_children(par)
L,R = getLR(depth,par,order)
dp = [[0]*3 for _ in range(n)]
op = [""]*n

q = [(0,n,root)]
while q:
    L,R,idx = q.pop()
    #print(L,R,idx)
    if L+1 == R:
        if s[L] == "?":
            dp[idx] = [1]*3
        else:
            dp[idx] = [0]*3
            dp[idx][int(s[L])] = 1
        continue
    elif depth[idx] == INF:
        continue
    op[idx] = s[L+1]
    
    lc = left_child[idx]
    rc = right_child[idx]
    L += 4
    R -= 1
    if lc != -1:
        q.append((L,idx,lc))
    if rc != -1:
        q.append((idx+1,R,rc))

def mex(i,j):
    if 0 not in [i,j]: return 0
    if 1 not in [i,j]: return 1
    return 2
MEX = [[mex(i,j) for i in range(3)] for j in range(3)]

#print(dp)

MOD = 998244353
for v in order:
    if depth[v] == INF: continue
    #print(v,"v")
    a = dp[left_child[v]]
    b = dp[right_child[v]]
    res = dp[v]
    if op[v] != "e":
        for i in range(3):
            for j in range(3):
                res[max(i,j)] += a[i]*b[j]
                res[max(i,j)] %= MOD
    if op[v] != "a":
        for i in range(3):
            for j in range(3):
                res[MEX[i][j]] += a[i]*b[j]
                res[MEX[i][j]] %= MOD

#print(dp[root])
print(dp[root][k]%MOD)


0