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

#pragma GCC optimize(3)
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
// #include"functions.h"
// #include"highprecision.h"
#define pb push_back
#define pf push_front
#define ppb pop_back
#define mp make_pair
#define np next_permutation
#define fi first
#define sc second
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pff pair<float, float>
#define pdd pair<double, double>
#define psi pair<string, int>
#define pis pair<int, string>
#define ppi pair<pii, int>
#define pdi pair<pii, pii>
#define LN 84757
#define INF 1e9
#define LLINF 1e18
#define MAXINT 0x7fffffff
#define MAXLL 0x7fffffffffffffff
#define EPS 1e-9
#define MOD 1000000007
#define E 2.7182818284
#define PI 3.1415926535
#define rep(a, b) for (int a = 0; a < b; a++)
#define forn(a, n) for (int a = 1; a <= n; a++)
#define repn(a, n, p) for (int a = 0; a < n; a += p)
#define foreach(a, b) for (__typeof(b.begin()) a = b.begin(); a != b.end(); a++)
#define fillemp(a) memset(a, 0, sizeof(a))
#define fillinf(a) memset(a, 0xff, sizeof(a))
#define stdf(a, b)                                                             \\
  freopen(a, "r", stdin);                                                      \\
  freopen(b, "w", stdout);
#define inputf(a) freopen(a, "r", stdin);
#define outputf(a) freopen(a, "w", stdout);
#define sz(a) a.size()
#define all(a) a.begin(), a.end()
#define MOD 1000000007
typedef long long ll;
typedef long double ld;
using namespace std;

string s;
bool can[200010];
int a[200010], b[200010];
int main(int argc, char **argv) {
  ios::sync_with_stdio(0);
  int n, k, c, d;
  cin >> n >> k >> c >> s;
  rep(i, n) {
    if (s[i] == 'o')
      can[i] = 1;
  }
  int pos = 0;
  rep(i, k) {
    // cout<<i<<" "<<pos<<endl;
    a[i] = pos;
    pos += c + 1;
    while (!can[pos] && pos < n)
      pos++;
  }
  pos = n - 1;
  for (int i = k - 1; i >= 0; i--) {
    b[i] = pos;
    pos -= c + 1;
    while (!can[pos] && pos >= 0)
      pos--;
  }
  rep(i, k) {
    if (a[i] == b[i]) {
      cout << a[i] + 1 << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize(3)
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
// #include"functions.h"
// #include"highprecision.h"
#define pb push_back
#define pf push_front
#define ppb pop_back
#define mp make_pair
#define np next_permutation
#define fi first
#define sc second
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pff pair<float, float>
#define pdd pair<double, double>
#define psi pair<string, int>
#define pis pair<int, string>
#define ppi pair<pii, int>
#define pdi pair<pii, pii>
#define LN 84757
#define INF 1e9
#define LLINF 1e18
#define MAXINT 0x7fffffff
#define MAXLL 0x7fffffffffffffff
#define EPS 1e-9
#define MOD 1000000007
#define E 2.7182818284
#define PI 3.1415926535
#define rep(a, b) for (int a = 0; a < b; a++)
#define forn(a, n) for (int a = 1; a <= n; a++)
#define repn(a, n, p) for (int a = 0; a < n; a += p)
#define foreach(a, b) for (__typeof(b.begin()) a = b.begin(); a != b.end(); a++)
#define fillemp(a) memset(a, 0, sizeof(a))
#define fillinf(a) memset(a, 0xff, sizeof(a))
#define stdf(a, b)                                                             \\
  freopen(a, "r", stdin);                                                      \\
  freopen(b, "w", stdout);
#define inputf(a) freopen(a, "r", stdin);
#define outputf(a) freopen(a, "w", stdout);
#define sz(a) a.size()
#define all(a) a.begin(), a.end()
#define MOD 1000000007
typedef long long ll;
typedef long double ld;
using namespace std;

string s;
bool can[200010];
int a[100010], b[100010];
int main(int argc, char **argv) {
  ios::sync_with_stdio(0);
  int n, k, c, d;
  cin >> n >> k >> c >> s;
  rep(i, n) {
    if (s[i] == 'o')
      can[i] = 1;
  }
  int pos = 0;
  rep(i, k) {
    // cout<<i<<" "<<pos<<endl;
    a[i] = pos;
    pos += c + 1;
    while (!can[pos])
      pos++;
  }
  pos = n - 1;
  for (int i = k - 1; i >= 0; i--) {
    b[i] = pos;
    pos -= c + 1;
    while (!can[pos])
      pos--;
  }
  rep(i, k) {
    if (a[i] == b[i]) {
      cout << a[i] + 1 << endl;
    }
  }
  return 0;
}