Problem p02943 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author majk
 */

#ifndef MAJK_LIB
#define MAJK_LIB

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define x first
#define y second
typedef std::pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<ui, ui> puu;

template <typename T, typename U>
std::istream &operator>>(std::istream &i, pair<T, U> &p) {
  i >> p.x >> p.y;
  return i;
}
template <typename T> std::istream &operator>>(std::istream &i, vector<T> &t) {
  for (auto &v : t) {
    i >> v;
  }
  return i;
}
template <typename T, typename U>
std::ostream &operator<<(std::ostream &o, const pair<T, U> &p) {
  o << p.x << ' ' << p.y;
  return o;
}
template <typename T>
std::ostream &operator<<(std::ostream &o, const vector<T> &t) {
  if (t.empty())
    o << '\n';
  for (size_t i = 0; i < t.size(); ++i) {
    o << t[i] << " \n"[i == t.size() - 1];
  }
  return o;
}
template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using maxheap = priority_queue<T, vector<T>, less<T>>;
ui logceil(ll x) { return x ? 8 * sizeof(ll) - __builtin_clzll(x) : 0; }

namespace std {
template <typename T, typename U> struct hash<pair<T, U>> {
  hash<T> t;
  hash<U> u;
  size_t operator()(const pair<T, U> &p) const {
    return t(p.x) ^ (u(p.y) << 7);
  }
};
} // namespace std
template <typename T, typename F> T bsh(T l, T h, const F &f) {
  T r = -1, m;
  while (l <= h) {
    m = (l + h) / 2;
    if (f(m)) {
      l = m + 1;
      r = m;
    } else {
      h = m - 1;
    }
  }
  return r;
}
template <typename F>
double bshd(double l, double h, const F &f, double p = 1e-9) {
  ui r = 3 + (ui)log2((h - l) / p);
  while (r--) {
    double m = (l + h) / 2;
    if (f(m)) {
      l = m;
    } else {
      h = m;
    }
  }
  return (l + h) / 2;
}
template <typename T, typename F> T bsl(T l, T h, const F &f) {
  T r = -1, m;
  while (l <= h) {
    m = (l + h) / 2;
    if (f(m)) {
      h = m - 1;
      r = m;
    } else {
      l = m + 1;
    }
  }
  return r;
}
template <typename F>
double bsld(double l, double h, const F &f, double p = 1e-9) {
  ui r = 3 + (ui)log2((h - l) / p);
  while (r--) {
    double m = (l + h) / 2;
    if (f(m)) {
      h = m;
    } else {
      l = m;
    }
  }
  return (l + h) / 2;
}
template <typename T> T gcd(T a, T b) {
  if (a < b)
    swap(a, b);
  return b ? gcd(b, a % b) : a;
}

template <typename T> class vector2 : public vector<vector<T>> {
public:
  vector2() {}
  vector2(size_t a, size_t b, T t = T())
      : vector<vector<T>>(a, vector<T>(b, t)) {}
};
template <typename T> class vector3 : public vector<vector2<T>> {
public:
  vector3() {}
  vector3(size_t a, size_t b, size_t c, T t = T())
      : vector<vector2<T>>(a, vector2<T>(b, c, t)) {}
};
template <typename T> class vector4 : public vector<vector3<T>> {
public:
  vector4() {}
  vector4(size_t a, size_t b, size_t c, size_t d, T t = T())
      : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {}
};
template <typename T> class vector5 : public vector<vector4<T>> {
public:
  vector5() {}
  vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T())
      : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {}
};

#endif

#include <type_traits>

// ordinary suffix array with optional LCP and LCP RMQ. look elsewhere
template <typename Index, bool PrecomputeLCP = false,
          bool PrecomputeRMQ = false>
class SuffixArray {
public:
  static_assert(PrecomputeLCP || !PrecomputeRMQ, "Must have RMQ for LCP");

