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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int mod = 1e9 + 7;
const int maxn = 4e5 + 5;
string s;
int dp[maxn];
int pre[maxn];
int num[maxn];
vector<int> v;
int n, k, c;
void dfs(int pos, int dep) {
  // cout<<pos<<"  12"<<endl;
  int flag = 0;
  for (int i = 0; i < c; i++) {
    // cout<<dp[pos-i]<<"  dp"<<endl;
    if (pos - i > 0 && s[pos - i - 1] == 'o' && pre[pos - i] == 0 &&
        pre[pos + 1] <= 1 && dp[pos - i] == dep) {
      //	cout<<pre[pos+1+c]<<endl;
      //	cout<<pos-i<<endl;
      v.push_back(pos - i);
      dfs(pos - i - c - 1, dep - 1);
      return;
    }
  }
  for (int i = pos; i >= 1; i--) {
    if (s[i - 1] == 'o') {
      if (pre[i] == 0 && pre[i + c + 1] <= 1)
        v.push_back(i);
      int flag = 0;
      for (int j = 1; j <= c; j++) {
        if ((i - j - 1) >= 0 && s[i - j - 1] == 'o' && dp[i - j] == dep) {
          break;
        }
        if ((i - j - 1) >= 0 && s[i - j - 1] == 'o' && dp[i - j] == dep - 1) {
          flag = 1;
          break;
        }
      }
      // if(dep==n&&num[dep]!=1) flag=0;
      // cout<<i<<endl;
      // cout<<1111<<endl;
      if (flag && (v.size() == 0 || v[v.size() - 1] != i))
        v.push_back(i);
      dfs(i - c - 1, dep - 1);
      return;
    }
  }
}
int main() {

  while (cin >> n >> k >> c) {
    memset(dp, 0, sizeof(dp));
    memset(pre, 0, sizeof(pre));
    memset(num, 0, sizeof(num));
    v.clear();
    cin >> s;
    for (int i = 1; i <= c; i++) {
      if (s[i - 1] == 'o') {
        dp[i] = 1, num[dp[i]]++;
        //	cout<<num[dp[i]]<<" ";
      } else
        dp[i] = dp[i - 1];
    }

    // cout<<endl;
    for (int i = c + 1; i <= n; i++) {
      if (s[i - 1] == 'o')
        dp[i] = dp[i - c - 1] + 1, num[dp[i]]++;
      else
        dp[i] = dp[i - 1];
    }
    for (int i = 1; i <= n + 1; i++) {
      if (i > 1)
        pre[i] = pre[i - 1] + (s[i - 2] == 'o' ? 1 : 0);
      if (i > c + 1) {
        pre[i] = pre[i] - (s[i - c - 2] == 'o' ? 1 : 0);
      }
    }
    for (int i = 2; i <= c + 10; i++) {
      pre[i + n] = pre[i + n - 1];
    }
    //	for(int i=1;i<=n+c;i++)
    //		cout<<pre[i]<<" ";
    if (dp[n] != k) {
      return 0;
      // cout<<11111<<endl;
      // cout<<0<<endl;
      // continue;
    } else {
      dfs(n, k);
    }
    if (v.size() == 0) {
      return 0;
      // cout<<v.size()<<endl;
      // continue;
    }
    // cout<<1111<<endl;
    sort(v.begin(), v.end());
    // cout<<v.size()<<endl;
    for (int i = 0; i < v.size(); i++) {
      cout << v[i] << "\\n";
    }
    // break;
  }

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 5;
string s;
int dp[maxn];
int pre[maxn];
int num[maxn];
vector<int> v;
int n, k, c;
void dfs(int pos, int dep) {
  // cout<<pos<<"  12"<<endl;
  int flag = 0;
  for (int i = 0; i < c; i++) {
    // cout<<dp[pos-i]<<"  dp"<<endl;
    if (pos - i > 0 && s[pos - i - 1] == 'o' && pre[pos - i] == 0 &&
        pre[pos + 1] <= 1 && dp[pos - i] == dep) {
      //	cout<<pos-i<<endl;
      v.push_back(pos - i);
      dfs(pos - i - c - 1, dep - 1);
      return;
    }
  }
  for (int i = pos; i >= 1; i--) {
    if (s[i - 1] == 'o') {
      if (pre[i] == 0 && pre[i + c + 1] <= 1)
        v.push_back(i);
      int flag = 0;
      for (int j = 1; j <= c; j++) {
        if (s[i - j - 1] == 'o' && dp[i - j] == dep) {
          break;
        }
        if (s[i - j - 1] == 'o' && dp[i - j] == dep - 1) {
          flag = 1;
          break;
        }
      }
      if (flag && v[v.size() - 1] != i)
        v.push_back(i);
      dfs(i - c - 1, dep - 1);
      return;
    }
  }
}
int main() {

  while (cin >> n >> k >> c) {
    memset(dp, 0, sizeof(dp));
    memset(pre, 0, sizeof(pre));
    memset(num, 0, sizeof(num));
    v.clear();
    cin >> s;
    for (int i = 1; i <= c; i++) {
      if (s[i - 1] == 'o') {
        dp[i] = 1, num[dp[i]]++;
        //	cout<<num[dp[i]]<<" ";
      } else
        dp[i] = dp[i - 1];
    }

    // cout<<endl;
    for (int i = c + 1; i <= n; i++) {
      if (s[i - 1] == 'o')
        dp[i] = dp[i - c - 1] + 1, num[dp[i]]++;
      else
        dp[i] = dp[i - 1];
    }
    for (int i = 1; i <= n; i++) {
      if (i > 1)
        pre[i] = pre[i - 1] + (s[i - 2] == 'o' ? 1 : 0);
      if (i > c + 1) {
        pre[i] = pre[i] - (s[i - c - 2] == 'o' ? 1 : 0);
      }
    }
    //	for(int i=1;i<=n;i++)
    //		cout<<dp[i]<<" ";
    if (dp[n] != k) {
      // return 0;
      // cout<<11111<<endl;
      cout << 0 << endl;
      continue;
    } else {
      dfs(n, k);
    }
    if (v.size() == 0) {
      // return 0;
      cout << v.size() << endl;
      continue;
    }
    // cout<<1111<<endl;
    sort(v.begin(), v.end());
    // cout<<v.size()<<endl;
    for (int i = 0; i < v.size(); i++) {
      cout << v[i] << "\\n";
    }
    // break;
  }

  return 0;
}