Problem p02721 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll mod = 998244353;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int k, n, c;
int dp[200005][2], bf[200005], st, nxt[200005], en;
char ch[200005];
int bef(int x) {
  int BF = x - c - 1;
  if (BF < 1)
    return 0;
  if (ch[BF] == 'x')
    BF = bf[BF];
  if (BF < 1)
    return 0;
  return dp[BF][0];
}
int nt(int x) {
  int NT = x + c + 1;
  if (NT > n)
    return 0;
  if (ch[NT] == 'x')
    NT = nxt[NT];
  if (NT > n)
    return 0;
  return dp[NT][1];
}
int dpbf[200005], dpnxt[200005];
int main() {
  int i, j, now;
  // freopen("a.in","r",stdin);
  n = read(), k = read(), c = read();
  scanf("%s", ch + 1);
  for (i = 1; i <= n; i++) {
    bf[i] = st;
    if (ch[i] == 'o')
      st = i;
  }
  en = n + 1;
  for (i = n; i >= 1; i--) {
    nxt[i] = en;
    if (ch[i] == 'o')
      en = i;
  }
  for (i = 1; i <= n; i++) {
    dp[i][0] = dp[i - 1][0];
    if (ch[i] == 'o')
      dp[i][0] = max(dp[i][0], bef(i) + 1);
  }
  for (i = n; i >= 1; i--) {
    dp[i][1] = dp[i + 1][1];
    if (ch[i] == 'o')
      dp[i][1] = max(dp[i][1], nt(i) + 1);
  }
  st = 0;
  for (i = 1; i <= n; i++) {
    dpbf[i] = st;
    if (dp[i][0] != dp[i - 1][0])
      st = i;
  }
  en = n + 1;
  for (i = n; i >= 1; i--) {
    dpnxt[i] = en;
    if (dp[i][1] != dp[i + 1][1])
      en = i;
  }
  for (i = 1; i <= n; i++) {
    if (ch[i] == 'o')
      if (dp[dpbf[i]][0] + dp[min(n + 1, max(i + 1, dpbf[i] + c + 1))][1] < k &&
          dp[dpnxt[i]][1] + dp[max(0, min(i - 1, dpnxt[i] - c - 1))][0] < k)
        printf("%d\\n", i);
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll mod = 998244353;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int k, n, c;
int dp[100005][2], bf[100005], st, nxt[100005], en;
char ch[100005];
int bef(int x) {
  int BF = x - c - 1;
  if (BF < 1)
    return 0;
  if (ch[BF] == 'x')
    BF = bf[BF];
  if (BF < 1)
    return 0;
  return dp[BF][0];
}
int nt(int x) {
  int NT = x + c + 1;
  if (NT > n)
    return 0;
  if (ch[NT] == 'x')
    NT = nxt[NT];
  if (NT > n)
    return 0;
  return dp[NT][1];
}
int dpbf[100005], dpnxt[100005];
int main() {
  int i, j, now;
  // freopen("a.in","r",stdin);
  n = read(), k = read(), c = read();
  scanf("%s", ch + 1);
  for (i = 1; i <= n; i++) {
    bf[i] = st;
    if (ch[i] == 'o')
      st = i;
  }
  en = n + 1;
  for (i = n; i >= 1; i--) {
    nxt[i] = en;
    if (ch[i] == 'o')
      en = i;
  }
  for (i = 1; i <= n; i++) {
    dp[i][0] = dp[i - 1][0];
    if (ch[i] == 'o')
      dp[i][0] = max(dp[i][0], bef(i) + 1);
  }
  for (i = n; i >= 1; i--) {
    dp[i][1] = dp[i + 1][1];
    if (ch[i] == 'o')
      dp[i][1] = max(dp[i][1], nt(i) + 1);
  }
  st = 0;
  for (i = 1; i <= n; i++) {
    dpbf[i] = st;
    if (dp[i][0] != dp[i - 1][0])
      st = i;
  }
  en = n + 1;
  for (i = n; i >= 1; i--) {
    dpnxt[i] = en;
    if (dp[i][1] != dp[i + 1][1])
      en = i;
  }
  for (i = 1; i <= n; i++) {
    if (ch[i] == 'o')
      if (dp[dpbf[i]][0] + dp[min(n + 1, max(i + 1, dpbf[i] + c + 1))][1] < k &&
          dp[dpnxt[i]][1] + dp[max(0, min(i - 1, dpnxt[i] - c - 1))][0] < k)
        printf("%d\\n", i);
  }
}