  template <typename T>
  explicit SuffixArray(T t)
      : N(t.size()), S(N), I(N), LCP(PrecomputeLCP ? N : 0) {
    typedef typename std::remove_reference<decltype(t[0])>::type Item;
    vector<pair<Item, Index>> TR(N);
    for (Index i = 0; i < N; ++i) {
      TR[i] = {t[i], i};
    }
    sort(TR.begin(), TR.end());
    vector<Index> R(N);
    Index r = R[TR[0].y] = S[TR[0].y] = 0;
    for (Index i = 1; i < N; ++i) {
      R[TR[i].y] = r += (TR[i - 1].x != TR[i].x);
      S[TR[i].y] = i;
    }
    vector<Index> RA(N), SA(N), C(N);
    for (Index k = 1; k < N; k <<= 1) {
      counting_sort(R, C, SA, k, r + 1);
      counting_sort(R, C, SA, 0, r + 1);
      RA[S[0]] = r = 0;
      for (Index i = 1; i < N; ++i) {
        RA[S[i]] = r += (R[S[i]] != R[S[i - 1]] || S[i] + k >= N ||
                         S[i - 1] + k >= N || R[S[i] + k] != R[S[i - 1] + k]);
      }
      swap(RA, R);
      if (R[S[N - 1]] == N - 1)
        break;
    }
    for (Index i = 0; i < N; ++i) {
      I[S[i]] = i;
    }
    if (PrecomputeLCP) {
      Index k = 0;
      for (Index i = 0; i < N; ++i) {
        if (I[i] == N - 1) {
          LCP[I[i]] = k = 0;
          continue;
        }
        while (i + k < N && S[I[i] + 1] + k < N &&
               t[i + k] == t[S[I[i] + 1] + k]) {
          ++k;
        }
        LCP[I[i]] = k;
        if (k > 0) {
          --k;
        }
      }
    }
    if (PrecomputeRMQ) {
      RMQ.push_back(LCP);
      for (int p = 0; (1 << p) < N; ++p) {
        RMQ.push_back(RMQ[p]);
        for (int i = 0; i < N - (1 << p); ++i) {
          RMQ[p + 1][i] = min(RMQ[p + 1][i], RMQ[p][i + (1 << p)]);
        }
      }
    }
  }

  Index next(Index i) { return (I[i] == N - 1) ? -1 : S[I[i] + 1]; }

  template <typename = std::enable_if<PrecomputeLCP>> Index lcp(Index i) const {
    return LCP[I[i]];
  }

  template <typename = std::enable_if<PrecomputeRMQ>>
  Index lcp(Index i, Index j) const {
    i = I[i];
    j = I[j];
    if (i > j) {
      swap(i, j);
    }
    if (i == j - 1)
      return LCP[i];
    Index p = 0;
    while ((1 << p) < j - i) {
      ++p;
    }
    --p;
    return min(RMQ[p][i], RMQ[p][j - (1 << p)]);
  }

  inline void counting_sort(const vector<int> &R, vector<int> &C,
                            vector<int> &SA, int k, int r) {
    Index i;
    fill(C.begin(), C.begin() + r, 0);
    for (i = k; i < N; i++)
      C[R[i]]++;
    C[0] += k;
    for (Index sum = i = 0; i < r; i++) {
      C[i] = (sum += C[i], sum - C[i]);
    }
    if (k)
      for (i = 0; i < N; i++) {
        SA[C[S[i] + k < N ? R[S[i] + k] : 0]++] = S[i];
      }
    else
      for (i = 0; i < N; i++) {
        SA[C[R[S[i]]]++] = S[i];
      }
    swap(SA, S);
  }

  Index N;
  vector<Index> S, I, LCP;
  vector<vector<Index>> RMQ;
};
#ifndef MAJK_RANDOM_H
#define MAJK_RANDOM_H

#include <chrono>
#include <random>
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
std::mt19937 rng(seed);
using random_int = std::uniform_int_distribution<int>;
using random_ll = std::uniform_int_distribution<ll>;
struct TimeLimit {
  TimeLimit(double seconds) : s(seconds - 0.2), last(clock()), mx(0) {}
  operator bool() const {
    clock_t cur = clock();
    mx = max(mx, cur - last);
    last = cur;
    return clock() < s * CLOCKS_PER_SEC - 1.5 * mx;
  }
  double s;
  mutable clock_t last, mx;
};

#endif // MAJK_RANDOM_H

