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;
  template <class T>
  bool umin(T &a, const T &b) {
    return (a > b ? a = b, 1 : 0);
  }
  template <class T>
  bool umax(T &a, const T &b) {
    return (a < b ? a = b, 1 : 0);
  }
  void INPUT(bool F = 0) {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    if (F) {
      freopen(
          "name"
          ".in",
          "r", stdin);
      freopen(
          "name"
          ".out",
          "w", stdout);
    }
  }
  const int N = 503;
  int dp[2][N][N];
  int state[N][N];
  vector<int> cmp(string a) {
    int n = (int)(a).size();
    vector<int> p(n);
    p[0] = 0;
    for (int i = 1; i < n; ++i) {
      int j = p[i - 1];
      while (a[j] != a[i] && j > 0) j = p[j - 1];
      if (a[j] == a[i]) j++;
      p[i] = j;
    }
    return p;
  }
  const int inf = 1e9;
  void clr(int layer) {
    for (int x = 0; x < N; ++x) {
      for (int y = 0; y < N; ++y) dp[layer][x][y] = inf;
    }
  }
  signed main() {
    INPUT();
    int n, m;
    cin >> n >> m;
    string a, b;
    cin >> a >> b;
    swap(a, b);
    for (int i = 0; i <= (int)(a).size(); ++i) {
      for (int bt = 0; bt < 2; ++bt) {
        string nw = a.substr(0, i) + char('0' + bt);
        nw = a + "#" + nw;
        vector<int> p = cmp(nw);
        state[i][bt] = p.back();
      }
    }
    int bt = 0;
    clr(bt);
    dp[0][0][0] = 0;
    for (int i = 0; i < (int)(b).size(); ++i) {
      clr(bt ^ 1);
      for (int k = 0; k < (int)(b).size(); ++k) {
        for (int first = 0; first <= (int)(b).size(); ++first) {
          for (int t = 0; t < 2; ++t) {
            int togo = state[first][t];
            int dk;
            if (togo == (int)(a).size())
              dk = 1;
            else
              dk = 0;
            dp[bt ^ 1][k + dk][togo] = min(
                dp[bt ^ 1][k + dk][togo], dp[bt][k][first] + (t != (b[i] - '0')));
          }
        }
      }
      bt ^= 1;
    }
    for (int k = 0; k <= n - m + 1; ++k) {
      int ans = 1e9;
      for (int z = 0; z <= (int)(a).size(); ++z) ans = min(ans, dp[bt][k][z]);
      cout << (ans == inf ? -1 : ans) << ' ';
    }
    return 0;
  }
  