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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &o) {
  out << "(" << o.first << "," << o.second << ")";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &V) {
  for (int i = 0; i < V.size(); i++) {
    out << V[i];
    if (i != V.size() - 1)
      out << " ";
  }
  return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &Mat) {
  for (int i = 0; i < Mat.size(); i++) {
    if (i != 0)
      out << endl;
    out << Mat[i];
  }
  return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const map<S, T> &mp) {
  out << "{ ";
  for (auto it = mp.begin(); it != mp.end(); it++) {
    out << it->first << ":" << it->second;
    if (mp.size() - 1 != distance(mp.begin(), it))
      out << ", ";
  }
  out << " }";
  return out;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

typedef unsigned long long ull;
struct RH {
public:
  vector<unsigned long long> p, hash;
  unsigned long long m, q;
  int n;
  void init(const string s) {
    // m=1000000007;
    m = 67280421310721;
    q = 1;
    n = (int)s.size();
    p.resize(n + 1);
    hash.resize(n + 1);
    p[0] = q;
    hash[0] = 0;
    for (int i = 0; i < n; i++) {
      p[i + 1] = p[i] * m;
      hash[i + 1] = s[i] + hash[i] * m;
    }
  }
  void apply(const char c) {
    p.push_back(p[n] * m);
    hash.push_back(c + hash[n] * m);
    n++;
  }
  unsigned long long h(int i) const { return hash[i]; }
  unsigned long long h(int i, int j) const { return h(j) - h(i) * p[j - i]; }
  unsigned long long back_h(int i) const {
    return h(n) - h(n - i) * p[n - (n - i)];
  }
};

template <class Type> Type solve(Type res = Type()) {
  int N;
  cin >> N;
  vector<string> S(N);
  for (auto &in : S)
    cin >> in;

  map<char, int> one_alpha;
  using Item = pair<ull, char>;
  map<Item, map<char, int>> mp;

  for (int i = 0; i < N; i++) {
    if (S[i].length() == 1) {
      one_alpha[S[i][0]]++;
    } else {
      // add
      RH rh;
      rh.init(S[i]);
      char a = S[i][0], b = S[i][1];
      mp[make_pair(rh.h(2, (int)S[i].length()), b)][a]++;
    }
  }
  for (int i = 0; i < N; i++) {
    if (S[i].length() == 1) {
      res += one_alpha[S[i][0]];
    } else {
      {
        set<char> alpha;
        for (auto &c : S[i])
          alpha.emplace(c);

        int T = 0;
        for (auto &c : alpha) {
          T += one_alpha[c];
        }
        res += T;
      }
      {
        RH rh;
        rh.init(S[i]);
        // search
        multiset<char> st;
        for (int j = 0; j < (int)S[i].length(); j++) {
          st.emplace(S[i][j]);
        }

        int T = 0;
        for (int j = (int)S[i].length(); j >= 2; j--) {
          st.erase(st.lower_bound(S[i][j - 1]));
          ull h = rh.h(j, (int)S[i].length());

          for (auto &p : mp[make_pair(h, S[i][j - 1])]) {
            if (st.find(p.first) != st.end()) {
              T += p.second;
            }
          }
        }
        res += T;
      }
    }
  }
  res = res - N;

  return res;
}

int main(void) {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  // solve(0);
  cout << fixed << setprecision(15) << solve<ll>() << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &o) {
  out << "(" << o.first << "," << o.second << ")";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &V) {
  for (int i = 0; i < V.size(); i++) {
    out << V[i];
    if (i != V.size() - 1)
      out << " ";
  }
  return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &Mat) {
  for (int i = 0; i < Mat.size(); i++) {
    if (i != 0)
      out << endl;
    out << Mat[i];
  }
  return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const map<S, T> &mp) {
  out << "{ ";
  for (auto it = mp.begin(); it != mp.end(); it++) {
    out << it->first << ":" << it->second;
    if (mp.size() - 1 != distance(mp.begin(), it))
      out << ", ";
  }
  out << " }";
  return out;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

typedef unsigned long long ull;
struct RH {
public:
  vector<unsigned long long> p, hash;
  unsigned long long m, q;
  int n;
  void init(const string s) {
    // m=1000000007;
    m = 67280421310721;
    q = 1;
    n = (int)s.size();
    p.resize(n + 1);
    hash.resize(n + 1);
    p[0] = q;
    hash[0] = 0;
    for (int i = 0; i < n; i++) {
      p[i + 1] = p[i] * m;
      hash[i + 1] = s[i] + hash[i] * m;
    }
  }
  void apply(const char c) {
    p.push_back(p[n] * m);
    hash.push_back(c + hash[n] * m);
    n++;
  }
  unsigned long long h(int i) const { return hash[i]; }
  unsigned long long h(int i, int j) const { return h(j) - h(i) * p[j - i]; }
  unsigned long long back_h(int i) const {
    return h(n) - h(n - i) * p[n - (n - i)];
  }
};

template <class Type> Type solve(Type res = Type()) {
  int N;
  cin >> N;
  vector<string> S(N);
  for (auto &in : S)
    cin >> in;

  map<char, int> one_alpha;
  map<ull, map<pair<char, char>, int>> mp;

  for (int i = 0; i < N; i++) {
    if (S[i].length() == 1) {
      one_alpha[S[i][0]]++;
    } else {
      // add
      RH rh;
      rh.init(S[i]);
      char a = S[i][0], b = S[i][1];
      mp[rh.h(2, (int)S[i].length())][make_pair(a, b)]++;
    }
  }
  for (int i = 0; i < N; i++) {
    if (S[i].length() == 1) {
      res += one_alpha[S[i][0]];
    } else {
      {
        set<char> alpha;
        for (auto &c : S[i])
          alpha.emplace(c);

        int T = 0;
        for (auto &c : alpha) {
          T += one_alpha[c];
        }
        res += T;
      }
      {
        RH rh;
        rh.init(S[i]);
        // search
        multiset<char> st;
        for (int j = 0; j < (int)S[i].length(); j++) {
          st.emplace(S[i][j]);
        }

        int T = 0;
        for (int j = (int)S[i].length(); j >= 2; j--) {
          st.erase(st.lower_bound(S[i][j - 1]));
          ull h = rh.h(j, (int)S[i].length());

          for (auto &p : mp[h]) {
            pair<char, char> ab;
            int num;
            tie(ab, num) = p;

            if (ab.second == S[i][j - 1]) {
              if (st.find(ab.first) != st.end()) {
                T += num;
              }
            }
          }
        }
        res += T;
      }
    }
  }
  res = res - N;

  return res;
}

int main(void) {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  // solve(0);
  cout << fixed << setprecision(15) << solve<ll>() << endl;
  return 0;
}