class EReversingAndConcatenating {
public:
  void solve(istream &cin, ostream &cout) {
    int N;
    cin >> N;
    int K;
    cin >> K;
    string S;
    cin >> S;
    string Ans(N, 'z');
    char smallest = *min_element(S.begin(), S.end());
    int longest = 0;
    vector<int> I(N + 1);
    for (int i = 0; i <= N; ++i)
      I[i] = i;
    shuffle(I.begin(), I.end(), rng);
    int suffixes = 0;
    for (int i : I) {
      string A = S;
      string T = A;
      reverse(T.begin(), T.end());
      A += T;
      A = A.substr(i, N);
      if (A[0] != smallest)
        continue;
      Ans = min(Ans, A);

      if (K >= 2) {
        string T = A;
        reverse(T.begin(), T.end());
        A = T + A;
        int cur = 0, best = 0;
        for (int a : A) {
          if (a == smallest) {
            cur++;
            best = max(best, cur);
          } else {
            cur = 0;
          }
        }
        best = min(best, N);
        if (best < longest || longest == N)
          continue;
        longest = max(longest, best);

        suffixes++;
        if (suffixes > 1300)
          continue;

        A += '{';
        SuffixArray<int> SA(A);
        int k = SA.S[0];
        A = A.substr(SA.S[0], N);

        int w = 0;
        while (w < N && A[0] == A[w])
          ++w;
        int z = w;
        for (int g = 2; g < K; ++g) {
          z *= 2;
          if (z > N) {
            z = N;
            break;
          }
        }

        string Q = string(z, A[0]) + A.substr(w);
        Ans = min(Ans, Q.substr(0, N));
        if (z == N)
          break;
      }
      //            for (int j = 1; j < K; ++j) {
      //                string T = A;
      //                reverse(T.begin(),T.end());
      //                A = T + A;
      //                A += '{';
      //
      //                SuffixArray<int> SA(A);
      //                int k = SA.S[0];
      //                A = A.substr(SA.S[0], N);
      //                if (count(A.begin(),A.end(),A[0]) == N) {
      //                    break;
      //                }
      //            }
      //            Ans = min(Ans, A);
    }

    cout << Ans << endl;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  EReversingAndConcatenating solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Fixed submission:
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author majk
 */

#ifndef MAJK_LIB
#define MAJK_LIB

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define x first
#define y second
typedef std::pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<ui, ui> puu;

template <typename T, typename U>
std::istream &operator>>(std::istream &i, pair<T, U> &p) {
  i >> p.x >> p.y;
  return i;
}
template <typename T> std::istream &operator>>(std::istream &i, vector<T> &t) {
  for (auto &v : t) {
    i >> v;
  }
  return i;
}
template <typename T, typename U>
std::ostream &operator<<(std::ostream &o, const pair<T, U> &p) {
  o << p.x << ' ' << p.y;
  return o;
}
template <typename T>
std::ostream &operator<<(std::ostream &o, const vector<T> &t) {
  if (t.empty())
    o << '\n';
  for (size_t i = 0; i < t.size(); ++i) {
    o << t[i] << " \n"[i == t.size() - 1];
  }
  return o;
}
template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using maxheap = priority_queue<T, vector<T>, less<T>>;
ui logceil(ll x) { return x ? 8 * sizeof(ll) - __builtin_clzll(x) : 0; }

namespace std {
template <typename T, typename U> struct hash<pair<T, U>> {
  hash<T> t;
  hash<U> u;
  size_t operator()(const pair<T, U> &p) const {
    return t(p.x) ^ (u(p.y) << 7);
  }
};
} // namespace std
template <typename T, typename F> T bsh(T l, T h, const F &f) {
  T r = -1, m;
  while (l <= h) {
    m = (l + h) / 2;
    if (f(m)) {
      l = m + 1;
      r = m;
    } else {
      h = m - 1;
    }
  }
  return r;
}
template <typename F>
double bshd(double l, double h, const F &f, double p = 1e-9) {
  ui r = 3 + (ui)log2((h - l) / p);
  while (r--) {
    double m = (l + h) / 2;
    if (f(m)) {
      l = m;
    } else {
      h = m;
    }
  }
  return (l + h) / 2;
}
template <typename T, typename F> T bsl(T l, T h, const F &f) {
  T r = -1, m;
  while (l <= h) {
    m = (l + h) / 2;
    if (f(m)) {
      h = m - 1;
      r = m;
    } else {
      l = m + 1;
    }
  }
  return r;
}
template <typename F>
double bsld(double l, double h, const F &f, double p = 1e-9) {
  ui r = 3 + (ui)log2((h - l) / p);
  while (r--) {
    double m = (l + h) / 2;
    if (f(m)) {
      h = m;
    } else {
      l = m;
    }
  }
  return (l + h) / 2;
}
template <typename T> T gcd(T a, T b) {
  if (a < b)
    swap(a, b);
  return b ? gcd(b, a % b) : a;
}

template <typename T> class vector2 : public vector<vector<T>> {
public:
  vector2() {}
  vector2(size_t a, size_t b, T t = T())
      : vector<vector<T>>(a, vector<T>(b, t)) {}
};
template <typename T> class vector3 : public vector<vector2<T>> {
public:
  vector3() {}
  vector3(size_t a, size_t b, size_t c, T t = T())
      : vector<vector2<T>>(a, vector2<T>(b, c, t)) {}
};
template <typename T> class vector4 : public vector<vector3<T>> {
public:
  vector4() {}
  vector4(size_t a, size_t b, size_t c, size_t d, T t = T())
      : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {}
};
template <typename T> class vector5 : public vector<vector4<T>> {
public:
  vector5() {}
  vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T())
      : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {}
};

#endif

#include <type_traits>

// ordinary suffix array with optional LCP and LCP RMQ. look elsewhere
template <typename Index, bool PrecomputeLCP = false,
          bool PrecomputeRMQ = false>
class SuffixArray {
public:
  static_assert(PrecomputeLCP || !PrecomputeRMQ, "Must have RMQ for LCP");

