description: The Winter holiday will be here soon. Mr. Chanek wants to decorate his house's wall with ornaments. The wall can be represented as a binary string a of length n. His favorite nephew has another binary string b of length m (m ≤ n).
  
  Mr. Chanek's nephew loves the non-negative integer k. His nephew wants exactly k occurrences of b as substrings in a. 
  
  However, Mr. Chanek does not know the value of k. So, for each k (0 ≤ k ≤ n - m + 1), find the minimum number of elements in a that have to be changed such that there are exactly k occurrences of b in a.
  
  A string s occurs exactly k times in t if there are exactly k different pairs (p,q) such that we can obtain s by deleting p characters from the beginning and q characters from the end of t.
  
  Input
  
  The first line contains two integers n and m (1 ≤ m ≤ n ≤ 500) — size of the binary string a and b respectively.
  
  The second line contains a binary string a of length n.
  
  The third line contains a binary string b of length m.
  
  Output
  
  Output n - m + 2 integers — the (k+1)-th integer denotes the minimal number of elements in a that have to be changed so there are exactly k occurrences of b as a substring in a.
  
  Example
  
  Input
  
  
  9 3
  100101011
  101
  
  
  Output
  
  
  1 1 0 1 6 -1 -1 -1
  
  Note
  
  For k = 0, to make the string a have no occurrence of 101, you can do one character change as follows.
  
  100101011 → 100100011
  
  For k = 1, you can also change a single character.
  
  100101011 → 100001011
  
  For k = 2, no changes are needed.
solution: #include <bits/stdc++.h>
  using namespace std;
  using ll = long long;
  using ld = long double;
  using pii = pair<int, int>;
  using cd = complex<ld>;
  vector<int> prefix_function(string s) {
    int n = (int)s.length();
    vector<int> pi(n);
    for (int i = 1; i < n; ++i) {
      int j = pi[i - 1];
      while (j > 0 && s[i] != s[j]) j = pi[j - 1];
      if (s[i] == s[j]) ++j;
      pi[i] = j;
    }
    return pi;
  }
  int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    string a, b;
    cin >> a >> b;
    vector<int> pi = prefix_function(b);
    b = "#" + b;
    pi.insert(pi.begin(), 0);
    vector<vector<int>> go(m + 1, vector<int>(2, -1));
    function<int(int, int)> go_c = [&](int i, int j) {
      if (go[i][j] != -1) {
        return go[i][j];
      }
      if (i + 1 < (int)a.size() && b[i + 1] == j + '0') {
        return go[i][j] = i + 1;
      } else if (i == 0) {
        return go[i][j] = 0;
      } else {
        return go[i][j] = go_c(pi[i], j);
      }
    };
    vector<vector<vector<int>>> dp(
        2, vector<vector<int>>(n + 2, vector<int>(n + 2, n + 100)));
    a = "#" + a;
    dp[0][0][0] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = m - 1; j >= 0; j--) {
        for (int k = n; k >= 0; k--) {
          int c = a[i] - '0';
          if (j != m) {
            int v = go_c(j, c);
            if (v == m) {
              dp[1][pi[v]][k + 1] = min(dp[1][pi[v]][k + 1], dp[0][j][k]);
            } else {
              dp[1][v][k] = min(dp[1][go_c(j, c)][k], dp[0][j][k]);
            }
            c = 1 - c;
            v = go_c(j, c);
            if (v == m) {
              dp[1][pi[v]][k + 1] = min(dp[1][pi[v]][k + 1], dp[0][j][k] + 1);
            } else {
              dp[1][v][k] = min(dp[1][go_c(j, c)][k], dp[0][j][k] + 1);
            }
          }
        }
      }
      swap(dp[0], dp[1]);
      for (int j = 0; j <= m; j++) {
        for (int k = 0; k <= n; k++) {
          dp[1][j][k] = n + 100;
        }
      }
    }
    for (int k = 0; k < n - m + 2; k++) {
      int res = n + 100;
      for (int j = 0; j < m; j++) {
        res = min(res, dp[0][j][k]);
      }
      if (res == n + 100) {
        cout << "-1 ";
      } else {
        cout << res << " ";
      }
    }
    return 0;
  }
  