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

// Beginner161.cpp : This file contains the 'main' function. Program execution
// begins and ends there.
//

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

// ll getResult(ll n, ll k)
//{
//	if (k == 1 || n == k) return 0;
//	if (n > k)
//	{
//		auto div = n / k;
//		return min(abs(n - div * k), abs(n - (div+1)*k));
//	}
//	else
//	{
//		return min(n, k-n);
//	}
// }

void getNextNumber(string &number) {
  bool allNine = true;
  for (int i = 0; i < number.size(); ++i) {
    if (number[i] != '9') {
      allNine = false;
      break;
    }
  }

  if (allNine) {
    string zeros(number.size(), '0');
    number = "1" + zeros;
    return;
  }

  for (int i = number.size() - 1; i > 0; --i) {
    if (number[i] - number[i - 1] < 1) {
      string c = to_string(number[i] - '0' + 1);
      number[i] = c[0];
      return;
    }
  }
  int startNumer = number[0] - '0' + 1;
  for (int i = 0; i < number.size(); ++i) {
    int curNumber = max(0, startNumer - i);
    string c = to_string(curNumber);
    number[i] = c[0];
  }
}

void getGraphs(const string &s, int k, int c, vector<vector<int>> &graphs,
               vector<int> &curGraph, int curPos,
               map<int, vector<vector<int>>> &mem_graphs) {
  if (curGraph.size() == k) {
    graphs.push_back(curGraph);
    return;
  }

  if (mem_graphs.count(curPos) > 0) {
  }

  for (int i = curPos + 1; i < s.size(); ++i) {
    if (s[i] == 'x')
      continue;
    if (!curGraph.empty() && i - curPos <= c)
      continue;
    vector<int> g1 = curGraph;
    g1.push_back(i);
    getGraphs(s, k, c, graphs, g1, i, mem_graphs);
  }
}

bool f(long long x, long long y) {
  while (x % y == 0) {
    x /= y;
  }
  return x % y == 1;
}

