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:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

// Trie
/*-----------------------------*/
// Map[{l,k}] := cnt of a group of k strings such that they a common prefix of
// lenght l
map<pii, int> Map;
/*-------Trie Setting---------*/
const int Alphabets = 26;
const int AlphabetBase = 'a'; // '0'
/*------------------------------------*/
class Trie {
public:
  struct node {
    bool isleaf;
    node *children[Alphabets];
    int id;
    int cnt;
    node() : cnt(0), isleaf(0), id(nodesize++) {
      memset(children, 0, sizeof children);
    }
  } *root;
  static int nodesize;

  // check children
  static bool is_free_node(node *n) {
    for (int i = 0; i < Alphabets; i++) {
      if (n->children[i] != NULL)
        return true;
    }
    return false;
  }

  bool _remove(const string &s, node *n, int level) {
    if (level == (int)s.size())
      return 1;
    else {
      int idx = s[level] - AlphabetBase;
      if (_remove(s, n->children[idx], level + 1)) {
        Map[pii(level + 1, n->children[idx]->cnt)]--;
        n->children[idx]->cnt--;
        return 1;
      }
    }
    return 1;
  }

  void DEL(const string &s, node *n, int level) {
    if (level == (int)s.size())
      return;
    int idx = s[level] - AlphabetBase;
    n = n->children[idx];
    Map[pii(level + 1, n->cnt)]--;
    n->cnt--;
    DEL(s, n, level + 1);
  }

  static void clean_up(node *n) {
    if (n == 0 || n->isleaf)
      return;
    for (int i = 0; i < Alphabets; i++)
      clean_up(n->children[i]);
    delete n;
  }

public:
  Trie() { root = new node(); }
  ~Trie() { clean_up(root); }

  int getnodesize() { return nodesize; }
  void insert(string &s) {
    node *n = root;
    for (int level = 0; level < (int)s.size(); level++) {
      int idx = s[level] - AlphabetBase;
      if (n->children[idx] != NULL)
        n = n->children[idx];
      else {
        n->children[idx] = new node();
        n = n->children[idx];
      }
      n->cnt++;
      Map[pii(level + 1, n->cnt)]++;
    }
    n->isleaf = 1;
  }
  bool exists(string &s) {
    node *n = root;
    for (int level = 0; level < (int)s.size(); level++) {
      int idx = s[level] - AlphabetBase;
      if (n->children[idx] == NULL)
        return 0;
      n = n->children[idx];
    }
    return (n != 0 && n->isleaf); //
  }
  bool remove(string &s) { return _remove(s, root, 0); }
  void DEL(string &s) { DEL(s, root, 0); }
};
int Trie::nodesize = 0;

template <class Type> Type solve(Type res = Type()) {
  int N;
  cin >> N;
  vector<string> S(N);
  for (auto &in : S) {
    cin >> in;
    reverse(in.begin(), in.end());
  }
  Trie T;
  for (auto &s : S)
    T.insert(s);

  for (int i = 0; i < N; i++) {
    int cnt[26] = {0};
    for (char c : S[i]) {
      cnt[c - 'a']++;
    }

    Trie::node *n = T.root;

    for (int j = 0; j < (int)S[i].length() - 1; j++) {
      for (int k = 0; k < 26; k++) {
        if (cnt[k]) {
          if (n->children[k] != NULL) {
            if (n->children[k]->isleaf) {
              res++;
            }
          }
        }
      }

      char c = S[i][j];
      n = n->children[c - 'a'];
      cnt[c - 'a']--;
    }
  }
  // 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:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

// Trie
/*-----------------------------*/
// Map[{l,k}] := cnt of a group of k strings such that they a common prefix of
// lenght l
map<pii, int> Map;
/*-------Trie Setting---------*/
const int Alphabets = 26;
const int AlphabetBase = 'a'; // '0'
/*------------------------------------*/
class Trie {
  struct node {
    bool isleaf;
    node *children[Alphabets];
    int id;
    int cnt;
    node() : cnt(0), isleaf(0), id(nodesize++) {
      memset(children, 0, sizeof children);
    }
  } *root;
  static int nodesize;

  // check children
  static bool is_free_node(node *n) {
    for (int i = 0; i < Alphabets; i++) {
      if (n->children[i] != NULL)
        return true;
    }
    return false;
  }

  bool _remove(const string &s, node *n, int level) {
    if (level == (int)s.size())
      return 1;
    else {
      int idx = s[level] - AlphabetBase;
      if (_remove(s, n->children[idx], level + 1)) {
        Map[pii(level + 1, n->children[idx]->cnt)]--;
        n->children[idx]->cnt--;
        return 1;
      }
    }
    return 1;
  }

  void DEL(const string &s, node *n, int level) {
    if (level == (int)s.size())
      return;
    int idx = s[level] - AlphabetBase;
    n = n->children[idx];
    Map[pii(level + 1, n->cnt)]--;
    n->cnt--;
    DEL(s, n, level + 1);
  }

  static void clean_up(node *n) {
    if (n == 0 || n->isleaf)
      return;
    for (int i = 0; i < Alphabets; i++)
      clean_up(n->children[i]);
    delete n;
  }

public:
  Trie() { root = new node(); }
  ~Trie() { clean_up(root); }

  int getnodesize() { return nodesize; }
  void insert(string &s) {
    node *n = root;
    for (int level = 0; level < (int)s.size(); level++) {
      int idx = s[level] - AlphabetBase;
      if (n->children[idx] != NULL)
        n = n->children[idx];
      else {
        n->children[idx] = new node();
        n = n->children[idx];
      }
      n->cnt++;
      Map[pii(level + 1, n->cnt)]++;
    }
    n->isleaf = 1;
  }
  bool exists(string &s) {
    node *n = root;
    for (int level = 0; level < (int)s.size(); level++) {
      int idx = s[level] - AlphabetBase;
      if (n->children[idx] == NULL)
        return 0;
      n = n->children[idx];
    }
    return (n != 0 && n->isleaf); //
  }
  bool remove(string &s) { return _remove(s, root, 0); }
  void DEL(string &s) { DEL(s, root, 0); }
};
int Trie::nodesize = 0;

template <class Type> Type solve(Type res = Type()) {
  int N;
  cin >> N;
  vector<string> S(N);
  for (auto &in : S) {
    cin >> in;
    reverse(in.begin(), in.end());
  }
  Trie T;
  for (auto &s : S)
    T.insert(s);

  for (int i = 0; i < N; i++) {
    int cnt[26] = {0};
    for (char c : S[i]) {
      cnt[c - 'a']++;
    }

    string s = "";
    for (int j = 0; j < (int)S[i].length() - 1; j++) {
      for (int k = 0; k < 26; k++) {
        if (cnt[k]) {
          s.push_back(k + 'a');

          if (T.exists(s)) {
            // cout << S[i] << " -> " << s << endl;
            res++;
          }

          s.pop_back();
        }
      }

      char c = S[i][j];
      s += c;
      cnt[c - 'a']--;
    }
  }
  // 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;
}