結果

問題 No.3500 01 String
コンテスト
ユーザー Azaki
提出日時 2026-04-18 01:28:19
言語 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
結果
WA  
実行時間 -
コード長 1,583 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,479 ms
コンパイル使用メモリ 161,056 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-04-18 01:29:08
合計ジャッジ時間 5,564 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 WA * 2
other AC * 3 WA * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
    // 標準入出力の高速化
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string a;
    if (!(cin >> a)) return 0;
    int n = a.length();

    long long mod = 998244353;

    // 1. 最初の '0' と 最後の '1' の位置を探す
    int first_zero = -1;
    for (int i = 0; i < n; ++i) {
        if (a[i] == '0') {
            first_zero = i;
            break;
        }
    }

    int last_one = -1;
    for (int i = n - 1; i >= 0; --i) {
        if (a[i] == '1') {
            last_one = i;
            break;
        }
    }

    // 0が1より左にない場合、変化は起きないので1通り
    if (first_zero == -1 || last_one == -1 || first_zero > last_one) {
        cout << 1 << endl;
        return 0;
    }

    // 2. 有効範囲 [first_zero, last_one] を走査してブロック計算
    long long ans = 1;
    int i = first_zero;
    while (i <= last_one) {
        long long zero_count = 0;
        long long one_count = 0;

        // 連続する '0' を数える
        while (i <= last_one && a[i] == '0') {
            zero_count++;
            i++;
        }
        // 連続する '1' を数える
        while (i <= last_one && a[i] == '1') {
            one_count++;
            i++;
        }

        // ブロック (0...01...1) の組み合わせ数を掛ける
        if (zero_count > 0 && one_count > 0) {
            ans = (ans * (zero_count + one_count)) % mod;
        }
    }

    cout << ans << endl;

    return 0;
}
0