結果

問題 No.1677 mæx
ユーザー ぷらぷら
提出日時 2021-09-10 22:28:15
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 2,381 bytes
コンパイル時間 2,091 ms
コンパイル使用メモリ 205,132 KB
実行使用メモリ 20,684 KB
最終ジャッジ日時 2023-09-02 18:43:08
合計ジャッジ時間 3,684 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,812 KB
testcase_01 AC 3 ms
9,752 KB
testcase_02 AC 3 ms
9,748 KB
testcase_03 AC 4 ms
9,808 KB
testcase_04 AC 22 ms
20,128 KB
testcase_05 AC 22 ms
20,048 KB
testcase_06 AC 22 ms
19,988 KB
testcase_07 AC 22 ms
19,988 KB
testcase_08 AC 22 ms
19,928 KB
testcase_09 AC 22 ms
20,012 KB
testcase_10 AC 23 ms
19,924 KB
testcase_11 AC 23 ms
20,072 KB
testcase_12 AC 23 ms
19,992 KB
testcase_13 AC 22 ms
19,976 KB
testcase_14 AC 22 ms
20,004 KB
testcase_15 AC 23 ms
20,020 KB
testcase_16 AC 23 ms
19,980 KB
testcase_17 AC 23 ms
19,924 KB
testcase_18 AC 23 ms
20,012 KB
testcase_19 AC 4 ms
9,740 KB
testcase_20 AC 3 ms
9,944 KB
testcase_21 AC 25 ms
20,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

constexpr int mod = 998244353;
int c[200005];
vector<int> ki[200005];
long long dp[200005][5];

int mex(int a,int b) {
    if(a != 0 && b != 0) {
        return 0;
    }
    if(a != 1 && b != 1) {
        return 1;
    }
    return 2;
}

void dfs(int n,int p) {
    int a = -1,b = -1;
    for(int i:ki[n]) {
        if(i == p) {
            continue;
        }
        dfs(i,n);
        if(a == -1) {
            a = i;
        }
        else {
            b = i;
        }
    }
    if(ki[n].size() == 1) {
        if(c[n] == 3) {
            dp[n][0] = dp[n][1] = dp[n][2] = 1;
        }
        else {
            dp[n][c[n]] = 1;
        }
    }
    else {
        if(c[n] == 4 || c[n] == 6) {
            for(int i = 0; i < 3; i++) {
                for(int j = 0; j < 3; j++) {
                    dp[n][max(i,j)] += dp[a][i]*dp[b][j]%mod;
                    dp[n][max(i,j)] %= mod;
                }
            }
        }
        if(c[n] == 5 || c[n] == 6) {
            for(int i = 0; i < 3; i++) {
                for(int j = 0; j < 3; j++) {
                    dp[n][mex(i,j)] += dp[a][i]*dp[b][j]%mod;
                    dp[n][mex(i,j)] %= mod;
                }
            }
        }
    }
}

int main() {
    string S;
    int K;
    cin >> S >> K;
    if(S.size() == 1) {
        if(S[0]-'0' == K || S[0] == '?') {
            cout << 1 << endl;
        }
        else {
            cout << 0 << endl;
        }
        return 0;
    }
    stack<int>st;
    for(int i = 0; i < S.size(); i++) {
        if(S[i] == 'm') {
            if(!st.empty()) {
                ki[i].push_back(st.top());
                ki[st.top()].push_back(i);
            }
            st.push(i);
            if(S[i+1] == '?') {
                c[i] = 6;
            }
            if(S[i+1] == 'a') {
                c[i] = 4;
            }
            if(S[i+1] == 'e') {
                c[i] = 5;
            }
            i += 2;
        }
        if(S[i] == ')') {
            st.pop();
        }
        if(S[i] == '?' || (S[i] >= '0' && S[i] <= '2')) {
            ki[st.top()].push_back(i);
            ki[i].push_back(st.top());
            if(S[i] == '?') {
                c[i] = 3;
            }
            else {
                c[i] = S[i]-'0';
            }
        }
    }
    dfs(0,-1);
    cout << dp[0][K] << endl;
}
0