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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#define REP(_i, _a, _n) for (int _i = _a; _i <= _n; ++_i)
#define PER(_i, _a, _n) for (int _i = _n; _i >= _a; --_i)
#define hr putchar(10)
#define pb push_back
#define lc (o << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define ls lc, l, mid
#define rs rc, mid + 1, r
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int P = 1e9 + 7, INF = 0x3f3f3f3f;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll qpow(ll a, ll n) {
  ll r = 1 % P;
  for (a %= P; n; a = a * a % P, n >>= 1)
    if (n & 1)
      r = r * a % P;
  return r;
}
ll inv(ll x) { return x <= 1 ? 1 : inv(P % x) * (P - P / x) % P; }
inline int rd() {
  int x = 0;
  char p = getchar();
  while (p < '0' || p > '9')
    p = getchar();
  while (p >= '0' && p <= '9')
    x = x * 10 + p - '0', p = getchar();
  return x;
}
// head

#ifdef __APPLE__
const int N = 1e2 + 50;
#else
const int N = 1e6 + 50;
#endif

const int P1 = 876756319, B1 = 991;
const int P2 = 799898821, B2 = 2333;
int n, fac1[N], fac2[N], f1[N], f2[N], vis[N], now[N];
string s[N];
pii f[N];
map<pii, int> mp, mp2;

int main() {
  fac1[0] = fac2[0] = 1;
  for (int i = 1; i < N; ++i) {
    fac1[i] = (long long)fac1[i - 1] * B1 % P1;
    fac2[i] = (long long)fac2[i - 1] * B2 % P2;
  }
  ios::sync_with_stdio(0);
  cin >> n;
  REP(i, 1, n) {
    cin >> s[i];
    if (s[i].size() == 1) {
      now[s[i][0]] = 1;
      continue;
    }
    int x = 0, y = 0, sz = s[i].size();
    for (int j = 1; j < sz; ++j) {
      x = ((ll)x * B1 + s[i][j] - 'a' + 1) % P1;
      y = ((ll)y * B2 + s[i][j] - 'a' + 1) % P2;
    }
    mp2[{x, y}] ^= 1 << s[i][0] - 'a';
    x = (x + (s[i][0] - 'a' + 1ll) * fac1[sz - 1]) % P1;
    y = (y + (s[i][0] - 'a' + 1ll) * fac2[sz - 1]) % P2;
    ++mp[{x, y}];
  }
  ll ans = 0;
  REP(i, 1, n) if (s[i].size() > 1) {
    int x = 0, y = 0, sz = s[i].size();
    for (int j = sz - 1; j >= 0; --j) {
      x = (x + (s[i][j] - 'a' + 1ll) * fac1[sz - j - 1]) % P1;
      y = (y + (s[i][j] - 'a' + 1ll) * fac2[sz - j - 1]) % P2;
      f[j] = {x, y};
    }
    REP(i, 'a', 'z') vis[i] = 0;
    int sta = 0;
    for (int j = 0; j < sz; ++j) {
      if (j >= 2) {
        int w = sta & mp2[f[j]];
        if (w >> s[i][j - 1] - 'a' & 1)
          w ^= 1 << s[i][j - 1] - 'a';
        ans += __builtin_popcount(w);
      }
      if (j) {
        vis[s[i][j - 1]] = 1;
        sta |= 1 << s[i][j - 1] - 'a';
        if (sz - j > 1)
          ans += mp[f[j]];
      }
    }
    vis[s[i][sz - 1]] = 1;
    REP(c, 'a', 'z') if (vis[c] && now[c])++ ans;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#define REP(_i, _a, _n) for (int _i = _a; _i <= _n; ++_i)
#define PER(_i, _a, _n) for (int _i = _n; _i >= _a; --_i)
#define hr putchar(10)
#define pb push_back
#define lc (o << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define ls lc, l, mid
#define rs rc, mid + 1, r
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int P = 1e9 + 7, INF = 0x3f3f3f3f;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll qpow(ll a, ll n) {
  ll r = 1 % P;
  for (a %= P; n; a = a * a % P, n >>= 1)
    if (n & 1)
      r = r * a % P;
  return r;
}
ll inv(ll x) { return x <= 1 ? 1 : inv(P % x) * (P - P / x) % P; }
inline int rd() {
  int x = 0;
  char p = getchar();
  while (p < '0' || p > '9')
    p = getchar();
  while (p >= '0' && p <= '9')
    x = x * 10 + p - '0', p = getchar();
  return x;
}
// head

#ifdef __APPLE__
const int N = 1e2 + 50;
#else
const int N = 1e6 + 50;
#endif

const int P1 = 876756319, B1 = 991;
const int P2 = 799898821, B2 = 2333;
int n, fac1[N], fac2[N], f1[N], f2[N], vis[N], now[N];
string s[N];
pii f[N];
map<pii, int> mp;

int main() {
  fac1[0] = fac2[0] = 1;
  for (int i = 1; i < N; ++i) {
    fac1[i] = (long long)fac1[i - 1] * B1 % P1;
    fac2[i] = (long long)fac2[i - 1] * B2 % P2;
  }
  ios::sync_with_stdio(0);
  cin >> n;
  REP(i, 1, n) {
    cin >> s[i];
    int x = 0, y = 0;
    for (int j = 0; j < s[i].size(); ++j) {
      x = ((ll)x * B1 + s[i][j] - 'a' + 1) % P1;
      y = ((ll)y * B2 + s[i][j] - 'a' + 1) % P2;
    }
    ++mp[{x, y}];
    if (s[i].size() == 1)
      now[s[i][0]] = 1;
  }
  ll ans = 0;
  REP(i, 1, n) if (s[i].size() > 1) {
    int x = 0, y = 0, sz = s[i].size();
    for (int j = sz - 1; j >= 0; --j) {
      x = (x + (s[i][j] - 'a' + 1ll) * fac1[sz - j - 1]) % P1;
      y = (y + (s[i][j] - 'a' + 1ll) * fac2[sz - j - 1]) % P2;
      f[j] = {x, y};
    }
    REP(i, 'a', 'z') vis[i] = 0;
    for (int j = 0; j < sz; ++j) {
      if (j >= 2) {
        REP(c, 'a', 'z') if (vis[c]) {
          int x = (f[j].x + (c - 'a' + 1ll) * fac1[sz - j]) % P1;
          int y = (f[j].y + (c - 'a' + 1ll) * fac2[sz - j]) % P2;
          pii u(x, y);
          if (u != f[j - 1])
            ans += mp[u];
        }
      }
      if (j) {
        vis[s[i][j - 1]] = 1;
        if (sz - j > 1)
          ans += mp[f[j]];
      }
    }
    vis[s[i][sz - 1]] = 1;
    REP(c, 'a', 'z') if (vis[c] && now[c])++ ans;
  }
  cout << ans << endl;
}