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 ll = long long;
  int const nmax = 500;
  char v[5 + nmax], v2[5 + nmax];
  int pre[5 + nmax], edges[5 + nmax][2];
  void precompute(int m) {
    int ptr = 0;
    for (int i = 2; i <= m; i++) {
      while (true) {
        if (v2[i] == v2[1 + ptr]) {
          ptr++;
          pre[i] = ptr;
          break;
        } else if (0 == ptr) {
          pre[i] = 0;
          break;
        } else {
          ptr = pre[ptr];
        }
      }
    }
    for (int i = 0; i <= m; i++) {
      for (char ch = '0'; ch <= '1'; ch++) {
        int curr = i;
        while (true) {
          if (curr < m && v2[curr + 1] == ch) {
            curr++;
            break;
          } else if (0 < curr) {
            curr = pre[curr];
          } else {
            break;
          }
        }
        edges[i][ch - '0'] = curr;
      }
    }
  }
  int dp[5 + nmax][5 + nmax];
  int dp2[5 + nmax][5 + nmax];
  int main() {
    int n, m;
    std::cin >> n >> m;
    for (int i = 1; i <= n; i++) std::cin >> v[i];
    for (int i = 1; i <= m; i++) std::cin >> v2[i];
    precompute(m);
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= m; j++) dp[i][j] = dp2[i][j] = n * 2;
    dp[0][0] = 0;
    for (int step = 1; step <= n; step++) {
      int lim = std::min(step, m);
      int lim2 = std::max(0, step - m + 1);
      for (int i = 0; i <= lim2; i++)
        for (int j = 0; j <= lim; j++) {
          for (int h = 0; h < 2; h++) {
            dp2[i + (edges[j][h] == m)][edges[j][h]] =
                std::min(dp2[i + (edges[j][h] == m)][edges[j][h]],
                         dp[i][j] + (h != (v[step] - '0')));
          }
        }
      for (int i = 0; i <= lim2; i++)
        for (int j = 0; j <= lim; j++) {
          dp[i][j] = dp2[i][j];
          dp2[i][j] = n * 2;
        }
    }
    for (int i = 0; i <= n - m + 1; i++) {
      int result = dp[i][0];
      for (int j = 0; j <= m; j++) {
        result = std::min(result, dp[i][j]);
      }
      if (n < result) {
        std::cout << -1 << " ";
      } else
        std::cout << result << " ";
    }
    std::cout << '\n';
    return 0;
  }
  