結果

問題 No.3500 01 String
コンテスト
ユーザー ,
提出日時 2026-04-18 19:31:53
言語 Python3
(3.14.7 + numpy 2.5.2 + scipy 1.18.0 + ACL)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 194 ms / 2,000 ms
+ 284µs
コード長 810 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 57 ms
コンパイル使用メモリ 15,104 KB
実行使用メモリ 12,068 KB
最終ジャッジ日時 2026-09-08 20:03:10
合計ジャッジ時間 3,970 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
input = sys.stdin.readline

MOD = 998244353

def solve():
    N = int(input())
    A = input().strip()
    
    if '0' not in A or '1' not in A:
        print(1)
        return
    
    pz = A.index('0')                # first '0' position
    lo = N - 1 - A[::-1].index('1') # last '1' position
    
    if pz > lo:
        print(1)
        return
    
    dp = [1, 1]
    
    for j in range(pz, lo):
        new_dp = [0, 0]
        new_dp[0] = (new_dp[0] + dp[0]) % MOD     # 0→0
        new_dp[1] = (new_dp[1] + dp[0]) % MOD     # 0→1
        new_dp[1] = (new_dp[1] + dp[1]) % MOD     # 1→1
        if A[j] == '1' and A[j+1] == '0':         # 1→0 only if A has "10" here
            new_dp[0] = (new_dp[0] + dp[1]) % MOD
        dp = new_dp
    
    print((dp[0] + dp[1]) % MOD)

solve()
0