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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LL long long
#define pii pair<int, int>
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define pd(x) printf("%d\\n", x)
#define plld(x) printf("%lld\\n", x)
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define mem(a) memset(a, 0, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define fast_io ios::sync_with_stdio(false)

const int INF = 1e9;
const LL mod = 1e9 + 7;
const int maxn = 2e5 + 7;

char s[maxn];
int L[maxn], R[maxn];

int main() {
  int n, k, c;
  while (~scanf("%d%d%d", &n, &k, &c)) {
    scanf("%s", s);
    int len = strlen(s);
    int cnt = 0;
    rep(i, 0, len - 1) {
      if (s[i] == 'o') {
        L[++cnt] = i + 1;
        i += c;
      }
      if (cnt == k)
        break;
    }
    cnt = 0;
    per(i, len - 1, 0) {
      if (s[i] == 'o') {
        R[++cnt] = i + 1;
        i -= c;
      }
      if (cnt == k)
        break;
    }
    rep(i, 1, k) {
      //            cout << L[i] << " " << R[i] << endl;
      if (L[i] == R[k + 1 - i]) {
        pd(L[i]);
      }
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LL long long
#define pii pair<int, int>
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define pd(x) printf("%d\\n", x)
#define plld(x) printf("%lld\\n", x)
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (b); i >= (a); i--)
#define mem(a) memset(a, 0, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define fast_io ios::sync_with_stdio(false)

const int INF = 1e9;
const LL mod = 1e9 + 7;
const int maxn = 2e5 + 7;

inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + (c - '0');
    c = getchar();
  }
  return x * f;
}

int n, K, c;
int vis[maxn];
char s[maxn];
vector<string> v;
struct node {
  int ch;
  string path;
  int cnt;
};
queue<node> q;

void bfs() {
  while (!q.empty())
    q.pop();

  node k, k1;
  k.ch = 0, k.path = "", k.cnt = 0;
  q.push(k);

  while (!q.empty()) {
    k = q.front();
    q.pop();
    if (k.cnt == K) {
      //            cout << "path = " << k.path << endl;
      v.push_back(k.path);
      continue;
    }
    if (k.ch >= n)
      continue;
    if (s[k.ch] == 'o') {
      k1.ch = k.ch + c + 1;
      k1.cnt = k.cnt + 1;
      k1.path = k.path;
      k1.path += (k.ch + '0');
      q.push(k1);

      k1.ch = k.ch + 1;
      k1.cnt = k.cnt;
      k1.path = k.path;
      q.push(k1);
    } else {
      k1.ch = k.ch + 1;
      k1.cnt = k.cnt;
      k1.path = k.path;
      q.push(k1);
    }
  }
}

int main() {
  scanf("%d%d%d", &n, &K, &c);
  scanf("%s", s);
  bfs();
  int len = v.size() - 1;
  rep(i, 0, len) {
    rep(j, 0, K - 1) { vis[v[i][j] - '0']++; }
  }
  rep(i, 0, n - 1) {
    //            cout << vis[i] << " ";
    if (vis[i] == len + 1) {
      pd(i + 1);
    }
  }
  //        cout << endl;
  return 0;
}