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

#pragma GCC optimize "-O3"
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
#define pb push_back
#define pp pop_back
#define ubo upper_bound
#define lbo lower_bound
#define VI vector<int>
#define VL vector<ll>
#define VP vector<pii>
#define all(x) x.begin(), x.end()
#define M vector<VI>

const int P = 1e9 + 7;

using namespace std;

int rseed() {
  int seed = chrono::duration_cast<chrono::nanoseconds>(
                 chrono::high_resolution_clock::now().time_since_epoch())
                 .count();
  srand(seed);
  return seed;
}

const int SEED = rseed();
const int A = rand();
const int B = rand();

map<ll, VI> mp;

int main() {

  cin.tie(0);
  ios_base::sync_with_stdio(0);

  int n;
  cin >> n;
  vector<string> s(n);

  for (string &i : s) {
    cin >> i;
    ll hsha = 0, hshb = 0;
    int j = i.size() - 1;
    for (; j > 0; j--) {
      hsha = (A * hsha + i[j]) % P;
      hshb = (B * hshb + i[j]) % P;
    }

    ll thsh = hsha + (hshb << 32);
    if (mp[thsh].size() == 0)
      mp[thsh].resize(26, 0);

    mp[thsh][i[0] - 97]++;
  }

  ll ans = 0;
  for (string i : s) {

    int sz = i.size();
    VI c(26, 0);
    for (int j = 0; j < sz; j++)
      c[i[j] - 97]++;

    ll hsha = 0, hshb = 0;
    for (int j = sz - 1; j > 0; j--) {

      ll thsh = hsha + (hshb << 32);

      VI x = mp[thsh];
      if (x.size()) {
        for (int i = 0; i < 26; i++) {
          if (c[i])
            ans += x[i];
        }
      }

      c[i[j] - 97]--;
      hsha = (A * hsha + i[j]) % P;
      hshb = (B * hshb + i[j]) % P;
    }
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize "-O3"
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
#define pb push_back
#define pp pop_back
#define ubo upper_bound
#define lbo lower_bound
#define VI vector<int>
#define VL vector<ll>
#define VP vector<pii>
#define all(x) x.begin(), x.end()
#define M vector<VI>

const int P = 1e9 + 7;

using namespace std;

int rseed() {
  int seed = chrono::duration_cast<chrono::nanoseconds>(
                 chrono::high_resolution_clock::now().time_since_epoch())
                 .count();
  srand(seed);
  return seed;
}

const int SEED = rseed();
const int A = rand();
const int B = rand();

map<pair<char, ll>, int> mp;

int main() {

  cin.tie(0);
  ios_base::sync_with_stdio(0);

  int n;
  cin >> n;
  vector<string> s(n);

  for (string &i : s) {
    cin >> i;
    ll hsha = 0, hshb = 0;
    int j = i.size() - 1;
    for (; j > 0; j--) {
      hsha = (A * hsha + i[j]) % P;
      hshb = (B * hshb + i[j]) % P;
    }

    ll thsh = hsha + (hshb << 32);
    pair<char, ll> p = {i[0], thsh};
    mp[p]++;
  }

  ll ans = 0;
  for (string i : s) {

    int sz = i.size();
    VI c(26, 0);
    for (int j = 0; j < sz; j++)
      c[i[j] - 97]++;

    ll hsha = 0, hshb = 0;
    for (int j = sz - 1; j > 0; j--) {

      ll thsh = hsha + (hshb << 32);

      for (int k = 0; k < 26; k++) {
        if (c[k]) {
          char cr = k + 97;
          pair<char, ll> tmp = {cr, thsh};
          ans += mp[tmp];
        }
      }

      c[i[j] - 97]--;
      hsha = (A * hsha + i[j]) % P;
      hshb = (B * hshb + i[j]) % P;
    }
  }

  cout << ans << endl;

  return 0;
}