参考答案

cookiebus 2024-01-24 16:21:11 3 返回题目

#include <bits/stdc++.h>
using namespace std;
int m, n, k, ans = 1e9;
string s;
int main() {
    cin >> n;
    cin >> s;
    cin >> k;
    int j = -1, cnt = 0;
    for (int i = 0; i < n; ++i) {
        j = max(j, i - 1);
        while (cnt < k && j + 1 < n) {
            j = j + 1;
            // [i, j]
            if (s[j] == '1')
                cnt++;
        }
        if (cnt == k)
            ans = min(ans, j - i + 1);
        if (s[i] == '1')
            cnt--;
    }
    if (ans == 1e9)
        ans = -1;
    cout << ans << endl;
}
{{ vote && vote.total.up }}