Problem p02589 (C++)

Buggy submission (status: Time Limit Exceeded):
// By TheOneYouWant
#pragma GCC optimize("-O2")
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
// #define int unsigned long long int
#define memreset(a) memset(a, 0, sizeof(a))
#define testcase(t)                                                            \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define forstl(i, v) for (auto &i : v)
#define forn(i, e) for (int i = 0; i < e; ++i)
#define forsn(i, s, e) for (int i = s; i < e; ++i)
#define rforn(i, s) for (int i = s; i >= 0; --i)
#define rforsn(i, s, e) for (int i = s; i >= e; --i)
#define bitcount(a) __builtin_popcount(a) // set bits (add ll)
#define ln '\n'
#define getcurrtime()                                                          \
  cerr << "Time = " << ((double)clock() / CLOCKS_PER_SEC) << endl
#define dbgarr(v, s, e)                                                        \
  cerr << #v << " = ";                                                         \
  forsn(i, s, e) cerr << v[i] << ", ";                                         \
  cerr << endl
#define inputfile freopen("input.txt", "r", stdin)
#define outputfile freopen("output.txt", "w", stdout)

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> p64;
typedef pair<int, int> p32;
typedef pair<int, p32> p96;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<v32> vv32;
typedef vector<v64> vv64;
typedef vector<p32> vp32;
typedef vector<p64> vp64;
typedef vector<vp32> vvp32;
typedef map<int, int> m32;
const int mul1 = 1003, mul2 = 5, mul3 = 8;
const int MOD3 = 179424673, MOD2 = 982451653, MOD1 = 1190494759;
const ld EPS = 1e-9;

int read() {
  int xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = 10 * xx + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}

ull hasher(ull a, ull b) {
  return 14831553208299411291ull * (a ^ 16184882973064831860ull) + b;
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n;
const int LIM = 2e5 + 5;
string s[LIM];
map<ull, int> m;

int main() {
  fastio;

  cin >> n;

  ll ans = -n;

  forn(i, n) {
    cin >> s[i];
    reverse(s[i].begin(), s[i].end());

    ull h1 = 0;
    forn(j, s[i].length()) { h1 = hasher(h1, s[i][j]); }
    ++m[h1];
  }

  forn(i, n) {
    int num[26] = {0};
    forn(j, s[i].length()) { ++num[s[i][j] - 'a']; }

    ull h1 = 0;
    forn(j, s[i].length()) {
      forn(k, 26) {
        if (num[k]) {
          ull n1 = hasher(h1, k + 'a');
          ans += m[n1];
        }
      }
      h1 = hasher(h1, s[i][j]);
      --num[s[i][j] - 'a'];
    }
  }

  cout << ans << ln;
  return 0;
}


Fixed submission:
// By TheOneYouWant
#pragma GCC optimize("-O2")
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
// #define int unsigned long long int
#define memreset(a) memset(a, 0, sizeof(a))
#define testcase(t)                                                            \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define forstl(i, v) for (auto &i : v)
#define forn(i, e) for (int i = 0; i < e; ++i)
#define forsn(i, s, e) for (int i = s; i < e; ++i)
#define rforn(i, s) for (int i = s; i >= 0; --i)
#define rforsn(i, s, e) for (int i = s; i >= e; --i)
#define bitcount(a) __builtin_popcount(a) // set bits (add ll)
#define ln '\n'
#define getcurrtime()                                                          \
  cerr << "Time = " << ((double)clock() / CLOCKS_PER_SEC) << endl
#define dbgarr(v, s, e)                                                        \
  cerr << #v << " = ";                                                         \
  forsn(i, s, e) cerr << v[i] << ", ";                                         \
  cerr << endl
#define inputfile freopen("input.txt", "r", stdin)
#define outputfile freopen("output.txt", "w", stdout)

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> p64;
typedef pair<int, int> p32;
typedef pair<int, p32> p96;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<v32> vv32;
typedef vector<v64> vv64;
typedef vector<p32> vp32;
typedef vector<p64> vp64;
typedef vector<vp32> vvp32;
typedef map<int, int> m32;
const int mul1 = 1003, mul2 = 5, mul3 = 8;
const int MOD3 = 179424673, MOD2 = 982451653, MOD1 = 1190494759;
const ld EPS = 1e-9;

int read() {
  int xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = 10 * xx + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}

ull hasher(ull a, ull b) {
  return 14831553208299411291ull * (a ^ 16184882973064831860ull) + b;
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n;
const int LIM = 2e5 + 5;
string s[LIM];
map<ull, int> m;

int main() {
  fastio;

  cin >> n;

  ll ans = -n;

  forn(i, n) {
    cin >> s[i];
    reverse(s[i].begin(), s[i].end());

    ull h1 = 0;
    forn(j, s[i].length()) { h1 = hasher(h1, s[i][j]); }
    ++m[h1];
  }

  forn(i, n) {
    int num[26] = {0};
    forn(j, s[i].length()) { ++num[s[i][j] - 'a']; }

    ull h1 = 0;
    forn(j, s[i].length()) {
      forn(k, 26) {
        if (num[k]) {
          ull n1 = hasher(h1, k + 'a');

          auto it = m.find(n1);
          if (it != m.end())
            ans += (*it).second;
        }
      }
      h1 = hasher(h1, s[i][j]);
      --num[s[i][j] - 'a'];
    }
  }

  cout << ans << ln;
  return 0;
}