結果

問題 No.1677 mæx
ユーザー convexineqconvexineq
提出日時 2021-09-10 22:48:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 201 ms / 2,000 ms
コード長 2,613 bytes
コンパイル時間 347 ms
コンパイル使用メモリ 82,376 KB
実行使用メモリ 117,560 KB
最終ジャッジ日時 2024-06-12 02:24:32
合計ジャッジ時間 4,292 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,480 KB
testcase_01 AC 42 ms
52,736 KB
testcase_02 AC 42 ms
52,992 KB
testcase_03 AC 42 ms
52,224 KB
testcase_04 AC 201 ms
117,272 KB
testcase_05 AC 191 ms
117,080 KB
testcase_06 AC 184 ms
117,240 KB
testcase_07 AC 190 ms
117,272 KB
testcase_08 AC 186 ms
117,324 KB
testcase_09 AC 185 ms
117,028 KB
testcase_10 AC 185 ms
117,028 KB
testcase_11 AC 187 ms
117,380 KB
testcase_12 AC 187 ms
117,100 KB
testcase_13 AC 189 ms
117,232 KB
testcase_14 AC 180 ms
117,432 KB
testcase_15 AC 180 ms
117,036 KB
testcase_16 AC 180 ms
117,328 KB
testcase_17 AC 181 ms
117,560 KB
testcase_18 AC 182 ms
117,324 KB
testcase_19 AC 42 ms
52,992 KB
testcase_20 AC 40 ms
53,120 KB
testcase_21 AC 179 ms
117,444 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