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

#include <bits/stdc++.h>

using namespace std;

const int _base = 1e9 + 7;

template <unsigned mod> struct RollingHash {
  vector<unsigned> hashed, power;

  inline unsigned mul(unsigned a, unsigned b) const {
    unsigned long long x = (unsigned long long)a * b;
    unsigned xh = (unsigned)(x >> 32), xl = (unsigned)x, d, m;
    asm("divl %4; \\n\\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod));
    return m;
  }

  RollingHash(const string &s, unsigned base = 10007) {
    int sz = (int)s.size();
    hashed.assign(sz + 1, 0);
    power.assign(sz + 1, 0);
    power[0] = 1;
    for (int i = 0; i < sz; i++) {
      power[i + 1] = mul(power[i], base);
      hashed[i + 1] = mul(hashed[i], base) + s[i];
      if (hashed[i + 1] >= mod)
        hashed[i + 1] -= mod;
    }
  }

  unsigned get(int l, int r) const {
    unsigned ret = hashed[r] + mod - mul(hashed[l], power[r - l]);
    if (ret >= mod)
      ret -= mod;
    return ret;
  }

  unsigned connect(unsigned h1, int h2, int h2len) const {
    unsigned ret = mul(h1, power[h2len]) + h2;
    if (ret >= mod)
      ret -= mod;
    return ret;
  }

  int LCP(const RollingHash<mod> &b, int l1, int r1, int l2, int r2) {
    int len = min(r1 - l1, r2 - l2);
    int low = -1, high = len + 1;
    while (high - low > 1) {
      int mid = (low + high) / 2;
      if (get(l1, l1 + mid) == b.get(l2, l2 + mid))
        low = mid;
      else
        high = mid;
    }
    return (low);
  }
};

using RH = RollingHash<1000000007>;

int main() {
  string T, P;

  cin >> T;
  cin >> P;
  RH rh(T), rh2(P);
  for (int i = 0; i + P.size() <= T.size(); i++) {
    if (rh.get(i, i + P.size()) == rh2.get(0, P.size())) {
      cout << i << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct SuffixArray {
  vector<int> SA;
  string s;

  void Build_SA(const string &str) {
    s = str;
    SA.resize(s.size());
    iota(begin(SA), end(SA), 0);
    sort(begin(SA), end(SA), [&](const int &a, const int &b) {
      if (s[a] == s[b])
        return (a > b);
      return (s[a] < s[b]);
    });
    vector<int> classes(s.size()), c(s.size()), cnt(s.size());
    for (int i = 0; i < s.size(); i++) {
      c[i] = s[i];
    }
    for (int len = 1; len < s.size(); len <<= 1) {
      for (int i = 0; i < s.size(); i++) {
        if (i > 0 && c[SA[i - 1]] == c[SA[i]] && SA[i - 1] + len < s.size() &&
            c[SA[i - 1] + len / 2] == c[SA[i] + len / 2]) {
          classes[SA[i]] = classes[SA[i - 1]];
        } else {
          classes[SA[i]] = i;
        }
      }
      iota(begin(cnt), end(cnt), 0);
      copy(begin(SA), end(SA), begin(c));
      for (int i = 0; i < s.size(); i++) {
        int s1 = c[i] - len;
        if (s1 >= 0)
          SA[cnt[classes[s1]]++] = s1;
      }
      classes.swap(c);
    }
  }

  int operator[](int k) const { return (SA[k]); }

  int size() const { return (s.size()); }

  bool lt_substr(string &t, int si = 0, int ti = 0) {
    int sn = s.size(), tn = t.size();
    while (si < sn && ti < tn) {
      if (s[si] < t[ti])
        return (true);
      if (s[si] > t[ti])
        return (false);
      ++si, ++ti;
    }
    return (si >= sn && ti < tn);
  }

  int lower_bound(string &t) {
    int low = -1, high = SA.size();
    while (high - low > 1) {
      int mid = (low + high) >> 1;
      if (lt_substr(t, SA[mid]))
        low = mid;
      else
        high = mid;
    }
    return (high);
  }

  pair<int, int> lower_upper_bound(string t) {
    int idx = lower_bound(t);
    int low = idx - 1, high = SA.size();
    t.back()++;
    while (high - low > 1) {
      int mid = (low + high) >> 1;
      if (lt_substr(t, SA[mid]))
        low = mid;
      else
        high = mid;
    }
    t.back()--;
    return (make_pair(idx, high));
  }

  void output() {
    for (int i = 0; i < size(); i++) {
      cout << i << ": " << s.substr(SA[i]) << endl;
    }
  }
};

int main() {
  char a[1000001], b[100001];
  scanf("%s", a);
  scanf("%s", b);

  SuffixArray sa;
  sa.Build_SA(string(a));

  auto get = sa.lower_upper_bound(string(b));
  bool v[1000000] = {};
  for (int i = get.first; i < get.second; i++) {
    v[sa[i]] = true;
  }
  for (int i = 0; i < sa.size(); i++) {
    if (v[i])
      printf("%d\\n", i);
  }
}