  template <typename T>
  explicit SuffixArray(T t)
      : N(t.size()), S(N), I(N), LCP(PrecomputeLCP ? N : 0) {
    typedef typename std::remove_reference<decltype(t[0])>::type Item;
    vector<pair<Item, Index>> TR(N);
    for (Index i = 0; i < N; ++i) {
      TR[i] = {t[i], i};
    }
    sort(TR.begin(), TR.end());
    vector<Index> R(N);
    Index r = R[TR[0].y] = S[TR[0].y] = 0;
    for (Index i = 1; i < N; ++i) {
      R[TR[i].y] = r += (TR[i - 1].x != TR[i].x);
      S[TR[i].y] = i;
    }
    vector<Index> RA(N), SA(N), C(N);
    for (Index k = 1; k < N; k <<= 1) {
      counting_sort(R, C, SA, k, r + 1);
      counting_sort(R, C, SA, 0, r + 1);
      RA[S[0]] = r = 0;
      for (Index i = 1; i < N; ++i) {
        RA[S[i]] = r += (R[S[i]] != R[S[i - 1]] || S[i] + k >= N ||
                         S[i - 1] + k >= N || R[S[i] + k] != R[S[i - 1] + k]);
      }
      swap(RA, R);
      if (R[S[N - 1]] == N - 1)
        break;
    }
    for (Index i = 0; i < N; ++i) {
      I[S[i]] = i;
    }
    if (PrecomputeLCP) {
      Index k = 0;
      for (Index i = 0; i < N; ++i) {
        if (I[i] == N - 1) {
          LCP[I[i]] = k = 0;
          continue;
        }
        while (i + k < N && S[I[i] + 1] + k < N &&
               t[i + k] == t[S[I[i] + 1] + k]) {
          ++k;
        }
        LCP[I[i]] = k;
        if (k > 0) {
          --k;
        }
      }
    }
    if (PrecomputeRMQ) {
      RMQ.push_back(LCP);
      for (int p = 0; (1 << p) < N; ++p) {
        RMQ.push_back(RMQ[p]);
        for (int i = 0; i < N - (1 << p); ++i) {
          RMQ[p + 1][i] = min(RMQ[p + 1][i], RMQ[p][i + (1 << p)]);
        }
      }
    }
  }

  Index next(Index i) { return (I[i] == N - 1) ? -1 : S[I[i] + 1]; }

  template <typename = std::enable_if<PrecomputeLCP>> Index lcp(Index i) const {
    return LCP[I[i]];
  }

