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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
string s;
int n, k, c;
vector<int> v;
pair<int, int> d[200001]; // i부터 시작해서 얻을 수 있는 최대 개수
pair<int, int> d2[200001];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> k >> c;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'o') {
      v.push_back(i);
    }
  }

  for (int i = 0; i < v.size(); i++) {
    if (i == 0) {
      d[i].first = 1;
      d[i].second = v[i];
    } else {
      if (v[i] - d[i - 1].second > c) {
        d[i].first = d[i - 1].first + 1;
        d[i].second = v[i];
      } else {
        d[i] = d[i - 1];
      }
    }
  }

  for (int i = v.size() - 1; i >= 0; i--) {
    if (i == v.size() - 1) {
      d2[i].first = 1;
      d2[i].second = v[i];
    } else {
      if (d2[i + 1].second - v[i] > c) {
        d2[i].first = d2[i + 1].first + 1;
        d2[i].second = v[i];
      } else {
        d2[i] = d2[i + 1];
      }
    }
  }

  if (d[v.size() - 1].first < k || d[v.size() - 1].first > k) {
    return 0;
  }

  vector<int> ans;
  if (v.size() == 1) {
    cout << v[0] + 1 << '\\n';
    return 0;
  }
  if (d2[1].first < k) {
    ans.push_back(v[0]);
  }
  if (d[v.size() - 2].first < k) {
    ans.push_back(v[v.size() - 1]);
  }

  for (int i = 1; i <= v.size() - 2; i++) {
    if (d[i].second == v[i]) {
      int sum = d[i - 1].first + d2[i + 1].first;
      if (sum < k) {
        ans.push_back(v[i]);
      }
    }
  }
  sort(ans.begin(), ans.end());
  for (int next : ans) {
    cout << next + 1 << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
string s;
int n, k, c;
vector<int> v;
int d[200001]; // i부터 시작해서 얻을 수 있는 최대 개수
bool check[200001];
map<int, int> m;
int go(int x) {
  if (x == v.size()) {
    return 1;
  }
  if (d[x] != -1)
    return d[x];
  d[x] = 1;
  d[x] = max(d[x], go(x + 1));
  for (int i = x + 1; i < v.size(); i++) {
    if (v[i] - v[x] > c) {
      d[x] = max(d[x], go(i) + 1);
    }
  }
  return d[x];
}

void go2(int x, int t) {
  check[x] = true;
  for (int j = x + 1; j < v.size(); j++) {
    if (d[x] == d[j] + 1 && v[j] - v[x] > c) {
      go2(j, d[j]);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> k >> c;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'o') {
      v.push_back(i);
    }
  }
  memset(d, -1, sizeof(d));
  go(0);
  if (d[0] < k || d[0] > k) {
    return 0;
  }

  for (int i = 0; i < v.size(); i++) {
    if (d[i] >= k) {
      go2(i, d[i]);
    }
  }

  for (int i = 0; i < v.size(); i++) {
    if (check[i]) {
      m[d[i]] += 1;
    }
  }

  vector<int> ans;

  for (int i = 0; i < v.size(); i++) {
    if (check[i] && m[d[i]] == 1 && d[i] <= k) {
      ans.push_back(v[i] + 1);
    }
  }

  for (int next : ans) {
    cout << next << '\\n';
  }
}