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;
  void read(int& x) { scanf("%d", &x); }
  void read(long long& x) { scanf("%lld", &x); }
  void read(double& x) { scanf("%lf", &x); }
  long long gcd(long long x, long long y) {
    while (y) {
      long long t = x % y;
      x = y;
      y = t;
    }
    return x;
  }
  char a[600], b[600];
  void solve() {
    int n, m;
    scanf("%d%d", &n, &m);
    scanf("%s%s", &a[1], &b[1]);
    vector<int> pi(m + 1, 0);
    pi[0] = -1;
    {
      int k = -1;
      for (int i = 1; i <= m; i++) {
        while (k >= 0 && b[k + 1] != b[i]) k = pi[k];
        pi[i] = ++k;
      }
    }
    vector<map<pair<int, int>, int>> d(n + 1);
    d[0][{0, 0}] = 0;
    for (int t = 0; t < n; t++) {
      for (auto& [key, v] : d[t]) {
        int c0 = key.first;
        int k0 = key.second;
        for (int dv = 0; dv <= 1; dv++) {
          int c = c0;
          int k = k0;
          int nxti = a[t + 1] - '0';
          if (dv) nxti = !nxti;
          char nxt = '0' + nxti;
          while (k >= 0 && b[k + 1] != nxt) k = pi[k];
          k++;
          if (k == m) {
            c++;
            k = pi[k];
          }
          auto nk = make_pair(c, k);
          auto it = d[t + 1].find(nk);
          if (it != d[t + 1].end())
            it->second = min(it->second, v + dv);
          else
            d[t + 1][nk] = v + dv;
        }
      }
      d[t].clear();
    }
    vector<int> ans(n - m + 2, -1);
    for (auto& [key, v] : d[n]) {
      if (ans[key.first] < 0 || ans[key.first] > v) ans[key.first] = v;
    }
    for (auto x : ans) printf("%d ", x);
    printf("\n");
  }
  int main() {
    { solve(); }
    return 0;
  }
  