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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(...) ;
#else
#define debug(...)                                                             \\
  cerr << __LINE__ << " : " << #__VA_ARGS__ << " = " << _tostr(__VA_ARGS__)    \\
       << endl;

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v);

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v)
    out << item << ", ";
  out << "\\b\\b}";
  return out;
}

void _tostr_rec(ostringstream &oss) { oss << "\\b\\b \\b"; }

template <typename Head, typename... Tail>
void _tostr_rec(ostringstream &oss, Head &&head, Tail &&...tail) {
  oss << head << ", ";
  _tostr_rec(oss, forward<Tail>(tail)...);
}

template <typename... T> string _tostr(T &&...args) {
  ostringstream oss;
  int size = sizeof...(args);
  if (size > 1)
    oss << "{";
  _tostr_rec(oss, forward<T>(args)...);
  if (size > 1)
    oss << "}";
  return oss.str();
}
#endif

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 200010

/* Rolling Hash */
// n = 9999991, 10000019, 924844033, 1012924417

struct RollingHash {
  int N, base;
  vector<ll> hash, pow;

  template <typename Type>
  RollingHash(int base, int n, Type *arr)
      : N(n), base(base), hash(n + 1), pow(n + 1) {
    hash[0] = 0;
    pow[0] = 1;
    for (int i = 0; i < N; i++) {
      hash[i + 1] = (hash[i] + arr[i]) * base % mod;
      pow[i + 1] = pow[i] * base % mod;
    }
  }

  // get [l,r)
  ll getHash(int l, int r) {
    return (mod + hash[r] - hash[l] * pow[r - l] % mod) % mod;
  }
};

char S[2000000];
char *s[SIZE];
map<pair<ll, ll>, int> counter[26];

int main() {
  int N, M[SIZE];

  scanf("%d", &N);

  char *c = S;

  for (int i = 0; i < N; i++) {
    scanf("%s", c);
    M[i] = strlen(c);
    s[i] = c;
    c += M[i] + 1;
  }

  debug(c - S);

  RollingHash rh1(1012924417, c - S, S);
  RollingHash rh2(924844033, c - S, S);

  for (int i = 0; i < N; i++) {
    debug(s[i] - S, M[i]);
    int base = s[i] - S;
    pair<ll, ll> p = {rh1.getHash(base + 1, base + M[i]),
                      rh2.getHash(base + 1, base + M[i])};
    counter[s[i][0] - 'a'][p]++;
  }

  ll ans = 0;

  for (int i = 0; i < N; i++) {
    bool f[26] = {};
    for (int j = 0; j < M[i]; j++) {
      f[s[i][j] - 'a'] = true;

      for (int k = 0; k < 26; k++) {
        if (!f[k])
          continue;
        int base = s[i] - S;
        pair<ll, ll> p = {rh1.getHash(base + j + 1, base + M[i]),
                          rh2.getHash(base + j + 1, base + M[i])};
        auto it = counter[k].find(p);

        if (it != counter[k].end()) {
          ans += it->second - (j == 0);
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(...) ;
#else
#define debug(...)                                                             \\
  cerr << __LINE__ << " : " << #__VA_ARGS__ << " = " << _tostr(__VA_ARGS__)    \\
       << endl;

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v);

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v)
    out << item << ", ";
  out << "\\b\\b}";
  return out;
}

void _tostr_rec(ostringstream &oss) { oss << "\\b\\b \\b"; }

template <typename Head, typename... Tail>
void _tostr_rec(ostringstream &oss, Head &&head, Tail &&...tail) {
  oss << head << ", ";
  _tostr_rec(oss, forward<Tail>(tail)...);
}

template <typename... T> string _tostr(T &&...args) {
  ostringstream oss;
  int size = sizeof...(args);
  if (size > 1)
    oss << "{";
  _tostr_rec(oss, forward<T>(args)...);
  if (size > 1)
    oss << "}";
  return oss.str();
}
#endif

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 200010

/* Rolling Hash */
// n = 9999991, 10000019, 924844033, 1012924417

struct RollingHash {
  int N, base;
  vector<ll> hash, pow;

  template <typename Type>
  RollingHash(int base, int n, Type *arr)
      : N(n), base(base), hash(n + 1), pow(n + 1) {
    hash[0] = 0;
    pow[0] = 1;
    for (int i = 0; i < N; i++) {
      hash[i + 1] = (hash[i] + arr[i]) * base % mod;
      pow[i + 1] = pow[i] * base % mod;
    }
  }

  // get [l,r)
  ll getHash(int l, int r) {
    return (mod + hash[r] - hash[l] * pow[r - l] % mod) % mod;
  }
};

char S[2000000];
char *s[SIZE];
vector<RollingHash> rhs[2];
map<pair<ll, ll>, int> counter[26];

int main() {
  int N, M[SIZE];

  scanf("%d", &N);

  char *c = S;

  for (int i = 0; i < N; i++) {
    scanf("%s", c);
    M[i] = strlen(c);
    RollingHash rh1(1012924417, M[i], c);
    RollingHash rh2(924844033, M[i], c);
    pair<ll, ll> p = {rh1.getHash(1, M[i]), rh2.getHash(1, M[i])};
    counter[c[0] - 'a'][p]++;
    rhs[0].push_back(rh1);
    rhs[1].push_back(rh2);

    s[i] = c;
    c += M[i] + 1;
  }

  ll ans = 0;

  for (int i = 0; i < N; i++) {
    bool f[26] = {};
    for (int j = 0; j < M[i]; j++) {
      f[s[i][j] - 'a'] = true;

      for (int k = 0; k < 26; k++) {
        if (!f[k])
          continue;
        pair<ll, ll> p = {rhs[0][i].getHash(j + 1, M[i]),
                          rhs[1][i].getHash(j + 1, M[i])};
        auto it = counter[k].find(p);

        if (it != counter[k].end()) {
          ans += it->second - (j == 0);
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}