Problem p02721 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <random>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
typedef vector<int> vi;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

static uint MD = mod;
struct ModInt {
  using M = ModInt;
  uint v;
  ModInt(ll _v = 0) { set_v(uint(_v % MD + MD)); }
  M &set_v(uint _v) {
    v = (_v < MD) ? _v : _v - MD;
    return *this;
  }
  explicit operator bool() const { return v != 0; }
  M operator-() const { return M() - *this; }
  M operator+(const M &r) const { return M().set_v(v + r.v); }
  M operator-(const M &r) const { return M().set_v(v + MD - r.v); }
  M operator*(const M &r) const { return M().set_v(uint(ull(v) * r.v % MD)); }
  M operator/(const M &r) const { return *this * r.inv(); }
  M &operator+=(const M &r) { return *this = *this + r; }
  M &operator-=(const M &r) { return *this = *this - r; }
  M &operator*=(const M &r) { return *this = *this * r; }
  M &operator/=(const M &r) { return *this = *this / r; }
  bool operator==(const M &r) const { return v == r.v; }
  M pow(ll n) const {
    M x = *this, r = 1;
    while (n) {
      if (n & 1)
        r *= x;
      x *= x;
      n >>= 1;
    }
    return r;
  }
  M inv() const { return pow(MD - 2); }
  friend ostream &operator<<(ostream &os, const M &r) { return os << r.v; }
};
using Mint = ModInt;
// head

const int MAXN = 200000 + 5;
int tarv[MAXN];
int tarv2[MAXN];
int ccc[MAXN];
string s;
int k, c;

int main() {
  ios::sync_with_stdio(false);
#ifdef RG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n >> k >> c;
  cin >> s;

  int ii = 0;
  for (ii = n - 1; ii >= 0; --ii) {
    if (s[ii] == 'x')
      tarv[ii] = 0;
    else
      break;
  }
  // tarv[n - 1] = (s[n - 1] == 'o' ? 1 : 0);
  tarv[ii] = 1;
  int cnt = 0;
  per(i, 0, ii) {
    cnt++;
    if (cnt < c + 1 || s[i] == 'x') {
      tarv[i] = tarv[i + 1];
      continue;
    } else if (s[i] == 'o') {
      tarv[i] = tarv[i + 1] + 1;
      cnt = 0;
    }
  }

  ii = 0;
  for (ii = 0; ii < n; ++ii) {
    if (s[ii] == 'x')
      tarv2[ii] = k + 1;
    else
      break;
  }
  // tarv2[0] = (s[0] == 'o' ? k : k+1);
  tarv2[ii] = k;
  cnt = 0;
  rep(i, ii + 1, n) {
    cnt++;
    if (cnt < c + 1 || s[i] == 'x') {
      tarv2[i] = tarv2[i - 1];
      continue;
    } else if (s[i] == 'o') {
      tarv2[i] = tarv2[i - 1] - 1;
      cnt = 0;
    }
  }

  // rep(i, 0, n) cout << tarv[i];
  // cout << endl;
  // rep(i, 0, n) cout << tarv2[i];
  // cout << endl;

  if (tarv[0] == k) {
    for (int i = 0; i < n; i++) {
      if (s[i] == 'o' && tarv[i] == tarv2[i]) {
        ccc[tarv[i]]++;
      }
    }
    rep(i, 0, n) {
      if (s[i] == 'o' && tarv[i] == tarv2[i] && ccc[tarv[i]] == 1) {
        cout << i + 1 << endl;
      }
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <random>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
typedef vector<int> vi;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

static uint MD = mod;
struct ModInt {
  using M = ModInt;
  uint v;
  ModInt(ll _v = 0) { set_v(uint(_v % MD + MD)); }
  M &set_v(uint _v) {
    v = (_v < MD) ? _v : _v - MD;
    return *this;
  }
  explicit operator bool() const { return v != 0; }
  M operator-() const { return M() - *this; }
  M operator+(const M &r) const { return M().set_v(v + r.v); }
  M operator-(const M &r) const { return M().set_v(v + MD - r.v); }
  M operator*(const M &r) const { return M().set_v(uint(ull(v) * r.v % MD)); }
  M operator/(const M &r) const { return *this * r.inv(); }
  M &operator+=(const M &r) { return *this = *this + r; }
  M &operator-=(const M &r) { return *this = *this - r; }
  M &operator*=(const M &r) { return *this = *this * r; }
  M &operator/=(const M &r) { return *this = *this / r; }
  bool operator==(const M &r) const { return v == r.v; }
  M pow(ll n) const {
    M x = *this, r = 1;
    while (n) {
      if (n & 1)
        r *= x;
      x *= x;
      n >>= 1;
    }
    return r;
  }
  M inv() const { return pow(MD - 2); }
  friend ostream &operator<<(ostream &os, const M &r) { return os << r.v; }
};
using Mint = ModInt;
// head

set<int> set_intersection(set<int> a, set<int> b) {
  set<int> ans;
  for (int val : a) {
    if (b.find(val) != b.end()) {
      ans.insert(val);
    }
  }
  return ans;
}

const int MAXN = 200000 + 5;
int tarv[MAXN];
set<int> tars[MAXN];
string s;
int k, c;
set<int> dp(int st, int &cnt, int tar) {
  int len = sz(s);
  if (st >= len || tar == 0) {
    cnt = 0;
    return set<int>();
  }
  if (tarv[st] != -1) {
    cnt = tarv[st];
    return tars[st];
  }
  int i;
  for (i = st; i < len; ++i) {
    if (s[i] == 'o')
      break;
  }
  int cc = 0;
  set<int> tt = dp(i + c + 1, cc, tar - 1);
  if (cc < tar - 1) {
    cnt = 1;
    tarv[st] = 1;
    tars[st] = set<int>();
    return set<int>();
  }
  bool flag = true;
  rep(j, i + 1, len) {
    if (s[j] == 'x')
      continue;
    set<int> t2 = dp(j + c + 1, cc, tar - 1);
    if (cc < tar - 1)
      break;
    flag = false;
    tt = set_intersection(tt, t2);
    if (tt.size() == 0)
      break;
  }
  if (flag)
    tt.insert(i);
  cnt = tar;
  tarv[st] = tar;
  tars[st] = tt;
  return tt;
}

int main() {
  ios::sync_with_stdio(false);
#ifdef RG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n >> k >> c;
  cin >> s;
  int cnt;
  memset(tarv, -1, sizeof(tarv));
  set<int> ans = dp(0, cnt, k);
  if (cnt >= k) {
    for (int val : ans)
      cout << val + 1 << endl;
  }
  return 0;
}