結果

問題 No.2106 Wild Cacco
ユーザー hitonanode
提出日時 2022-09-19 22:41:32
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 151 ms / 2,000 ms
コード長 1,689 bytes
コンパイル時間 9,768 ms
コンパイル使用メモリ 276,032 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-30 11:43:51
合計ジャッジ時間 12,737 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

// Validator
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;

#include "testlib.h"

#include <atcoder/modint>
using mint = atcoder::modint998244353;

#define FOR(i, begin, end) for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define REP(i, n) FOR(i, 0, n)

int main(int argc, char *argv[]) {
    registerValidation(argc, argv);

    string S = inf.readString();
    cerr << S << ' ' << S.size() << endl;
    inf.readEof();

    assert(1 <= S.size() and S.size() <= 4000);
    for (char c : S) {
        assert(c == '(' or c == ')' or c == '?' or c == '.');
    }

    const int N = S.size();

    vector dp(2, vector<mint>(N + 1));
    dp[0][0] = dp[1][0] = 1;

    for (auto c : S) {
        vector dpnxt(2, vector<mint>(N + 1));

        if (c != ')' and c != '?') {
            // Insert (
            REP(b, 2) REP(i, dp[b].size()) {
                if (dp[b][i].val()) { dpnxt[b][i + 1] += dp[b][i]; }
            }
        }

        if (c != '(' and c != '?') {
            // Insert )
            REP(b, 2) REP(i, dp[b].size()) {
                if (dp[b][i].val() and i) { dpnxt[b][i - 1] += dp[b][i]; }
            }
        }

        if (c == '.' or c == '?') {
            // Insert ?
            REP(b, 2) REP(i, dp[b].size()) {
                if (dp[b][i].val()) {
                    if (b == 0) {
                        dpnxt[0][i + 1] += dp[b][i];
                        dpnxt[1][i + 1] += dp[b][i];
                    } else {
                        if (i) { dpnxt[1][i - 1] += dp[b][i]; }
                    }
                }
            }
        }
        dp = dpnxt;
    }
    cout << dp.at(1).at(0).val() << endl;
}
0