Problem p02852 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// #define int long long
#define endl '\n'
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define bug1(x)                                                                \
  { cerr << (#x) << "=" << x << endl; }
#define bug2(x, y)                                                             \
  { cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << endl; }
#define bug3(x, y, z)                                                          \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << endl;                                        \
  }
#define bug4(x, y, z, w)                                                       \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << "    " << (#w) << "=" << w << endl;          \
  }
#define bug5(x, y, z, w, p)                                                    \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << "    " << (#w) << "=" << w << "    " << (#p) \
         << "=" << p << endl;                                                  \
  }
#define bug6(x, y, z, w, p, q)                                                 \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << "    " << (#w) << "=" << w << "    " << (#p) \
         << "=" << p << "    " << (#q) << "=" << q << endl;                    \
  }
#define bugn(x, n)                                                             \
  {                                                                            \
    cerr << (#x) << ":";                                                       \
    for (int i = 0; i < n; i++)                                                \
      cerr << x[i] << "  ";                                                    \
    cerr << endl;                                                              \
  }
#define bugnm(x, n, m)                                                         \
  {                                                                            \
    cerr << (#x) << endl;                                                      \
    for (int i = 0; i < n; i++) {                                              \
      cerr << "Row #" << i << ":";                                             \
      for (int j = 0; j < m; j++)                                              \
        cerr << x[i][j] << "   ";                                              \
      cerr << endl;                                                            \
    }                                                                          \
  }
typedef long long ll;
typedef long double ld;
using namespace std;

int n, m;
string s;
vector<int> path;
vector<int> vis(100000 + 5);
void dfs(int n) {
  // if( vis[n] )return;
  // vis[n]=1;
  if (n == 0) {
    reverse(path.begin(), path.end());
    for (auto p : path) {
      cout << p << " ";
    }
    exit(0);
  }
  for (int k = min(n, m); k >= 1; k--) {
    if (s[n - k] == '0') {
      path.push_back(k);
      dfs(n - k);
      path.pop_back();
    }
  }
}
int32_t main() {
  IOS cin >> n >> m >> s;
  dfs(n);
  cout << -1 << endl;
}
/*
 * long long or int?
 * index out of bound?
 * Tested on own test case?corner?
 * Make more general solution.
 * Read Read Read Read ....
 */


Fixed submission:
#include <bits/stdc++.h>
// #define int long long
#define endl '\n'
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define bug1(x)                                                                \
  { cerr << (#x) << "=" << x << endl; }
#define bug2(x, y)                                                             \
  { cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << endl; }
#define bug3(x, y, z)                                                          \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << endl;                                        \
  }
#define bug4(x, y, z, w)                                                       \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << "    " << (#w) << "=" << w << endl;          \
  }
#define bug5(x, y, z, w, p)                                                    \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << "    " << (#w) << "=" << w << "    " << (#p) \
         << "=" << p << endl;                                                  \
  }
#define bug6(x, y, z, w, p, q)                                                 \
  {                                                                            \
    cerr << (#x) << "=" << (x) << "    " << (#y) << "=" << (y) << "    "       \
         << (#z) << "=" << (z) << "    " << (#w) << "=" << w << "    " << (#p) \
         << "=" << p << "    " << (#q) << "=" << q << endl;                    \
  }
#define bugn(x, n)                                                             \
  {                                                                            \
    cerr << (#x) << ":";                                                       \
    for (int i = 0; i < n; i++)                                                \
      cerr << x[i] << "  ";                                                    \
    cerr << endl;                                                              \
  }
#define bugnm(x, n, m)                                                         \
  {                                                                            \
    cerr << (#x) << endl;                                                      \
    for (int i = 0; i < n; i++) {                                              \
      cerr << "Row #" << i << ":";                                             \
      for (int j = 0; j < m; j++)                                              \
        cerr << x[i][j] << "   ";                                              \
      cerr << endl;                                                            \
    }                                                                          \
  }
typedef long long ll;
typedef long double ld;
using namespace std;

int n, m;
string s;
vector<int> path;
vector<int> vis(100000 + 5);
void dfs(int n) {
  if (vis[n])
    return;
  vis[n] = 1;
  if (n == 0) {
    reverse(path.begin(), path.end());
    for (auto p : path) {
      cout << p << " ";
    }
    exit(0);
  }
  for (int k = min(n, m); k >= 1; k--) {
    if (s[n - k] == '0') {
      path.push_back(k);
      dfs(n - k);
      path.pop_back();
    }
  }
}
int32_t main() {
  IOS cin >> n >> m >> s;
  dfs(n);
  cout << -1 << endl;
}
/*
 * long long or int?
 * index out of bound?
 * Tested on own test case?corner?
 * Make more general solution.
 * Read Read Read Read ....
 */