Problem p02589 (C++) — buggy submission, verdict: 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;

#include <bits/extc++.h>
using namespace __gnu_pbds;

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];
gp_hash_table<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;
}


Error: TLE

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);                                                                  \\
  cout.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;

inline unsigned mod(unsigned int x, unsigned int y) {
  return y & (y - 1) ? x % y : x & (y - 1);
}

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 = (xx << 3) + (xx << 1) + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}

int hash1(ll curr, char c) {
  // ll x =
  return mod(((curr * mul1) + (c - 'a')), MOD1);
  // if(x>MOD1) x %= MOD1;
  // return x;
}
int hash2(ll curr, char c) {
  // ll x =
  return mod(((curr * mul2) + (c - 'a')), MOD2);
  // if(x>MOD2) x %= MOD2;
  // return x;
}
// ll hash3(ll curr, char c){
//     return (curr*mul3 + (c-'a'))%MOD3;
// }

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

int n;
const int LIM = 2e5 + 5;
string s[LIM];
map<tuple<int, int, int>, int> m;
// map<tuple<int,int>,bool> done;

int main() {
  fastio;

  cin >> n;

  ll ans = -n;

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

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

    int h1 = 0, h2 = 0, h3 = 0;
    forn(j, s[i].length()) {
      forn(k, 26) {
        if (num[k] != 0) {
          int n1 = hash1(h1, k + 'a');
          int n2 = hash2(h2, k + 'a');
          m[{j + 1, n1, n2}]++;
          // done[{n1,n2}] = 1;
        }
      }
      h1 = hash1(h1, s[i][j]);
      h2 = hash2(h2, s[i][j]);
      num[s[i][j] - 'a']--;
    }
  }
  forn(i, n) {
    int h1 = 0, h2 = 0, h3 = 0;
    forn(j, s[i].length()) {
      h1 = hash1(h1, s[i][j]);
      h2 = hash2(h2, s[i][j]);
    }
    ans += m[{s[i].length(), h1, h2}];
  }

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