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

#include <bits/stdc++.h>

using namespace std;

#ifndef aa
#define trace(...)
#define endl '\\n'
#endif
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double ld;
#define double long double
#define pii pair<int, int>
#define pdd pair<double, double>
#define pll pair<ll, ll>
#define sz(x) ((long long)x.size())
#define fr(a, b, c) for (int a = b; a <= c; a++)
#define frev(a, b, c) for (int a = c; a >= b; a--)
#define rep(a, b, c) for (int a = b; a < c; a++)
#define trav(a, x) for (auto &a : x)
#define all(con) con.begin(), con.end()
#define deb(x) cout << x << endl
#define done(x)                                                                \\
  {                                                                            \\
    cout << x << endl;                                                         \\
    return;                                                                    \\
  }
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
const int mod1 = 998244353;
const int mod2 = 1e9 + 7;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef map<int, int> mii;
typedef set<int> si;
typedef set<pair<int, int>> spii;
typedef queue<int> qi;

const int N = 1e6 + 5;

spii heap;
int p1[N], p2[N];

void pre() {
  p1[0] = p2[0] = 1;
  rep(i, 1, N) {
    p1[i] = (p1[i - 1] * 29) % mod1;
    p2[i] = (p2[i - 1] * 31) % mod2;
  }
}

pii hsh(pii h, int n, int c) {
  int x = (h.fi + (c + 1) * p1[n]) % mod1;
  int y = (h.se + (c + 1) * p2[n]) % mod2;
  return {x, y};
}

void solve() {

  pre();
  int n, ans = 0;
  cin >> n;
  vector<string> str(n);
  rep(i, 0, n) cin >> str[i];
  sort(all(str), [](string &s1, string &s2) { return sz(s1) < sz(s2); });
  rep(i, 0, n) {
    string &s = str[i];
    int m = sz(s);
    vi f(26);
    rep(j, 0, m) f[s[j] - 'a']++;
    pii hs = {0, 0};
    frev(j, 0, m - 1) {
      rep(k, 0, 26) {
        if (!f[k])
          continue;
        pii hs1 = hsh(hs, m - 1 - j, k);
        ans += heap.count(hs1);
      }
      hs = hsh(hs, m - 1 - j, s[j] - 'a');
      f[s[j] - 'a']--;
    }
    heap.insert(hs);
  }
  cout << ans;
}

signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  cout << fixed << setprecision(15);
  int t = 1;
  // cin >> t;

  while (t--)
    solve();
#ifdef aa
  cout << endl
       << endl
       << endl
       << endl
       << "Time elapsed: " << (double)(clock() - clk) / CLOCKS_PER_SEC << endl;
#endif
  return 0;
}

/*


int powm(int a, int b, int mod) {
    int res = 1;
    while (b) {
        if (b & 1)
            res = (res*a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return res;
}

int divide(int a, int b) {
    return (a % mod) * powm(b, mod - 2) % mod;
}

int norm(int a) {
    while (a >= mod) a -= mod;
    while (a < 0) a += mod;
    return a;
}


int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}*/

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#ifndef aa
#define trace(...)
#define endl '\\n'
#endif
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double ld;
#define double long double
#define pii pair<int, int>
#define pdd pair<double, double>
#define pll pair<ll, ll>
#define sz(x) ((long long)x.size())
#define fr(a, b, c) for (int a = b; a <= c; a++)
#define frev(a, b, c) for (int a = c; a >= b; a--)
#define rep(a, b, c) for (int a = b; a < c; a++)
#define trav(a, x) for (auto &a : x)
#define all(con) con.begin(), con.end()
#define deb(x) cout << x << endl
#define done(x)                                                                \\
  {                                                                            \\
    cout << x << endl;                                                         \\
    return;                                                                    \\
  }
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
const int mod1 = 998244353;
const int mod2 = 1e9 + 7;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef map<int, int> mii;
typedef set<int> si;
typedef set<pair<int, int>> spii;
typedef queue<int> qi;

const int N = 5e5 + 5;

spii heap;

int powm(int a, int b, int mod) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

pii hsh(pii h, int n, int c) {
  int x = (h.fi + (c + 1) * powm(31, n, mod1)) % mod1;
  int y = (h.se + (c + 1) * powm(29, n, mod2)) % mod2;
  return {x, y};
}

void solve() {

  int n, ans = 0;
  cin >> n;
  vector<string> str(n);
  rep(i, 0, n) cin >> str[i];
  sort(all(str), [](string &s1, string &s2) { return sz(s1) < sz(s2); });
  rep(i, 0, n) {
    string &s = str[i];
    int m = sz(s);
    vi f(26);
    rep(j, 0, m) f[s[j] - 'a']++;
    pii hs = {0, 0};
    frev(j, 0, m - 1) {
      rep(k, 0, 26) {
        if (!f[k])
          continue;
        pii hs1 = hsh(hs, m - 1 - j, k);
        ans += heap.count(hs1);
      }
      hs = hsh(hs, m - 1 - j, s[j] - 'a');
      f[s[j] - 'a']--;
    }
    heap.insert(hs);
  }
  cout << ans;
}

signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  cout << fixed << setprecision(15);
  int t = 1;
  // cin >> t;

  while (t--)
    solve();
#ifdef aa
  cout << endl
       << endl
       << endl
       << endl
       << "Time elapsed: " << (double)(clock() - clk) / CLOCKS_PER_SEC << endl;
#endif
  return 0;
}

/*int divide(int a, int b) {
    return (a % mod) * powm(b, mod - 2) % mod;
}




int norm(int a) {
    while (a >= mod) a -= mod;
    while (a < 0) a += mod;
    return a;
}


int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}*/