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

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define pb push_back
#define pi pair<int, int>
#define l first
#define r second
#define all(x) x.begin(), x.end()
#define fori(a, b, step) for (int i = a; i < b; i += step)
#define forj(a, b, step) for (int j = a; j < b; j += step)

const int maxn = 2e5 + 1, mod1 = 1e9 + 7, mod2 = 1e9 + 9, mod3 = 1148822869;
const int k1 = 29, k2 = 31, k3 = 37;

using namespace std;

struct trie {
  int next[26];
  int psb[26];
  int p = -1;

  trie() {
    for (int i = 0; i < 26; i++) {
      next[i] = -1;
      psb[i] = 0;
    }
  }
};

trie t[5 * maxn];
int mx = 0;
string v[maxn];

void add_string(string s) {
  int x = 0;
  for (int i = 0; i < s.size(); i++) {
    if (t[x].next[s[i] - 'a'] == -1) {
      t[x].next[s[i] - 'a'] = ++mx;
      t[mx].p = x;
    }
    x = t[x].next[s[i] - 'a'];
  }
  bool cc[26];
  for (int j = 0; j < 26; j++)
    cc[j] = 0;
  int i = s.size() - 1;
  for (; x != -1; x = t[x].p) {
    for (int j = 0; j < 26; j++)
      t[x].psb[j] += cc[j];
    cc[s[i] - 'a'] = 1;
    --i;
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    string nv = "";
    for (int j = v[i].size() - 1; j >= 0; j--)
      nv += v[i][j];
    v[i] = nv;
    add_string(v[i]);
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int x = 0;
    for (int j = 0; j < v[i].size() - 1; j++)
      x = t[x].next[v[i][j] - 'a'];
    ans += t[x].psb[v[i].back() - 'a'];
  }
  cout << ans - n;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define pb push_back
#define pi pair<int, int>
#define l first
#define r second
#define all(x) x.begin(), x.end()
#define fori(a, b, step) for (int i = a; i < b; i += step)
#define forj(a, b, step) for (int j = a; j < b; j += step)

const int maxn = 2e5 + 1, mod1 = 1e9 + 7, mod2 = 1e9 + 9, mod3 = 1148822869;
const int k1 = 29, k2 = 31, k3 = 37;

using namespace std;

struct hype {
  int h1 = 0, h2 = 0, h3 = 0;

  hype() {}

  hype(int h1, int h2, int h3) : h1(h1), h2(h2), h3(h3) {}

  void add_s(char ch) {
    int nc = ch - 'a' + 1;
    h1 = (h1 * 1ll * k1 + nc) % mod1;
    h2 = (h2 * 1ll * k2 + nc) % mod2;
    h3 = (h3 * 1ll * k3 + nc) % mod3;
  }
};

bool operator<(hype x, hype y) {
  if (x.h1 != y.h1)
    return x.h1 < y.h1;
  if (x.h2 != y.h2)
    return x.h2 < y.h2;
  return x.h3 < y.h3;
}

map<hype, int> mp;
string v[maxn];

hype ghash(string s) {
  hype ch;
  for (int i = s.size() - 1; i >= 0; i--) {
    ch.add_s(s[i]);
  }
  return ch;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    int cc[26];
    for (int j = 0; j < 26; j++)
      cc[j] = 0;
    for (int j = 0; j < v[i].size(); j++)
      ++cc[v[i][j] - 'a'];
    set<hype> cst;
    for (int j = 0; j < 26; j++)
      if (cc[j])
        cst.insert(hype(j + 1, j + 1, j + 1));
    --cc[v[i].back() - 'a'];
    hype ch;
    for (int j = v[i].size() - 1; j >= 0; j--) {
      if (j)
        --cc[v[i][j - 1] - 'a'];
      ch.add_s(v[i][j]);
      cst.insert(ch);
      for (int j = 0; j < 26; j++) {
        if (!cc[j])
          continue;
        hype nh = ch;
        nh.add_s('a' + j);
        cst.insert(nh);
      }
    }
    for (auto gg : cst)
      ++mp[gg];
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += mp[ghash(v[i])] - 1;
  }
  cout << ans;
  return 0;
}