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

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ul;
typedef signed long long ll;

mt19937 mt(time(nullptr));
// uniform_int_distribution< int > rand_int(0, 10) // rand[0, 10] inclusive

void solve(int idx, int c, vector<int> &dp, vector<bool> &v,
           vector<set<int>> &dpl) {
  if (v[idx])
    return;
  v[idx] = true;
  dpl[dp[idx]].insert(idx);
  for (int i = idx - c - 1; i >= 0; --i) {
    if (dp[i] > 0 && dp[idx] - dp[i] >= 2)
      break;
    solve(i, c, dp, v, dpl);
  }
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed;
  int n, k, c;
  string s;
  cin >> n >> k >> c >> s;
  vector<int> dp(n, 0);
  vector<vector<int>> d(k + 1, vector<int>(0));
  d[0].push_back(-c - 1);
  int largest = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'x')
      continue;
    dp[i] = (d[largest][0] + c < i ? largest + 1 : largest);
    /*
    for (int j=i-c-1; j>=0; --j) {
      if (dp[j] > 0 && dp[j] <= dp[i]-2) break;
      dp[i] = max(dp[i], dp[j]+1);
    }
    */
    if (dp[i] > k)
      return 0;
    largest = dp[i];
    d[dp[i]].push_back(i);
  }
  // for (int i=0; i<n; ++i) cout << dp[i];
  // cout << endl;
  /*
  vector< set< int > > dpl(k+1, set< int >());
  vector< bool > v(n, false);
  for (int i=n-1; i>=0; --i) {
    if (v[i]) continue;
    if (dp[i] < k) continue;
    solve(i, c, dp, v, dpl);
  }
  for (int i=1; i<=k; ++i) {
    if (dpl[i].size() == 1) cout << *(dpl[i].begin())+1 << endl;
  }
  */
  vector<int> dpl(k + 1, -1), dpr(k + 1, -1);
  int cur = -1;
  for (int i = n - 1; i >= 0; --i) {
    if (dp[i] == 0)
      continue;
    if (cur == -1) {
      dpr[dp[i]] = i;
      cur = dp[i];
    } else if (i + c < dpr[cur]) {
      dpr[dp[i]] = i;
      cur = dp[i];
    }
    dpl[dp[i]] = i;
  }
  for (int i = 1; i <= k; ++i) {
    // cout << dpl[i] << " " << dpr[i] << endl;
    if (dpl[i] == dpr[i])
      cout << dpr[i] + 1 << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ul;
typedef signed long long ll;

mt19937 mt(time(nullptr));
// uniform_int_distribution< int > rand_int(0, 10) // rand[0, 10] inclusive

void solve(int idx, int c, vector<int> &dp, vector<bool> &v,
           vector<set<int>> &dpl) {
  if (v[idx])
    return;
  v[idx] = true;
  dpl[dp[idx]].insert(idx);
  for (int i = idx - c - 1; i >= 0; --i) {
    if (dp[i] > 0 && dp[idx] - dp[i] >= 2)
      break;
    solve(i, c, dp, v, dpl);
  }
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed;
  int n, k, c;
  string s;
  cin >> n >> k >> c >> s;
  vector<int> dp(n, 0);
  vector<vector<int>> d(k + 1, vector<int>(0));
  d[0].push_back(-c - 1);
  int largest = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'x')
      continue;
    dp[i] = (d[largest][0] + c < i ? largest + 1 : largest);
    /*
    for (int j=i-c-1; j>=0; --j) {
      if (dp[j] > 0 && dp[j] <= dp[i]-2) break;
      dp[i] = max(dp[i], dp[j]+1);
    }
    */
    if (dp[i] > k)
      return 0;
    largest = dp[i];
    d[dp[i]].push_back(i);
  }
  // for (int i=0; i<n; ++i) cout << dp[i];
  // cout << endl;
  vector<set<int>> dpl(k + 1, set<int>());
  vector<bool> v(n, false);
  for (int i = n - 1; i >= 0; --i) {
    if (v[i])
      continue;
    if (dp[i] < k)
      continue;
    solve(i, c, dp, v, dpl);
  }
  for (int i = 1; i <= k; ++i) {
    if (dpl[i].size() == 1)
      cout << *(dpl[i].begin()) + 1 << endl;
  }
  return 0;
}