結果

問題 No.3500 01 String
コンテスト
ユーザー よいちなすの
提出日時 2026-04-18 00:06:31
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,316 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,281 ms
コンパイル使用メモリ 337,584 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-04-18 00:06:38
合計ジャッジ時間 3,440 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    cin >> N;
    string A;
    cin >> A;

    const int MOD = 998244353;

    vector<bool> ok1(N, false), ok2(N, false);
    
    bool ans0 = false;
    for (int i = 0; i < N; ++i) {
        if (A[i] == '0') ans0 = true;
        ok1[i] = ans0;
    }

    bool ans1 = false;
    for (int i = N - 1; i >= 0; --i) {
        if (A[i] == '1') ans1 = true;
        ok2[i] = ans1;
    }

    vector<long long> dp(2, 0);
    if (ok1[0]) dp[0] = 1;
    if (ok2[0]) dp[1] = 1;

    for (int i = 0; i < N - 1; ++i) {
        vector<long long> next_dp(2, 0);
        
        for (int curr = 0; curr <= 1; ++curr) {
            if (dp[curr] == 0) continue;

            for (int nxt = 0; nxt <= 1; ++nxt) {
                if (nxt == 0 && !ok1[i + 1]) continue;
                if (nxt == 1 && !ok2[i + 1]) continue;

                if (curr == 1 && nxt == 0) {
                    if (!(A[i] == '1' && A[i + 1] == '0')) {
                        continue;
                    }
                }

                next_dp[nxt] = (next_dp[nxt] + dp[curr]) % MOD;
            }
        }
        dp = next_dp;
    }

    long long ans = (dp[0] + dp[1]) % MOD;
    cout << ans << endl;

    return 0;
}
0