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

/*
    飲んだ魔剤で家が建つ。
    created at: 2020-04-05 18:56:19
*/

#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define rev(x) greater<x>()
#define MOD 1000000007
#define INF 1000000000

typedef long long LL;
typedef long double LD;

bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

LL modpower(LL n, LL m, LL mod) {
  if (m == 0) {
    return 1;
  } else if (m % 2 == 0) {
    LL p = modpower(n, m / 2, mod);
    return p * p % mod;
  } else {
    return n * modpower(n, m - 1, mod) % mod;
  }
}

LL modinv(LL n, LL mod) { return modpower(n, mod - 2, mod); }

LL modcombination(LL n, LL r, LL mod) {
  LL ret = 1;

  for (int i = 0; i < r; i++) {
    ret *= (n - i);
    ret %= mod;
    ret *= modinv(i + 1, mod);
    ret % mod;
  }

  return ret % mod;
}

int gcd(int k, int l) {
  if (l > 0)
    return gcd(l, k % l);
  else
    return k;
}

struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n) {
    for (int i = 0; i < n; i++)
      par[i] = i;
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int n, k, c;
  cin >> n >> k >> c;
  string s;
  cin >> s;

  int l[k], r[k];
  int x = 0;
  int h = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'o' && h == 0) {
      if (x < k)
        l[x++] = i;
      h = c;
    } else if (h > 0) {
      h--;
    }
  }

  x = k - 1;
  h = 0;
  for (int i = n - 1; i > -1; i--) {
    if (s[i] == 'o' && h == 0) {
      if (x >= 0)
        r[x--] = i;
      h = c;
    } else if (h > 0) {
      h--;
    }
  }

  for (int i = 0; i < k; i++) {
    if (l[i] == r[i])
      cout << l[i] + 1 << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

/*
    飲んだ魔剤で家が建つ。
    created at: 2020-04-05 18:56:19
*/

#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define rev(x) greater<x>()
#define MOD 1000000007
#define INF 1000000000

typedef long long LL;
typedef long double LD;

bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

LL modpower(LL n, LL m, LL mod) {
  if (m == 0) {
    return 1;
  } else if (m % 2 == 0) {
    LL p = modpower(n, m / 2, mod);
    return p * p % mod;
  } else {
    return n * modpower(n, m - 1, mod) % mod;
  }
}

LL modinv(LL n, LL mod) { return modpower(n, mod - 2, mod); }

LL modcombination(LL n, LL r, LL mod) {
  LL ret = 1;

  for (int i = 0; i < r; i++) {
    ret *= (n - i);
    ret %= mod;
    ret *= modinv(i + 1, mod);
    ret % mod;
  }

  return ret % mod;
}

int gcd(int k, int l) {
  if (l > 0)
    return gcd(l, k % l);
  else
    return k;
}

struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n) {
    for (int i = 0; i < n; i++)
      par[i] = i;
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int n, k, c;
  cin >> n >> k >> c;
  string s;
  cin >> s;

  int l[k], r[k];
  int x = 0;
  int h = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'o' && h == 0) {
      l[x++] = i;
      h = c;
    } else if (h > 0) {
      h--;
    }
  }

  x = k - 1;
  h = 0;
  for (int i = n - 1; i > -1; i--) {
    if (s[i] == 'o' && h == 0) {
      r[x--] = i;
      h = c;
    } else if (h > 0) {
      h--;
    }
  }

  for (int i = 0; i < k; i++) {
    if (l[i] == r[i])
      cout << l[i] + 1 << endl;
  }

  return 0;
}