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

#include <algorithm>
#include <boost/multiprecision/cpp_int.hpp>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, m, n) for (int i = m; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
#define RREP(i, m, n) for (int i = n; i >= m; --i)
#define llrep(i, n) for (ll i = 0; i < n; ++i)
#define llREP(i, m, n) for (ll i = m; i < n; ++i)
#define llrrep(i, n) for (ll i = n; i >= 0; --i)
#define llRREP(i, m, n) for (ll i = n; i >= m; --i)

using ll = long long;
using ull = unsigned long long;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
using pil = std::pair<int, long long>;
using pli = std::pair<long long, int>;
using plpii = std::pair<long long, std::pair<int, int>>;
using pdd = std::pair<double, double>;
using pis = std::pair<int, std::string>;

const long long INF = 1000000000000000001;
const long long _MOD_ = 1000000000000000009;
const long long _MOD = 1000000007;
const long long MOD = 998244353;
const int ALPHABET = 27;
const double pi = 3.14159265358979;

using namespace std;
namespace mp = boost::multiprecision;

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

  int n;
  cin >> n;
  string *s = new string[n];
  rep(i, n) cin >> s[i];
  sort(s, s + n, [](const string &x, const string &y) {
    return x.length() < y.length();
  });

  ll ans = 0;
  map<mp::int128_t, ll> rh;
  ll alpha[27];
  random_device rnd;
  mp::int128_t t = rnd();
  if (t < 125)
    t += 125;
  rep(i, n) {
    memset(alpha, 0, sizeof(ll) * 27);
    mp::int128_t hash = 0;

    rep(j, s[i].length())++ alpha[s[i][j] - 'a' + 1];
    rep(k, 26) {
      mp::int128_t _hash = 0;
      if (alpha[k + 1]) {
        _hash = t * hash;
        _hash %= _MOD_;
        _hash += k + 1;
        _hash %= _MOD_;

        if (rh.find(_hash) != rh.end())
          ans += rh[_hash];
      }
    }
    rrep(j, s[i].length() - 1) {
      --alpha[s[i][j] - 'a' + 1];

      hash *= t;
      hash %= _MOD_;
      hash += s[i][j] - 'a' + 1;
      hash %= _MOD_;

      rep(k, 26) {
        mp::int128_t _hash = 0;
        if (alpha[k + 1]) {
          _hash = t * hash;
          _hash %= _MOD_;
          _hash += k + 1;
          _hash %= _MOD_;

          if (rh.find(_hash) != rh.end())
            ans += rh[_hash];
        }
      }
    }

    rh[hash] += 1;
  }

  cout << ans << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <boost/multiprecision/cpp_int.hpp>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, m, n) for (int i = m; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
#define RREP(i, m, n) for (int i = n; i >= m; --i)
#define llrep(i, n) for (ll i = 0; i < n; ++i)
#define llREP(i, m, n) for (ll i = m; i < n; ++i)
#define llrrep(i, n) for (ll i = n; i >= 0; --i)
#define llRREP(i, m, n) for (ll i = n; i >= m; --i)

using ll = long long;
using ull = unsigned long long;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
using pil = std::pair<int, long long>;
using pli = std::pair<long long, int>;
using plpii = std::pair<long long, std::pair<int, int>>;
using pdd = std::pair<double, double>;
using pis = std::pair<int, std::string>;

const long long INF = 1000000000000000001;
const long long _MOD_ = 1000000000000000009;
const long long _MOD = 1000000007;
const long long MOD = 998244353;
const int ALPHABET = 27;
const double pi = 3.14159265358979;

using namespace std;
namespace mp = boost::multiprecision;

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

  int n;
  cin >> n;
  string *s = new string[n];
  rep(i, n) cin >> s[i];
  sort(s, s + n, [](const string &x, const string &y) {
    return x.length() < y.length();
  });

  ll ans = 0;
  map<mp::int128_t, ll> lh;
  ll alpha[27];
  random_device rnd;
  mp::int128_t t = rnd();
  if (t < 125)
    t += 125;
  rep(i, n) {
    memset(alpha, 0, sizeof(ll) * 27);
    mp::int128_t hash = 0;

    rep(j, s[i].length())++ alpha[s[i][j] - 'a' + 1];
    rep(k, 26) {
      mp::int128_t _hash = 0;
      if (alpha[k + 1]) {
        _hash = t * hash;
        _hash %= _MOD_;
        _hash += k + 1;
        _hash %= _MOD_;

        ans += lh[_hash];
      }
    }
    rrep(j, s[i].length() - 1) {
      --alpha[s[i][j] - 'a' + 1];

      hash *= t;
      hash %= _MOD_;
      hash += s[i][j] - 'a' + 1;
      hash %= _MOD_;

      rep(k, 26) {
        mp::int128_t _hash = 0;
        if (alpha[k + 1]) {
          _hash = t * hash;
          _hash %= _MOD_;
          _hash += k + 1;
          _hash %= _MOD_;

          ans += lh[_hash];
        }
      }
    }

    lh[hash] += 1;
  }

  cout << ans << "\\n";

  return 0;
}