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>
  #pragma GCC optimize("-O3")
  using namespace std;
  const long long mod = 998244353;
  int n, m;
  string a, b;
  void prefix_func(string &s, vector<int> &v) {
    int n = s.size();
    v.assign(n, 0);
    for (int i = 1; i < n; i++) {
      int j = v[i - 1];
      while (j >= 0 && s[j] != s[i]) {
        j = j == 0 ? -1 : v[j - 1];
      }
      v[i] = j + 1;
    }
  }
  void solve() {
    cin >> n >> m;
    cin >> a >> b;
    vector<int> v;
    prefix_func(b, v);
    int mx = n - m + 1;
    vector<int> va(n);
    int cnt = 0;
    vector<vector<vector<int> > > dp(
        2, vector<vector<int> >(m + 1, vector<int>(mx + 1, 1000)));
    int x = 0;
    dp[0][0][0] = 0;
    for (int i = 0; i < n; i++) {
      x ^= 1;
      vector<vector<int> > jfin(m, vector<int>(2, -1));
      for (int j = 0; j < m; j++) {
        for (int cnt = 0; cnt <= mx; cnt++) {
          dp[x][j][cnt] = 1000;
        }
      }
      for (int j = 0; j < m; j++) {
        if (a[i] == b[j]) {
          jfin[j][0] = j + 1;
          jfin[j][1] = j == 0 ? 0 : jfin[v[j - 1]][1];
        } else {
          jfin[j][1] = j + 1;
          jfin[j][0] = j == 0 ? 0 : jfin[v[j - 1]][0];
        }
      }
      for (int j = 0; j < m; j++) {
        for (int cnt = 0; cnt < mx; cnt++) {
          if (dp[x ^ 1][j][cnt] == 1000) continue;
          int cr1 = jfin[j][0], cr2 = jfin[j][1];
          if (cr1 == m) {
            cr1 = v[cr1 - 1];
          }
          if (cr2 == m) {
            cr2 = v[cr2 - 1];
          }
          dp[x][cr1][cnt + (jfin[j][0] == m)] =
              min(dp[x][cr1][cnt + (jfin[j][0] == m)], dp[x ^ 1][j][cnt]);
          dp[x][cr2][cnt + (jfin[j][1] == m)] =
              min(dp[x][cr2][cnt + (jfin[j][1] == m)], 1 + dp[x ^ 1][j][cnt]);
        }
      }
    }
    vector<int> ans(n - m + 2, -1);
    for (int j = 0; j < m; j++) {
      for (int cnt = 0; cnt <= mx; cnt++) {
        if (dp[x][j][cnt] == 1000) continue;
        ans[cnt] = ans[cnt] == -1 ? dp[x][j][cnt] : min(ans[cnt], dp[x][j][cnt]);
      }
    }
    for (int i = (int)0; i != (int)n - m + 2; i = i + 1) {
      cout << ans[i] << " ";
    }
    cout << '\n';
    ;
  }
  int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    while (t--) {
      solve();
    }
  }
  