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

#include <bits/stdc++.h>
using namespace std;
#define modulo 998244353
#define mod(mod_x) ((((long long)mod_x + modulo)) % modulo)
#define Inf 1000000001

template <typename T> struct trie {
  T init_value;
  struct node {
    vector<int> next;
    vector<int> v;
    int depth;
    node(int k, T iv, int d) {
      next.resize(k, -1);
      v = iv;
      depth = d;
    }

    int link;
  };
  vector<node> _N;
  int n;
  trie(int k, T iv) {
    init_value = iv;
    n = k;
    _N.push_back(node(n, init_value, 0));
  }

  T add(string &s, int now = 0, int nowv = 0) {
    if (now == s.size())
      return init_value;
    int c = get(s[now]);
    if (_N[nowv].next[c] == -1) {
      _N[nowv].next[c] = _N.size();
      _N.push_back(node(n, init_value, _N[nowv].depth + 1));
    }
    int newv = _N[nowv].next[c];
    vector<int> V = init_value;
    V = add(s, now + 1, newv);
    V[c]++;
    for (int i = 0; i < 26; i++) {
      if (V[i] > 0) {
        _N[nowv].v[i]++;
      }
    }
    return V;
  }

  T get_value(string &S) {
    T ret = init_value;
    ;
    int now = 0;
    for (int i = 0; i < S.size(); i++) {
      int c = get(S[i]);
      if (_N[now].next[c] != -1) {
        now = _N[now].next[c];
      } else {
        return ret;
      }
    }
    return _N[now].v;
  }

  int get(char c) { return c - 'a'; }
};

int main() {

  int N;
  cin >> N;

  vector<vector<string>> X(1000000, vector<string>());

  for (int i = 0; i < N; i++) {
    string S;
    cin >> S;
    reverse(S.begin(), S.end());
    X[(int)S.size() - 1].push_back(S);
  }

  vector<string> S;

  for (int i = 1000000 - 1; i >= 0; i--) {
    for (int j = 0; j < X[i].size(); j++)
      S.push_back(X[i][j]);
  }

  trie<vector<int>> T(26, vector<int>(26, 0));

  long long ans = 0LL;

  for (int i = 0; i < N; i++) {
    char c = S[i].back();
    S[i].pop_back();
    vector<int> ret = T.get_value(S[i]);
    ans += ret[c - 'a'];
    S[i] += c;
    T.add(S[i]);
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define modulo 998244353
#define mod(mod_x) ((((long long)mod_x + modulo)) % modulo)
#define Inf 1000000001

template <typename T> struct trie {
  T init_value;
  struct node {
    vector<int> next;
    T v;
    int depth;
    node(int k, T iv, int d) {
      next.resize(k, -1);
      v = iv;
      depth = d;
    }

    int link;
  };
  vector<node> _N;
  vector<string> _S;
  int n;
  trie(int k, T iv) {
    init_value = iv;
    n = k;
    _N.push_back(node(n, init_value, 0));
  }

  T add(string &s, int now = 0, int nowv = 0) {
    if (now == s.size())
      return init_value;
    int c = get(s[now]);
    if (_N[nowv].next[c] == -1) {
      _N[nowv].next[c] = _N.size();
      _N.push_back(node(n, init_value, _N[nowv].depth + 1));
    }
    int newv = _N[nowv].next[c];
    vector<int> V = init_value;
    V = add(s, now + 1, newv);
    V[c]++;
    for (int i = 0; i < 26; i++) {
      if (V[i] > 0) {
        _N[nowv].v[i]++;
      }
    }

    _S.push_back(s);
    return V;
  }

  T get_value(string S) {
    T ret = init_value;
    ;
    int now = 0;
    for (int i = 0; i < S.size(); i++) {
      int c = get(S[i]);
      if (_N[now].next[c] != -1) {
        now = _N[now].next[c];
      } else {
        return ret;
      }
    }
    return _N[now].v;
  }

  int get(char c) { return c - 'a'; }
};

int main() {

  int N;
  cin >> N;

  vector<vector<string>> X(1000000, vector<string>());

  for (int i = 0; i < N; i++) {
    string S;
    cin >> S;
    reverse(S.begin(), S.end());
    X[(int)S.size() - 1].push_back(S);
  }

  vector<string> S;

  for (int i = 1000000 - 1; i >= 0; i--) {
    for (int j = 0; j < X[i].size(); j++)
      S.push_back(X[i][j]);
  }

  trie<vector<int>> T(26, vector<int>(26, 0));

  long long ans = 0LL;

  for (int i = 0; i < N; i++) {
    char c = S[i].back();
    S[i].pop_back();
    vector<int> ret = T.get_value(S[i]);
    ans += ret[c - 'a'];
    S[i] += c;
    T.add(S[i]);
  }

  cout << ans << endl;

  return 0;
}