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

#include <bits/stdc++.h>
#define int long long
#define ll long long
using ull = unsigned long long;
using namespace std;
#define dump(x)                                                                \\
  if (dbg) {                                                                   \\
    cerr << #x << " = " << (x) << endl;                                        \\
  }
#define overload4(_1, _2, _3, _4, name, ...) name
#define FOR1(n) for (ll i = 0; i < (n); ++i)
#define FOR2(i, n) for (ll i = 0; i < (n); ++i)
#define FOR3(i, a, b) for (ll i = (a); i < (b); ++i)
#define FOR4(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define bit(n, k) ((n >> k) & 1) /*nのk bit目*/
namespace mydef {
const int INF = 1ll << 60;
const int MOD = 1e9 + 7;
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  } else
    return 0;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  } else
    return 0;
}
void Yes(bool flag = true) {
  if (flag)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
void No(bool flag = true) { Yes(!flag); }
void YES(bool flag = true) {
  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void NO(bool flag = true) { YES(!flag); }
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
bool dbg = false;
} // namespace mydef
using namespace mydef;
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int, int>>
#define vvp vector<vector<pair<int, int>>>
#define pi pair<int, int>
// #define P pair<int, int>
// #define V vector<int>
// #define S set<int>
#define asn ans

struct RollingHash {
private:
  using u64 = uint_fast64_t;
  using size_type = uint_fast32_t;
  using i128 = __int128_t;
  string str;
  vector<u64> hashed, power;
  static constexpr u64 mod = (1uL << 61) - 1;
  static constexpr u64 base = 1e9 + 7;
  // static constexpr u64 base = 1e9 + 9;
  static constexpr u64 mask30 = (1ul << 30) - 1;
  static constexpr u64 mask31 = (1ul << 31) - 1;
  u64 mul(i128 a, i128 b) const {
    i128 t = a * b;
    t = (t >> 61) + (t & mod);
    if (t >= mod)
      return (u64)(t - mod);
    return (u64)t;
  }
  //
public:
  void set(const string &_str) {
    str = _str;
    hashed.resize(str.size() + 1, 0);
    power.resize(str.size() + 1, 1);
    for (size_type i = 0; i < str.size(); i++) {
      hashed[i + 1] = mul(hashed[i], base) + str[i] + 1;
      power[i + 1] = mul(power[i], base);
      if (hashed[i + 1] >= mod)
        hashed[i + 1] -= mod;
    }
  }
  RollingHash() : str(){};
  RollingHash(const string &str) : str() { set(str); };
  u64 hash() const { return hashed.back(); }
  u64 hash(size_type l, size_type r) const {
    u64 ret = mod + hashed[r] - mul(hashed[l], power[r - l]);
    return ret < mod ? ret : ret - mod;
  }
  size_type size() const { return str.size(); }
};

int N;
vector<string> S;

void solve() {
  unordered_map<int, int> mp[30];
  int ans = 0;
  for (int i = 0; i < N; i++) {
    RollingHash Roll(S[i]);
    int mask = 0;
    for (int j = S[i].size() - 1; j >= -1; j--) {
      int hash = Roll.hash(0, j + 1);
      for (int x = 0; x < 26; x++) {
        if ((1 << x) & mask) {
          if (mp[x].count(hash))
            ans += mp[x][hash];
        }
      }
      if (j >= 0)
        mask |= 1 << S[i][j];
    }
    mp[(int)S[i].back()][Roll.hash(0, S[i].size() - 1)]++;
  }
  cout << ans << endl;
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  cin >> N;
  S.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> S[i];
    for (auto &s : S[i]) {
      s -= 'a';
    }
    reverse(all(S[i]));
  }
  sort(all(S), [](string x, string y) { return x.size() < y.size(); });

  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define ll long long
using ull = unsigned long long;
using namespace std;
#define dump(x)                                                                \\
  if (dbg) {                                                                   \\
    cerr << #x << " = " << (x) << endl;                                        \\
  }
#define overload4(_1, _2, _3, _4, name, ...) name
#define FOR1(n) for (ll i = 0; i < (n); ++i)
#define FOR2(i, n) for (ll i = 0; i < (n); ++i)
#define FOR3(i, a, b) for (ll i = (a); i < (b); ++i)
#define FOR4(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define bit(n, k) ((n >> k) & 1) /*nのk bit目*/
namespace mydef {
const int INF = 1ll << 60;
const int MOD = 1e9 + 7;
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  } else
    return 0;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  } else
    return 0;
}
void Yes(bool flag = true) {
  if (flag)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
void No(bool flag = true) { Yes(!flag); }
void YES(bool flag = true) {
  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void NO(bool flag = true) { YES(!flag); }
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
bool dbg = false;
} // namespace mydef
using namespace mydef;
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int, int>>
#define vvp vector<vector<pair<int, int>>>
#define pi pair<int, int>
// #define P pair<int, int>
// #define V vector<int>
// #define S set<int>
#define asn ans

struct RollingHash {
private:
  using u64 = uint_fast64_t;
  using size_type = uint_fast32_t;
  using i128 = __int128_t;
  string str;
  vector<u64> hashed, power;
  static constexpr u64 mod = (1uL << 61) - 1;
  static constexpr u64 base = 1e9 + 7;
  // static constexpr u64 base = 1e9 + 9;
  static constexpr u64 mask30 = (1ul << 30) - 1;
  static constexpr u64 mask31 = (1ul << 31) - 1;
  u64 mul(i128 a, i128 b) const {
    i128 t = a * b;
    t = (t >> 61) + (t & mod);
    if (t >= mod)
      return (u64)(t - mod);
    return (u64)t;
  }
  //
public:
  void set(const string &_str) {
    str = _str;
    hashed.resize(str.size() + 1, 0);
    power.resize(str.size() + 1, 1);
    for (size_type i = 0; i < str.size(); i++) {
      hashed[i + 1] = mul(hashed[i], base) + str[i] + 1;
      power[i + 1] = mul(power[i], base);
      if (hashed[i + 1] >= mod)
        hashed[i + 1] -= mod;
    }
  }
  RollingHash() : str(){};
  RollingHash(const string &str) : str() { set(str); };
  u64 hash() const { return hashed.back(); }
  u64 hash(size_type l, size_type r) const {
    u64 ret = mod + hashed[r] - mul(hashed[l], power[r - l]);
    return ret < mod ? ret : ret - mod;
  }
  size_type size() const { return str.size(); }
};

int N;
vector<string> S;

void solve() {
  unordered_map<int, int> mp[30]; // hash,last
  int ans = 0;
  for (int i = 0; i < N; i++) {
    RollingHash Roll(S[i]);
    ans += mp[(int)S[i].back()][Roll.hash(0, S[i].size() - 1)];
    int mask = 0;
    for (int j = S[i].size() - 1; j >= -1; j--) {
      int hash = Roll.hash(0, j + 1);
      for (int x = 0; x < 26; x++) {
        if ((1 << x) & mask) {
          mp[x][hash]++;
        }
      }
      if (j >= 0)
        mask |= 1 << S[i][j];
    }
  }
  cout << ans << endl;
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  cin >> N;
  S.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> S[i];
    for (auto &s : S[i]) {
      s -= 'a';
    }
    reverse(all(S[i]));
  }
  sort(all(S), [](string x, string y) { return x.size() > y.size(); });

  solve();
  return 0;
}