Problem p02721 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#define REL

#pragma GCC optimize("Ofast")

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef int64_t i64;
typedef long long LL;
typedef long long ll;
typedef long double LD;
typedef complex<LD> cld;
typedef unsigned int uint;
typedef complex<double> cd;
typedef unsigned long long ULL;

#define endl "\n"
#define dendl "\n\n"
#define mp make_pair
#define mt make_tuple
#define size(v) (int)v.size()
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

/////////////////////////////////////////////////////////////////////

template <typename T> T sqr(T a) { return a * a; }

template <typename T> int sign(T a) {
  if (a == 0)
    return 0;
  return (a > 0 ? 1 : -1);
}

template <typename T1, typename T2> T2 sum_mod(T1 x, T2 MOD) {
  if (x < 0)
    x += MOD;
  if (x >= MOD)
    x -= MOD;
  return x;
}

template <typename T1, typename T2> T2 mul_mod(T1 x, T2 MOD) {
  if (x < 0)
    return (abs(x) % MOD == 0 ? 0 : MOD - abs(x) % MOD);
  return x % MOD;
}

template <typename T> bool uax(T &a, const T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> bool uin(T &a, const T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
  in >> p.first >> p.second;
  return in;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> &p) {
  out << p.first << ' ' << p.second;
  return out;
}

const int LOG = 18;
const int N = (int)1e5 + 7;
const int MAXN = (int)2e3 + 7;
const int MOD = (int)998244353;
const int INF = (int)1e9 + 7;
const int CONST = 300;
const LL LINF = (i64)2e15;
const LD PI = 3.1415926535897932384626433832795;
const LD EPS = 1;

/////////////////////////////////////////////////////////////////////

int nxt[N], work[N];
int n, k, c;
string s;

int main() {
  double start = clock();
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  cout << fixed << setprecision(20);
  cerr << fixed << setprecision(20);
#else
  cout << fixed << setprecision(20);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#endif

  cin >> n >> k >> c >> s;
  int lst = INF;
  for (int i = n - 1; i >= 0; i--) {
    nxt[i] = lst;
    if (s[i] == 'o')
      lst = i;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == 'x')
      continue;
    int pos = i + c + 1;
    if (pos >= n || (s[pos] == 'x' && nxt[pos] == INF))
      work[i] = 1;
    else {
      if (s[pos] == 'o')
        work[i] = work[pos] + 1;
      else
        work[i] = work[nxt[pos]] + 1;
    }
  }
  vector<int> day;
  int pos = (s[0] == 'o' ? 0 : nxt[0]);
  if (work[pos] > k)
    return 0;
  for (int i = 0; i < k; i++) {
    day.push_back(pos);
    if (i != k - 1)
      pos = (s[pos + c + 1] == 'o' ? pos + c + 1 : nxt[pos + c + 1]);
  }
  vector<int> ans;
  for (int i = 0; i < k; i++) {
    if (nxt[day[i]] == INF || i + work[nxt[day[i]]] < k)
      ans.push_back(day[i] + 1);
  }
  for (int x : ans)
    cout << x << endl;

#ifdef _DEBUG
  cout << endl << "Time is " << (clock() - start) / 1000.0;
#endif
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#define REL

#pragma GCC optimize("Ofast")

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef int64_t i64;
typedef long long LL;
typedef long long ll;
typedef long double LD;
typedef complex<LD> cld;
typedef unsigned int uint;
typedef complex<double> cd;
typedef unsigned long long ULL;

#define endl "\n"
#define dendl "\n\n"
#define mp make_pair
#define mt make_tuple
#define size(v) (int)v.size()
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

/////////////////////////////////////////////////////////////////////

template <typename T> T sqr(T a) { return a * a; }

template <typename T> int sign(T a) {
  if (a == 0)
    return 0;
  return (a > 0 ? 1 : -1);
}

template <typename T1, typename T2> T2 sum_mod(T1 x, T2 MOD) {
  if (x < 0)
    x += MOD;
  if (x >= MOD)
    x -= MOD;
  return x;
}

template <typename T1, typename T2> T2 mul_mod(T1 x, T2 MOD) {
  if (x < 0)
    return (abs(x) % MOD == 0 ? 0 : MOD - abs(x) % MOD);
  return x % MOD;
}

template <typename T> bool uax(T &a, const T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> bool uin(T &a, const T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
  in >> p.first >> p.second;
  return in;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> &p) {
  out << p.first << ' ' << p.second;
  return out;
}

const int LOG = 18;
const int N = (int)2e5 + 7;
const int MAXN = (int)2e3 + 7;
const int MOD = (int)998244353;
const int INF = (int)1e9 + 7;
const int CONST = 300;
const LL LINF = (i64)2e15;
const LD PI = 3.1415926535897932384626433832795;
const LD EPS = 1;

/////////////////////////////////////////////////////////////////////

int nxt[N], work[N];
int n, k, c;
string s;

int main() {
  double start = clock();
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  cout << fixed << setprecision(20);
  cerr << fixed << setprecision(20);
#else
  cout << fixed << setprecision(20);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#endif

  cin >> n >> k >> c >> s;
  int lst = INF;
  for (int i = n - 1; i >= 0; i--) {
    nxt[i] = lst;
    if (s[i] == 'o')
      lst = i;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == 'x')
      continue;
    int pos = i + c + 1;
    if (pos >= n || (s[pos] == 'x' && nxt[pos] == INF))
      work[i] = 1;
    else {
      if (s[pos] == 'o')
        work[i] = work[pos] + 1;
      else
        work[i] = work[nxt[pos]] + 1;
    }
  }
  vector<int> day;
  int pos = (s[0] == 'o' ? 0 : nxt[0]);
  if (work[pos] > k)
    return 0;
  for (int i = 0; i < k; i++) {
    day.push_back(pos);
    if (i != k - 1)
      pos = (s[pos + c + 1] == 'o' ? pos + c + 1 : nxt[pos + c + 1]);
  }
  vector<int> ans;
  for (int i = 0; i < k; i++) {
    if (nxt[day[i]] == INF || i + work[nxt[day[i]]] < k)
      ans.push_back(day[i] + 1);
  }
  for (int x : ans)
    cout << x << endl;

#ifdef _DEBUG
  cout << endl << "Time is " << (clock() - start) / 1000.0;
#endif
}