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

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

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

void pre() {}

void solve() {}
vector<pair<int, string>> v;
typedef unsigned long long H;
static const H C = 123891739; // arbitrary

// Arithmetic mod 2^64-1. 5x slower than mod 2^64 and more
// code, but works on evil test data (e.g. Thue-Morse).
// "typedef H K;" instead if you think test data is random.
struct K {
  typedef __uint128_t H2;
  H x;
  K(H x = 0) : x(x) {}
  K operator+(K o) { return x + o.x + H(((H2)x + o.x) >> 64); }
  K operator*(K o) { return K(x * o.x) + H(((H2)x * o.x) >> 64); }
  H operator-(K o) {
    K a = *this + ~o.x;
    return a.x + !~a.x;
  }
};
vector<K> ha, pw;
int cnt[26];
vector<K> hsh(string &str) {
  fill(cnt);
  pw.resize(sz(str) + 1);
  ha.resize(sz(pw));
  pw[0] = 1;
  rep(i, sz(str)) {
    cnt[str[i] - 'a']++;
    ha[i + 1] = ha[i] * C + str[i], pw[i + 1] = pw[i] * C;
  }
  return ha;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  pre();
  int n;
  cin >> n;
  rep(i, n) {
    string s;
    cin >> s;
    v.pb(mp(sz(s), s));
  }
  sort(all(v));
  map<pair<H, int>, int> m;
  ll ans = 0;
  trav(i, v) {
    reverse(all(i.snd));
    vector<K> h = hsh(i.snd);
    int ix = 0;
    trav(j, h) {
      auto it = m.lower_bound(mp(j.x, -1));
      rep(k, 26) if (cnt[k]) {
        while (it != m.end() && it->fst.snd < k)
          it++;
        if ((*it).fst == mp(j.x, k))
          ans += it->snd;
      }
      if (ix < sz(i.snd))
        cnt[i.snd[ix] - 'a']--;
      ix++;
    }
    m[mp(h[sz(i.snd) - 1].x, i.snd.back() - 'a')]++;
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

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

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

void pre() {}

void solve() {}
vector<pair<int, string>> v;
typedef unsigned long long H;
static const H C = 123891739; // arbitrary

// Arithmetic mod 2^64-1. 5x slower than mod 2^64 and more
// code, but works on evil test data (e.g. Thue-Morse).
// "typedef H K;" instead if you think test data is random.
struct K {
  typedef __uint128_t H2;
  H x;
  K(H x = 0) : x(x) {}
  K operator+(K o) { return x + o.x + H(((H2)x + o.x) >> 64); }
  K operator*(K o) { return K(x * o.x) + H(((H2)x * o.x) >> 64); }
  H operator-(K o) {
    K a = *this + ~o.x;
    return a.x + !~a.x;
  }
};
vector<K> ha, pw;
int cnt[26];
vector<K> hsh(string &str) {
  fill(cnt);
  pw.resize(sz(str) + 1);
  ha.resize(sz(pw));
  pw[0] = 1;
  rep(i, sz(str)) {
    cnt[str[i] - 'a']++;
    ha[i + 1] = ha[i] * C + str[i], pw[i + 1] = pw[i] * C;
  }
  return ha;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  pre();
  int n;
  cin >> n;
  rep(i, n) {
    string s;
    cin >> s;
    v.pb(mp(sz(s), s));
  }
  sort(all(v));
  map<pair<H, int>, int> m;
  ll ans = 0;
  trav(i, v) {
    reverse(all(i.snd));
    vector<K> h = hsh(i.snd);
    int ix = 0;
    trav(j, h) {
      rep(k, 26) if (cnt[k]) ans += m[mp(j.x, k)];
      if (ix < sz(i.snd))
        cnt[i.snd[ix] - 'a']--;
      ix++;
    }
    m[mp(h[sz(i.snd) - 1].x, i.snd.back() - 'a')]++;
  }
  cout << ans;
  return 0;
}