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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

/*
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
*/

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long long, long long> pll;
typedef vector<pll> vpll;

typedef long double ld;
typedef vector<ld> vld;

typedef vector<bool> vb;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, n) for (ll i = (n); i >= 1; i--)
#define all(v) (v).begin(), (v).end()

template <class T> void chmin(T &a, T b) { a = min(a, b); }
template <class T> void chmax(T &a, T b) { a = max(a, b); }

constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

struct RollingHash {
  static const ll b1 = 1007, b2 = 2009;
  static const ll mod1 = 1000000007, mod2 = 1000000009;
  vll h1, h2, p1, p2;

  RollingHash(const string &s) {
    ll n = s.size();
    h1.assign(n + 1, 0);
    h2.assign(n + 1, 0);
    p1.assign(n + 1, 1);
    p2.assign(n + 1, 1);
    for (int i = 0; i < n; ++i) {
      h1[i + 1] = (h1[i] * b1 + s[i]) % mod1;
      h2[i + 1] = (h2[i] * b2 + s[i]) % mod2;
      p1[i + 1] = (p1[i] * b1) % mod1;
      p2[i + 1] = (p2[i] * b2) % mod2;
    }
  }

  void add(char c, ll i) {
    h1[i + 1] = (h1[i] * b1 + c) % mod1;
    h2[i + 1] = (h2[i] * b2 + c) % mod2;
    p1[i + 1] = (p1[i] * b1) % mod1;
    p2[i + 1] = (p2[i] * b2) % mod2;
  }

  inline pll get(ll l, ll r) const {
    ll res1 = h1[r] - h1[l] * p1[r - l] % mod1;
    if (res1 < 0)
      res1 += mod1;
    ll res2 = h2[r] - h2[l] * p2[r - l] % mod2;
    if (res2 < 0)
      res2 += mod2;
    return make_pair(res1, res2);
  }

  inline pll get() const {
    int n = h1.size();
    return get(0, n - 1);
  }
};

typedef pair<ll, string> pls;
bool comp(const pls &a, const pls &b) { return a.first < b.first; }

void solve() {
  ll n;
  cin >> n;
  vector<pair<ll, string>> v;
  rep(i, n) {
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    v.emplace_back(s.size(), s);
  }
  sort(v.begin(), v.end(), comp);
  RollingHash rh = RollingHash(v[n - 1].second);
  ll ans = 0;
  set<pll> set;
  rep(i, n) {
    string &t = v[i].second;
    ll tsize = t.size();
    vll c(26);
    rep(j, tsize)++ c[t[j] - 'a'];
    string _t = "";
    rep(j, tsize) {
      rep(k, 26) if (c[k] > 0) {
        char ch = 'a' + k;
        rh.add(ch, j);
        pll q = rh.get(0, j + 1);
        if (!set.empty() && set.find(q) != set.end())
          ++ans;
      }
      rh.add(t[j], j);
      _t += t[j];
      --c[t[j] - 'a'];
    }
    pll p = rh.get(0, tsize);
    set.insert(p);
  }
  cout << ans << endl;
  return;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

/*
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
*/

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long long, long long> pll;
typedef vector<pll> vpll;

typedef long double ld;
typedef vector<ld> vld;

typedef vector<bool> vb;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, n) for (ll i = (n); i >= 1; i--)
#define all(v) (v).begin(), (v).end()

template <class T> void chmin(T &a, T b) { a = min(a, b); }
template <class T> void chmax(T &a, T b) { a = max(a, b); }

constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

struct RollingHash {
  static const ll b1 = 1007, b2 = 2009;
  static const ll mod1 = 1000000007, mod2 = 1000000009;
  vll h1, h2, p1, p2;

  RollingHash(const string &s) {
    ll n = s.size();
    h1.assign(n + 1, 0);
    h2.assign(n + 1, 0);
    p1.assign(n + 1, 1);
    p2.assign(n + 1, 1);
    for (int i = 0; i < n; ++i) {
      h1[i + 1] = (h1[i] * b1 + s[i]) % mod1;
      h2[i + 1] = (h2[i] * b2 + s[i]) % mod2;
      p1[i + 1] = (p1[i] * b1) % mod1;
      p2[i + 1] = (p2[i] * b2) % mod2;
    }
  }

  void add(char c, ll i) {
    h1[i + 1] = (h1[i] * b1 + c) % mod1;
    h2[i + 1] = (h2[i] * b2 + c) % mod2;
    p1[i + 1] = (p1[i] * b1) % mod1;
    p2[i + 1] = (p2[i] * b2) % mod2;
  }

  inline pll get(ll l, ll r) const {
    ll res1 = h1[r] - h1[l] * p1[r - l] % mod1;
    if (res1 < 0)
      res1 += mod1;
    ll res2 = h2[r] - h2[l] * p2[r - l] % mod2;
    if (res2 < 0)
      res2 += mod2;
    return make_pair(res1, res2);
  }

  inline pll get() const {
    int n = h1.size();
    return get(0, n - 1);
  }
};

typedef pair<ll, string> pls;
bool comp(const pls &a, const pls &b) { return a.first > b.first; }

void solve() {
  ll n;
  cin >> n;
  vector<pair<ll, string>> v;
  rep(i, n) {
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    v.emplace_back(s.size(), s);
  }
  sort(v.begin(), v.end(), comp);
  map<pll, ll> mp;
  RollingHash rh = RollingHash(v[0].second);
  ll ans = 0;
  rep(i, n) {
    string &t = v[i].second;
    ll tsize = t.size();
    rep(j, tsize) rh.add(t[j], j);
    pll p = rh.get(0, tsize);
    ans += mp[p];
    vll c(26);
    rep(j, tsize)++ c[t[j] - 'a'];
    string _t = "";
    rep(j, tsize) {
      rep(k, 26) if (c[k] > 0) {
        char ch = 'a' + k;
        rh.add(ch, j);
        pll q = rh.get(0, j + 1);
        ++mp[q];
      }
      rh.add(t[j], j);
      _t += t[j];
      --c[t[j] - 'a'];
    }
  }
  cout << ans << endl;
  return;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
}