  template <typename = std::enable_if<PrecomputeRMQ>>
  Index lcp(Index i, Index j) const {
    i = I[i];
    j = I[j];
    if (i > j) {
      swap(i, j);
    }
    if (i == j - 1)
      return LCP[i];
    Index p = 0;
    while ((1 << p) < j - i) {
      ++p;
    }
    --p;
    return min(RMQ[p][i], RMQ[p][j - (1 << p)]);
  }

  inline void counting_sort(const vector<int> &R, vector<int> &C,
                            vector<int> &SA, int k, int r) {
    Index i;
    fill(C.begin(), C.begin() + r, 0);
    for (i = k; i < N; i++)
      C[R[i]]++;
    C[0] += k;
    for (Index sum = i = 0; i < r; i++) {
      C[i] = (sum += C[i], sum - C[i]);
    }
    if (k)
      for (i = 0; i < N; i++) {
        SA[C[S[i] + k < N ? R[S[i] + k] : 0]++] = S[i];
      }
    else
      for (i = 0; i < N; i++) {
        SA[C[R[S[i]]]++] = S[i];
      }
    swap(SA, S);
  }

  Index N;
  vector<Index> S, I, LCP;
  vector<vector<Index>> RMQ;
};
#ifndef MAJK_RANDOM_H
#define MAJK_RANDOM_H

#include <chrono>
#include <random>
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
std::mt19937 rng(seed);
using random_int = std::uniform_int_distribution<int>;
using random_ll = std::uniform_int_distribution<ll>;
struct TimeLimit {
  TimeLimit(double seconds) : s(seconds - 0.2), last(clock()), mx(0) {}
  operator bool() const {
    clock_t cur = clock();
    mx = max(mx, cur - last);
    last = cur;
    return clock() < s * CLOCKS_PER_SEC - 1.5 * mx;
  }
  double s;
  mutable clock_t last, mx;
};

#endif // MAJK_RANDOM_H

class EReversingAndConcatenating {
public:
  void solve(istream &cin, ostream &cout) {
    int N;
    cin >> N;
    int K;
    cin >> K;
    string S;
    cin >> S;
    string Ans(N, 'z');
    char smallest = *min_element(S.begin(), S.end());
    int longest = 0;
    vector<int> I(N + 1);
    for (int i = 0; i <= N; ++i)
      I[i] = i;
    shuffle(I.begin(), I.end(), rng);
    int suffixes = 0;
    for (int i : I) {
      string A = S;
      string T = A;
      reverse(T.begin(), T.end());
      A += T;
      A = A.substr(i, N);
      if (A[0] != smallest)
        continue;
      Ans = min(Ans, A);

      if (K >= 2) {
        string T = A;
        reverse(T.begin(), T.end());
        A = T + A;
        int cur = 0, best = 0;
        for (int a : A) {
          if (a == smallest) {
            cur++;
            best = max(best, cur);
          } else {
            cur = 0;
          }
        }
        best = min(best, N);
        if (best < longest || longest == N)
          continue;
        longest = max(longest, best);

        suffixes++;
        if (suffixes > 1200)
          continue;

        A += '{';
        SuffixArray<int> SA(A);
        int k = SA.S[0];
        A = A.substr(SA.S[0], N);

        int w = 0;
        while (w < N && A[0] == A[w])
          ++w;
        int z = w;
        for (int g = 2; g < K; ++g) {
          z *= 2;
          if (z > N) {
            z = N;
            break;
          }
        }

        string Q = string(z, A[0]) + A.substr(w);
        Ans = min(Ans, Q.substr(0, N));
        if (z == N)
          break;
      }
      //            for (int j = 1; j < K; ++j) {
      //                string T = A;
      //                reverse(T.begin(),T.end());
      //                A = T + A;
      //                A += '{';
      //
      //                SuffixArray<int> SA(A);
      //                int k = SA.S[0];
      //                A = A.substr(SA.S[0], N);
      //                if (count(A.begin(),A.end(),A[0]) == N) {
      //                    break;
      //                }
      //            }
      //            Ans = min(Ans, A);
    }

    cout << Ans << endl;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  EReversingAndConcatenating solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}