Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
using namespace std;
using ll = long long;
using PL = pair<ll, ll>;
using P = pair<int, int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007; // = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;

template <int char_size, int base> struct Trie {
  struct Node {
    vector<int> child, accept;
    int c;
    int common;
    Node(int c) : c(c), common(0) { child.assign(char_size, -1); }
  };
  vector<Node> tree;
  int root;
  Trie() : root(0) { tree.push_back(Node(root)); }
  void insert(const string &s, int id) {
    int node_id = 0;
    for (int i = 0; i < (int)s.size(); ++i) {
      int c = (int)s[i] - base;
      int &next_id = tree[node_id].child[c];
      if (next_id < 0) {
        next_id = (int)tree.size();
        tree.push_back(Node(c));
      }
      ++tree[node_id].common;
      node_id = next_id;
    }
    ++tree[node_id].common;
    tree[node_id].accept.push_back(id);
  }
  // insert s to Trie
  void insert(const string &s) { insert(s, tree[0].common); }
  // return the number of s in Trie
  int search(const string &s) {
    vector<int> alpha(26, 0);
    rep(i, s.size()) alpha[s[i] - base]++;

    int node_id = 0;
    int ans = 0;
    for (int i = 0; i < (int)s.size(); ++i) {
      rep(j, 26) {
        if (alpha[j] > 0 && tree[node_id].child[j] >= 0) {
          int x = tree[node_id].child[j];
          if ((int)tree[x].accept.size() > 0)
            ++ans;
        }
      }
      alpha[s[i] - base]--;
      int c = (int)s[i] - base;
      int &next_id = tree[node_id].child[c];
      if (next_id < 0)
        return ans;
      node_id = next_id;
    }
    return ans;
  }
};

int main() {
  int N;
  cin >> N;
  vector<string> S(N);
  rep(i, N) cin >> S[i];
  sort(S.begin(), S.end(),
       [](const string &a, const string &b) { return a.size() < b.size(); });

  ll ans = 0;
  Trie<26, 'a'> tr;
  rep(i, N) {
    string s = S[i];
    reverse(s.begin(), s.end());
    ans += (ll)tr.search(s);
    tr.insert(s);
  }
  cout << ans << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
using namespace std;
using ll = long long;
using PL = pair<ll, ll>;
using P = pair<int, int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007; // = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;

template <int char_size, int base> struct Trie {
  struct Node {
    vector<int> child, accept;
    int c;
    int common;
    Node(int c) : c(c), common(0) { child.assign(char_size, -1); }
  };
  vector<Node> tree;
  int root = 0;
  Trie() { tree.push_back(Node(0)); }
  void insert(const string &s, int id) {
    int node_id = 0;
    for (int i = 0; i < (int)s.size(); ++i) {
      int c = s[i] - base;
      int &next_id = tree[node_id].child[c];
      if (next_id < 0) {
        next_id = (int)tree.size();
        tree.push_back(Node(c));
      }
      ++tree[node_id].common;
      node_id = next_id;
    }
    ++tree[node_id].common;
    tree[node_id].accept.push_back(id);
  }
  // insert s to Trie
  void insert(const string &s) { insert(s, tree[0].common); }
  // return the number of s in Trie
  int search(const string &s) {
    vector<int> alpha(26, 0);
    rep(i, s.size()) alpha[s[i] - base]++;

    int node_id = 0;
    int ans = 0;
    for (int i = 0; i < (int)s.size(); ++i) {
      rep(j, 26) {
        if (alpha[j] > 0 && tree[node_id].child[j]) {
          int x = tree[node_id].child[j];
          if (tree[x].accept.size() > 0)
            ++ans;
        }
      }
      alpha[s[i] - base]--;
      int c = s[i] - base;
      int &next_id = tree[node_id].child[c];
      if (next_id < 0)
        return ans;
      node_id = next_id;
    }
    return ans;
  }
};

int main() {
  int N;
  cin >> N;
  vector<string> S(N);
  rep(i, N) cin >> S[i];
  sort(S.begin(), S.end(),
       [](const string &a, const string &b) { return a.size() < b.size(); });

  ll ans = 0;
  Trie<26, 'a'> tr;
  rep(i, N) {
    string s = S[i];
    reverse(s.begin(), s.end());
    ans += (ll)tr.search(s);
    tr.insert(s);
  }
  cout << ans << '\\n';
  return 0;
}