Problem p03904 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <utility>
#include <vector>

using namespace std;

template <typename T, typename S>
istream &operator>>(istream &is, pair<T, S> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v) {
    is >> x;
  }
  return is;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto &x : v) {
    os << x << " ";
  }
  os << "\n";
  return os;
}

vector<int> BuildSuffArray(const string &S) {
  const int LEN = S.size();
  const int ALPHABET = 11;

  vector<int> p(LEN), cnt(ALPHABET, 0), c(LEN);
  for (auto c : S)
    cnt[int(c) + 1 - int('0')]++;
  for (int i = 1; i < ALPHABET; ++i)
    cnt[i] += cnt[i - 1];
  for (int i = 0; i < LEN; ++i)
    p[--cnt[int(S[i]) + 1 - int('0')]] = i;
  c[p[0]] = 0;
  int classes = 1;
  for (int i = 1; i < LEN; ++i) {
    if (S[p[i]] != S[p[i - 1]])
      ++classes;
    c[p[i]] = classes - 1;
  }
  // cout << p;

  vector<int> pn(LEN), cn(LEN);
  for (int h = 0; (1 << h) < LEN; ++h) {
    for (int i = 0; i < LEN; ++i) {
      pn[i] = p[i] - (1 << h);
      if (pn[i] < 0)
        pn[i] += LEN;
    }
    cnt.assign(classes, 0);
    for (int i = 0; i < LEN; ++i)
      ++cnt[c[pn[i]]];
    for (int i = 1; i < classes; ++i)
      cnt[i] += cnt[i - 1];
    for (int i = LEN - 1; i >= 0; --i)
      p[--cnt[c[pn[i]]]] = pn[i];
    cn[p[0]] = 0;
    classes = 1;
    for (int i = 1; i < LEN; ++i) {
      int mid1 = (p[i] + (1 << h)) % LEN, mid2 = (p[i - 1] + (1 << h)) % LEN;
      if (c[p[i]] != c[p[i - 1]] || c[mid1] != c[mid2])
        ++classes;
      cn[p[i]] = classes - 1;
    }
    c = cn;
    // cout << p;
  }
  // cout << endl;
  return p;
}

int MinParts(const string &S, const vector<int> &pos, const int L,
             const int max_pos) {
  // cout << max_pos << "!\n";
  int N = S.size() - 1;

  int cur_idx = 0;
  int num_parts = 0;
  while (cur_idx < N && num_parts <= N) {
    // cout << cur_idx << " " << pos[cur_idx] << " ";
    if (pos[cur_idx] <= max_pos) {
      // cout << L << "\n";
      cur_idx += L;
    } else {
      // cout << L - 1 << "\n";
      cur_idx += L - 1;
    }
    ++num_parts;
  }
  // cout << num_parts << "!!\n";
  return num_parts;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int K;
  string S;
  cin >> K >> S;
  int N = S.size();
  int L = (N + K) / (K + 1);

  S.push_back('0' - 1);
  auto order = BuildSuffArray(S);

  vector<int> pos(N);
  for (int i = 0; i <= N; ++i)
    pos[order[i]] = i;

  int l = 0, r = N;
  while (r - l > 1) {
    int t = (r + l) / 2;
    if (MinParts(S, pos, L, t) <= K + 1) {
      r = t;
    } else {
      l = t;
    }
  }
  int ans_st = order[r];
  for (int i = ans_st; i < ans_st + L; ++i) {
    if (i < N)
      cout << S[i];
    else
      cout << '0';
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <utility>
#include <vector>

using namespace std;

template <typename T, typename S>
istream &operator>>(istream &is, pair<T, S> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v) {
    is >> x;
  }
  return is;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto &x : v) {
    os << x << " ";
  }
  os << "\n";
  return os;
}

vector<int> BuildSuffArray(const string &S) {
  const int LEN = S.size();
  const int ALPHABET = 11;

  vector<int> p(LEN), cnt(ALPHABET, 0), c(LEN);
  for (auto c : S)
    cnt[int(c) + 1 - int('0')]++;
  for (int i = 1; i < ALPHABET; ++i)
    cnt[i] += cnt[i - 1];
  for (int i = 0; i < LEN; ++i)
    p[--cnt[int(S[i]) + 1 - int('0')]] = i;
  c[p[0]] = 0;
  int classes = 1;
  for (int i = 1; i < LEN; ++i) {
    if (S[p[i]] != S[p[i - 1]])
      ++classes;
    c[p[i]] = classes - 1;
  }
  // cout << p;

  vector<int> pn(LEN), cn(LEN);
  for (int h = 0; (1 << h) < LEN; ++h) {
    for (int i = 0; i < LEN; ++i) {
      pn[i] = p[i] - (1 << h);
      if (pn[i] < 0)
        pn[i] += LEN;
    }
    cnt.assign(classes, 0);
    for (int i = 0; i < LEN; ++i)
      ++cnt[c[pn[i]]];
    for (int i = 1; i < classes; ++i)
      cnt[i] += cnt[i - 1];
    for (int i = LEN - 1; i >= 0; --i)
      p[--cnt[c[pn[i]]]] = pn[i];
    cn[p[0]] = 0;
    classes = 1;
    for (int i = 1; i < LEN; ++i) {
      int mid1 = (p[i] + (1 << h)) % LEN, mid2 = (p[i - 1] + (1 << h)) % LEN;
      if (c[p[i]] != c[p[i - 1]] || c[mid1] != c[mid2])
        ++classes;
      cn[p[i]] = classes - 1;
    }
    c = cn;
    // cout << p;
  }
  // cout << endl;
  return p;
}

int MinParts(const string &S, const vector<int> &pos, const int L,
             const int max_pos) {
  // cout << max_pos << "!\n";
  int N = S.size() - 1;

  int cur_idx = 0;
  int num_parts = 0;
  while (cur_idx < N && num_parts <= N) {
    // cout << cur_idx << " " << pos[cur_idx] << " ";
    if (pos[cur_idx] <= max_pos) {
      // cout << L << "\n";
      cur_idx += L;
    } else {
      // cout << L - 1 << "\n";
      cur_idx += L - 1;
    }
    ++num_parts;
  }
  // cout << num_parts << "!!\n";
  return num_parts;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int K;
  string S;
  cin >> K >> S;
  int N = S.size();
  int L = (N + K) / (K + 1);

  S.push_back('0' - 1);
  auto order = BuildSuffArray(S);

  vector<int> pos(N + 1);
  for (int i = 0; i <= N; ++i)
    pos[order[i]] = i;

  int l = 0, r = N;
  while (r - l > 1) {
    int t = (r + l) / 2;
    if (MinParts(S, pos, L, t) <= K + 1) {
      r = t;
    } else {
      l = t;
    }
  }
  int ans_st = order[r];
  for (int i = ans_st; i < ans_st + L; ++i) {
    if (i < N)
      cout << S[i];
    else
      cout << '0';
  }
}