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;
  const int MAX = 500;
  const int INF = 1e6;
  int dp[2][MAX + 1][MAX + 1] = {0};
  int nxt[MAX + 1][2];
  int n, m;
  string s, t;
  void pre() {
    string pref = ".";
    for (int j = 0; j <= m; j++) {
      if (j) pref += t[j];
      {
        for (int c = 0; c <= 1; c++) {
          pref += '0' + c;
          int sz = (int)pref.size() - 1;
          nxt[j][c] = 0;
          for (int z = m; z >= 1; z--) {
            if (z <= sz) {
              if (t.substr(1, z) == pref.substr(sz - z + 1)) {
                nxt[j][c] = z;
                break;
              }
            }
          }
          pref.pop_back();
        }
      }
    }
  }
  void solve() {
    cin >> n >> m;
    cin >> s >> t;
    s = "." + s;
    t = "." + t;
    pre();
    dp[0][0][0] = 0;
    for (int j = 1; j <= n; j++) {
      for (int pref = 0; pref <= m; pref++)
        for (int k = 0; k <= n - m + 1; k++) dp[1][pref][k] = INF;
      for (int pref = 0; pref <= m; pref++) {
        for (int k = 0; k <= n - m + 1; k++) {
          for (int z = 0; z <= 1; z++) {
            if (nxt[pref][z] == m) {
              dp[1][nxt[pref][z]][k + 1] = min(dp[0][pref][k] + (s[j] != '0' + z),
                                               dp[1][nxt[pref][z]][k + 1]);
            } else {
              dp[1][nxt[pref][z]][k] =
                  min(dp[0][pref][k] + (s[j] != '0' + z), dp[1][nxt[pref][z]][k]);
            }
          }
        }
      }
      for (int pref = 0; pref <= m; pref++)
        for (int k = 0; k <= n - m + 1; k++) dp[0][pref][k] = dp[1][pref][k];
    }
    for (int k = 0; k <= n - m + 1; k++) {
      int ans = INF;
      for (int z = 0; z <= m; z++) ans = min(ans, dp[0][z][k]);
      cout << (ans == INF ? -1 : ans) << ' ';
    }
    cout << '\n';
    return;
  }
  signed main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    t = 1;
    for (int i = 0; i <= MAX; i++) {
      for (int k = 0; k <= MAX; k++) dp[0][i][k] = INF;
    }
    while (t--) {
      solve();
    }
    return 0;
  }
  