結果

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

ソースコード

diff #
raw source code

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

using namespace std;

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

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

    long long mod = 998244353;

    // 1. 変化に関係しない両端を削る
    // 先頭の '1' を飛ばす
    int start = 0;
    while (start < n && a[start] == '1') start++;

    // 末尾の '0' を飛ばす
    int end = n - 1;
    while (end >= 0 && a[end] == '0') end--;

    // もし 01 という並びが残らなければ、変化できないので 1 通り
    if (start >= end) {
        cout << 1 << endl;
        return 0;
    }

    // 2. ブロック分割して計算
    // 「0...01...1」をひとつの塊とし、1の次に0が来たら新しいブロック
    long long ans = 1;
    for (int i = start; i <= end; ) {
        int j = i;
        int zero_cnt = 0;
        int one_cnt = 0;

        // 0の塊を数える
        while (j <= end && a[j] == '0') {
            zero_cnt++;
            j++;
        }
        // 1の塊を数える
        while (j <= end && a[j] == '1') {
            one_cnt++;
            j++;
        }

        // ブロックの長さ + 1 を掛ける
        if (zero_cnt > 0 && one_cnt > 0) {
            ans = ans * (zero_cnt + one_cnt + 1) % mod;
        } else if (zero_cnt > 0) {
            // 末尾に 0 しか残らなかった場合(本来 end の処理で消えるはずですが念のため)
            ans = ans * 1 % mod;
        }
        
        i = j;
    }

    cout << ans << endl;

    return 0;
}
0