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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define ll long long
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define eb emplace_back
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
constexpr int INF = 1000000000000000000;
constexpr int mod = 1000000007;
constexpr double eps = 1e-8;
const double pi = acos(-1);
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int Random(int mi, int ma) {
  random_device rnd;
  mt19937 mt(rnd()); // 32bit
  //[mi,ma]
  uniform_int_distribution<int> engine(mi, ma);
  return engine(mt);
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool prime(int a) {
  if (a == 1)
    return false;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
int modpow(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return modpow(a, b - 1) * a % mod;
  int memo = modpow(a, b / 2);
  return memo * memo % mod;
}
vector<int> kaijo, invkaijo;
void init_fact(int n) {
  kaijo.resize(n + 1);
  invkaijo.resize(n + 1);
  kaijo[0] = 1;
  for (int i = 1; i <= n; i++) {
    kaijo[i] = kaijo[i - 1] * i;
    kaijo[i] %= mod;
  }
  rep(i, n + 1) invkaijo[i] = modpow(kaijo[i], mod - 2);
}
int comb(int a, int b) {
  if (a < b)
    return 0;
  if (a < 0 || b < 0)
    return 0;
  return kaijo[a] * invkaijo[a - b] % mod * invkaijo[b] % mod;
}
int inv(int x) {
  x = modpow(x, mod - 2);
  return x;
}
bool comp(const string &s1, const string &s2) { return s1.size() < s2.size(); }
struct RollingHash {
  const int MASK30 = (1ll << 30) - 1;
  const int MASK31 = (1ll << 31) - 1;
  const int MOD = (1ll << 61) - 1;
  int Base = 9973;
  vector<int> hash, power;
  int calcmod(int x) {
    int res = (x >> 61) + (x & MOD);
    if (res >= MOD)
      res -= MOD;
    return res;
  }
  int mul(int a, int b) {
    int au = a >> 31;
    int ad = a & MASK31;
    int bu = b >> 31;
    int bd = b & MASK31;
    int mid = ad * bu + au * bd;
    int midu = mid >> 30;
    int midd = mid & MASK30;
    return calcmod(au * bu * 2 + midu + (midd << 31) + ad * bd);
  }
  RollingHash(string s, int base) {
    // Base=Random(1<<7,1<<9)
    Base = base;
    int n = s.size();
    hash.resize(n + 1, 0);
    power.resize(n + 1, 1);
    rep(i, n) {
      hash[i + 1] = (mul(hash[i], Base) + s[i]) % MOD;
      power[i + 1] = mul(power[i], Base) % MOD;
    }
  }
  int get_hash(int l, int r) {
    //[l,r)
    return (hash[r] - mul(hash[l], power[r - l]) + MOD) % MOD;
  }
};
int n;
string s[200010];
multiset<int> st[26];
bool used[1000010];
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  rep(i, n) {
    cin >> s[i];
    used[s[i].size()] = true;
  }
  sort(s, s + n, comp);
  int ans = 0;
  rep(i, n) {
    RollingHash rh(s[i], 9973);
    int Bit = 0;
    rep(j, s[i].size() + 1) {
      if (!used[s[i].size() - j + 1]) {
        if (j < s[i].size())
          Bit |= (1 << (s[i][j] - 'a'));
        continue;
      }
      int hash = rh.get_hash(j, s[i].size());
      rep(k, 26) if ((Bit >> k) & 1) ans += st[k].count(hash);
      if (j < s[i].size())
        Bit |= (1 << (s[i][j] - 'a'));
    }
    st[s[i][0] - 'a'].insert(rh.get_hash(1, s[i].size()));
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define ll long long
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define eb emplace_back
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
constexpr int INF = 1000000000000000000;
constexpr int mod = 1000000007;
constexpr double eps = 1e-8;
const double pi = acos(-1);
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int Random(int mi, int ma) {
  random_device rnd;
  mt19937 mt(rnd()); // 32bit
  //[mi,ma]
  uniform_int_distribution<int> engine(mi, ma);
  return engine(mt);
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool prime(int a) {
  if (a == 1)
    return false;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
int modpow(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return modpow(a, b - 1) * a % mod;
  int memo = modpow(a, b / 2);
  return memo * memo % mod;
}
vector<int> kaijo, invkaijo;
void init_fact(int n) {
  kaijo.resize(n + 1);
  invkaijo.resize(n + 1);
  kaijo[0] = 1;
  for (int i = 1; i <= n; i++) {
    kaijo[i] = kaijo[i - 1] * i;
    kaijo[i] %= mod;
  }
  rep(i, n + 1) invkaijo[i] = modpow(kaijo[i], mod - 2);
}
int comb(int a, int b) {
  if (a < b)
    return 0;
  if (a < 0 || b < 0)
    return 0;
  return kaijo[a] * invkaijo[a - b] % mod * invkaijo[b] % mod;
}
int inv(int x) {
  x = modpow(x, mod - 2);
  return x;
}
bool comp(const string &s1, const string &s2) { return s1.size() < s2.size(); }
struct RollingHash {
  const int MASK30 = (1ll << 30) - 1;
  const int MASK31 = (1ll << 31) - 1;
  const int MOD = (1ll << 61) - 1;
  int Base = 9973;
  vector<int> hash, power;
  int calcmod(int x) {
    int res = (x >> 61) + (x & MOD);
    if (res >= MOD)
      res -= MOD;
    return res;
  }
  int mul(int a, int b) {
    int au = a >> 31;
    int ad = a & MASK31;
    int bu = b >> 31;
    int bd = b & MASK31;
    int mid = ad * bu + au * bd;
    int midu = mid >> 30;
    int midd = mid & MASK30;
    return calcmod(au * bu * 2 + midu + (midd << 31) + ad * bd);
  }
  RollingHash(string s, int base) {
    // Base=Random(1<<7,1<<9)
    Base = base;
    int n = s.size();
    hash.resize(n + 1, 0);
    power.resize(n + 1, 1);
    rep(i, n) {
      hash[i + 1] = (mul(hash[i], Base) + s[i]) % MOD;
      power[i + 1] = mul(power[i], Base) % MOD;
    }
  }
  int get_hash(int l, int r) {
    //[l,r)
    return (hash[r] - mul(hash[l], power[r - l]) + MOD) % MOD;
  }
};
int n;
string s[200010];
map<int, int> st[26];
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  rep(i, n) cin >> s[i];
  sort(s, s + n, comp);
  int ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    RollingHash rh(s[i], 9973);
    ans += st[s[i][0] - 'a'][rh.get_hash(1, s[i].size())];
    int Bit = 0;
    rep(j, s[i].size() + 1) {
      int hash = rh.get_hash(j, s[i].size());
      rep(k, 26) if ((Bit >> k) & 1) st[k][hash]++;
      if (j < s[i].size())
        Bit |= (1 << (s[i][j] - 'a'));
    }
  }
  cout << ans << endl;
  return 0;
}