int main() {
  // A
  /*int a, b, c;
  cin >> a >> b >> c;
  cout << c << ' ' << a << ' ' << b << '\\n';*/

  // B
  /*int n, m;
  cin >> n >> m;
  vector<int> a(n);

  int c = 0;
  double t = 1.0 / (4 * m);

  int sum = 0;
  for (int i = 0; i < n; ++i)
  {
          cin >> a[i];
          sum += a[i];
  }
  for (int i = 0; i < n; ++i)
  {
          double aCoef = a[i] * 1.0 / sum;
          if (aCoef >= t) c++;
  }

  cout << (c >= m ? "Yes" : "No");*/

  // C
  /*ll n, k;
  cin >> n >> k;

  auto result = getResult(n, k);
  cout << result;*/

  // D
  /*int k;
  cin >> k;
  if (k <= 12) {
          cout << k;
          return 0;
  }
  queue<ll> pq;
  for (int i = 1; i <= 9; i++)
          pq.push(i);

  int cnt = 0;
  ll ans;
  while (true) {
          ll num = pq.front();
          pq.pop();
          cnt++;

          if (cnt == k) {
                  ans = num;
                  break;
          }

          int ld = num % 10;
          ll num1;

          if (ld != 0) {
                  num1 = num * 10 + ld - 1;
                  pq.push(num1);
          }

          num1 = num * 10 + ld;
          pq.push(num1);

          if (ld != 9) {
                  num1 = num * 10 + ld + 1;
                  pq.push(num1);
          }
  }
  cout << ans;*/

  // E
  int n, k, c;
  cin >> n >> k >> c;
  string s;
  cin >> s;

  vector<int> dpl(n + 1); // максимальное число рабочих дней среди первых i дней
  vector<int> dpr(n +
                  1); // максимальное число рабочих дней среди последних j дней

  dpl[0] = 0;
  dpr[0] = 0;
  int lastWorkingDay = INT_MIN;
  for (int i = 1; i <= n; ++i) {
    if (s[i - 1] == 'x')
      dpl[i] = dpl[i - 1];
    else {
      if (lastWorkingDay + c < i) {
        dpl[i] = dpl[i - 1] + 1;
        lastWorkingDay = i;
      } else {
        dpl[i] = dpl[i - 1];
      }
    }
  }

  lastWorkingDay = INT_MIN;
  for (int j = 1; j <= n; ++j) {
    if (s[n - j] == 'x')
      dpr[j] = dpr[j - 1];
    else {
      if (lastWorkingDay + c < j) {
        dpr[j] = dpr[j - 1] + 1;
        lastWorkingDay = j;
      } else {
        dpr[j] = dpr[j - 1];
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    if (s[i] == 'x')
      continue;
    int left = i; // максимальное число рабочих дней слева (не включая этот)
    int right =
        n - 1 - i; // максимальное число рабочих дней справа (не включая этот)
    if (dpl[left] + dpr[right] < k) {
      cout << i + 1 << '\\n';
    }
  }
}


Error: 0

Fixed submission:

// Beginner161.cpp : This file contains the 'main' function. Program execution
// begins and ends there.
//

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

// ll getResult(ll n, ll k)
//{
//	if (k == 1 || n == k) return 0;
//	if (n > k)
//	{
//		auto div = n / k;
//		return min(abs(n - div * k), abs(n - (div+1)*k));
//	}
//	else
//	{
//		return min(n, k-n);
//	}
// }

void getNextNumber(string &number) {
  bool allNine = true;
  for (int i = 0; i < number.size(); ++i) {
    if (number[i] != '9') {
      allNine = false;
      break;
    }
  }

  if (allNine) {
    string zeros(number.size(), '0');
    number = "1" + zeros;
    return;
  }

  for (int i = number.size() - 1; i > 0; --i) {
    if (number[i] - number[i - 1] < 1) {
      string c = to_string(number[i] - '0' + 1);
      number[i] = c[0];
      return;
    }
  }
  int startNumer = number[0] - '0' + 1;
  for (int i = 0; i < number.size(); ++i) {
    int curNumber = max(0, startNumer - i);
    string c = to_string(curNumber);
    number[i] = c[0];
  }
}

void getGraphs(const string &s, int k, int c, vector<vector<int>> &graphs,
               vector<int> &curGraph, int curPos) {
  if (curGraph.size() == k) {
    graphs.push_back(curGraph);
    return;
  }

  for (int i = curPos + 1; i < s.size(); ++i) {
    if (s[i] == 'x')
      continue;
    if (!curGraph.empty() && i - curPos <= c)
      continue;
    vector<int> g1 = curGraph;
    g1.push_back(i);
    getGraphs(s, k, c, graphs, g1, i);
  }
}

int main() {
  // A
  /*int a, b, c;
  cin >> a >> b >> c;
  cout << c << ' ' << a << ' ' << b << '\\n';*/

  // B
  /*int n, m;
  cin >> n >> m;
  vector<int> a(n);

  int c = 0;
  double t = 1.0 / (4 * m);

  int sum = 0;
  for (int i = 0; i < n; ++i)
  {
          cin >> a[i];
          sum += a[i];
  }
  for (int i = 0; i < n; ++i)
  {
          double aCoef = a[i] * 1.0 / sum;
          if (aCoef >= t) c++;
  }

  cout << (c >= m ? "Yes" : "No");*/

  // C
  /*ll n, k;
  cin >> n >> k;

  auto result = getResult(n, k);
  cout << result;*/

  // D

  /*string s = "9909";
  getNextNumber(s);
  cout << s;

  int k;
  cin >> k;

  string number = "0";
  for (int i = 0; i < k; ++i)
  {
          getNextNumber(number);
  }
  cout << number;*/

  // E
  int n, k, c;
  cin >> n >> k >> c;
  string s;
  cin >> s;

  // map<int, vector<int>> graphs; //графы

  vector<vector<int>> graphs;
  vector<int> curGraph;
  getGraphs(s, k, c, graphs, curGraph, -1);

  vector<bool> workDays(n, true);
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'x')
      workDays[i] = false;
  }

  if (graphs.empty())
    return 0;

  vector<int> res = graphs[0];

  for (int i = 0; i < graphs.size(); ++i) {
    vector<int> tmp;
    set_intersection(res.begin(), res.end(), graphs[i].begin(), graphs[i].end(),
                     back_inserter(tmp));
    res = tmp;
  }

  for (auto &item : res) {
    cout << item + 1 << '\\n';
  }
}