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

// #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

// vector string deque break continue
#define forn(i, s, f) for (int i = (int)s; i < (int)f; i++)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fs first
#define sc second
#define pf push_front
#define pb push_back
#define pop_f pop_front
#define pop_b pop_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

#ifdef DEBUG
#else
#define cerr                                                                   \\
  if (false)                                                                   \\
  cerr
#endif

template <typename T> istream &operator>>(istream &in, vector<T> &a) {
  for (auto &i : a)
    in >> i;
  return in;
}
template <typename T> ostream &operator<<(ostream &out, vector<T> &a) {
  for (auto &i : a)
    out << i << " ";
  return out;
}
template <typename T, typename U> void chkmin(T &a, U b) {
  if (a > b)
    a = b;
}
template <typename T, typename U> void chkmax(T &a, U b) {
  if (a < b)
    a = b;
}

const int MAXN = 1e6 + 5;
const ull P = 41;

vector<ull> p;

void init_p() {
  p.resize(MAXN);
  p[0] = 1;
  forn(i, 1, sz(p)) p[i] = p[i - 1] * P;
}

ull char_to_int(char c) { return (c - 'a' + 1); }

vector<ull> init_h(const string &s) {
  vector<ull> h(sz(s) + 1);
  forn(i, 1, sz(h)) h[i] = h[i - 1] * P + char_to_int(s[i - 1]);
  return h;
}

ull get(const vector<ull> &h, int l, int r) { return h[r] - h[l] * p[r - l]; }

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  init_p();
  int n;
  cin >> n;
  vector<vector<string>> by_sz(MAXN);
  forn(i, 0, n) {
    string s;
    cin >> s;
    by_sz[sz(s)].pb(s);
  }
  long long ans = 0;
  map<ull, array<int, 26>> cnt;
  for (int sz = MAXN - 1; sz > 0; --sz) {
    for (auto &s : by_sz[sz]) {
      auto h = init_h(s);
      ull t = get(h, 1, sz(s));
      ans += cnt[get(h, 1, sz(s))][s[0] - 'a'];
    }
    for (auto &s : by_sz[sz]) {
      auto h = init_h(s);
      vector<int> cur(26);
      for (char c : s)
        cur[c - 'a']++;
      for (int i = sz(s) - 1; i >= 0; --i) {
        auto &mp = cnt[get(h, i + 1, sz(s))];
        forn(j, 0, 26) if (cur[j]) mp[j]++;
        cur[s[i] - 'a']--;
      }
    }
    // cerr << sz << " ";
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

// #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

// vector string deque break continue
#define forn(i, s, f) for (int i = (int)s; i < (int)f; i++)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fs first
#define sc second
#define pf push_front
#define pb push_back
#define pop_f pop_front
#define pop_b pop_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

#ifdef DEBUG
#else
#define cerr                                                                   \\
  if (false)                                                                   \\
  cerr
#endif

template <typename T> istream &operator>>(istream &in, vector<T> &a) {
  for (auto &i : a)
    in >> i;
  return in;
}
template <typename T> ostream &operator<<(ostream &out, vector<T> &a) {
  for (auto &i : a)
    out << i << " ";
  return out;
}
template <typename T, typename U> void chkmin(T &a, U b) {
  if (a > b)
    a = b;
}
template <typename T, typename U> void chkmax(T &a, U b) {
  if (a < b)
    a = b;
}

const int MAXN = 1e6 + 5, P = 41;

vector<ull> p;

void init_p() {
  p.resize(MAXN);
  p[0] = 1;
  forn(i, 1, sz(p)) p[i] = (1LL * p[i - 1] * P);
}

ull char_to_int(char c) { return (c - 'a' + 1); }

vector<ull> init_h(const string &s) {
  vector<ull> h(sz(s) + 1);
  forn(i, 1, sz(h)) h[i] = h[i - 1] * P + char_to_int(s[i - 1]);
  return h;
}

ull get(const vector<ull> &h, int l, int r) { return h[r] - h[l] * p[r - l]; }

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  init_p();
  int n;
  cin >> n;
  vector<vector<string>> by_sz(MAXN);
  forn(i, 0, n) {
    string s;
    cin >> s;
    by_sz[sz(s)].pb(s);
  }
  long long ans = 0;
  map<ull, map<int, int>> cnt;
  for (int sz = MAXN - 1; sz > 0; --sz) {
    for (auto &s : by_sz[sz]) {
      auto h = init_h(s);
      ans += cnt[get(h, 1, sz(s))][s[0] - 'a'];
    }
    for (auto &s : by_sz[sz]) {
      auto h = init_h(s);
      vector<int> cur(26);
      for (char c : s)
        cur[c - 'a']++;
      for (int i = sz(s) - 1; i >= 0; --i) {
        auto &mp = cnt[get(h, i + 1, sz(s))];
        forn(j, 0, 26) if (cur[j]) mp[j]++;
        cur[s[i] - 'a']--;
      }
    }
    // cerr << sz << " ";
  }
  cout << ans;
  return 0;
}