C - 交通渋滞の報告 / Traffic Jam Report Editorial by MMNMM

別解

\(S\) の列の先頭に \(0\) を追加しておきます(これで答えが変わることはありません)。

求めるものは「\(S\) の中に \((0,1,1,1,\ldots,1)\ (1\) が \(K\) 個\()\) がいくつ含まれているか」と言い換えることができます。

これは、例えば Z-algorithm などで求めることができます。 列 \((0,1,1,1,\ldots,1)+{\!\!\!\!}+(2)+{\!\!\!\!}+S\) に対して Z-algorithm を使い、\(K+1\) がいくつあるか数えることなどで解くことができます。

実装例は以下のようになります。

#include <iostream>
#include <vector>
#include <ranges>
#include <atcoder/string>
using namespace std;

int main(){
    int N, K;
    cin >> N >> K;
    cout << ranges::count(atcoder::z_algorithm<int>(
        vector{
            vector{0}, vector(K, 1), // (0, 1, 1, ..., 1) が
            vector{2},
            vector{0}, views::istream<int>(cin) | ranges::to<vector>() // (0) + S の中に
        } | views::join | ranges::to<vector>()
    ), K + 1) << endl; // いくつあるか数える
    return 0;
}
N, K = map(int, input().split())

print(('0 ' + input() + ' ').count('0 ' + '1 ' * K))

posted:
last update: