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

#pragma GCC optimize("O3") //
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, b, a) for (int i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef long double ld;
typedef unsigned long long ull;

unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937 eng(seed);

uniform_int_distribution<> DD(0, (1ll << 31));

ll random2() {
  return (ll(DD(eng)) * (1ll << 31) + ll(DD(eng))) & ((1ll << 62ll) - 1);
}

ll n, m, k, q, T;

const ll big = 1000000007;
const ll big2 = 1000000009;
const ll mod = 998244353;

const int MAXN = 200001;

vector<string> A;
vi ind;

bool comp(ll i, ll j) { return sz(A[i]) < sz(A[j]); }

vector<vl> H(MAXN, vl());
unordered_map<ll, ll> M;
const ll base = 12738481;

ll hh(ll c, ll x) { return c + base * x; }

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

  // freopen("input.txt","r",stdin);
  // freopen("autput.txt","w",stdout);

  ll a, b, c, d, e;

  cin >> n;
  rep(c1, 0, n) {
    string s;
    cin >> s;
    A.push_back(s);
    ind.push_back(c1);

    ll h1 = 0;
    ll h2 = 0;
    H[c1].push_back(0);
    m = sz(s);
    for (int c2 = m - 1; c2 >= 0; c2--) {
      h1 *= base;
      h1 += (s[c2] - 'a' + 1);
      h1 %= big;

      h2 *= base;
      h2 += (s[c2] - 'a' + 1);
      h2 %= big2;

      ll tmp = h1 * big2 + h2;
      H[c1].push_back(tmp);
    }
    reverse(all(H[c1]));
  }

  sort(all(ind), comp);
  ll ans = 0;
  trav(i, ind) {
    bool seen[26] = {0};
    m = sz(A[i]);
    rep(c1, 0, m) {
      ll L = A[i][c1] - 'a';
      if (!seen[L]) {
        seen[L] = 1;
        rep(c2, c1 + 1, m + 1) {
          ll tmp = hh(L, H[i][c2]);
          if (M.find(tmp) != M.end()) {
            ans++;
          }
        }
      }
    }
    ll tmp = hh(A[i][0] - 'a', H[i][1]);
    if (M.find(tmp) == M.end()) {
      M[tmp] = 1;
    }
  }
  cout << ans << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O3") //
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, b, a) for (int i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef long double ld;
typedef unsigned long long ull;

unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937 eng(seed);

uniform_int_distribution<> DD(0, (1ll << 31));

ll random2() {
  return (ll(DD(eng)) * (1ll << 31) + ll(DD(eng))) & ((1ll << 62ll) - 1);
}

ll n, m, k, q, T;

const ll big = 1000000007;
const ll big2 = 1000000009;
const ll mod = 998244353;

const int MAXN = 200001;

vector<string> A;
vi ind;

bool comp(ll i, ll j) { return sz(A[i]) < sz(A[j]); }

vector<vl> H(MAXN, vl());
map<ll, ll> M;
const ll base = 12738481;

ll hh(ll c, ll x) { return c + base * x; }

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

  // freopen("input.txt","r",stdin);
  // freopen("autput.txt","w",stdout);

  ll a, b, c, d, e;

  cin >> n;
  rep(c1, 0, n) {
    string s;
    cin >> s;
    A.push_back(s);
    ind.push_back(c1);

    ll h1 = 0;
    ll h2 = 0;
    H[c1].push_back(0);
    m = sz(s);
    for (int c2 = m - 1; c2 >= 0; c2--) {
      h1 *= base;
      h1 += (s[c2] - 'a' + 1);
      h1 %= big;

      h2 *= base;
      h2 += (s[c2] - 'a' + 1);
      h2 %= big2;

      ll tmp = h1 * big2 + h2;
      H[c1].push_back(tmp);
    }
    reverse(all(H[c1]));
  }

  sort(all(ind), comp);
  ll ans = 0;
  trav(i, ind) {
    bool seen[26] = {0};
    m = sz(A[i]);
    rep(c1, 0, m) {
      ll L = A[i][c1] - 'a';
      if (!seen[L]) {
        seen[L] = 1;
        rep(c2, c1 + 1, m + 1) { ans += M[hh(L, H[i][c2])]; }
      }
    }
    M[hh(A[i][0] - 'a', H[i][1])]++;
  }
  cout << ans << "\\n";

  return 0;
}