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

#include <bits/stdc++.h>
using namespace std;
// #define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define dbv(v)                                                                 \\
  cerr << #v << "=";                                                           \\
  for (auto _x : v)                                                            \\
    cerr << _x << ", ";                                                        \\
  cerr << endl
#define dba(a, n)                                                              \\
  cerr << #a << "=";                                                           \\
  for (int _i = 0; _i < (n); ++_i)                                             \\
    cerr << a[_i] << ", ";                                                     \\
  cerr << endl
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &x) {
  return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
struct Node {
  int ch[26];
  int spec;
  bool leaf;
};
Node nod[1000005];
int nid;
char str[1000005];
ll ans;
int cnt[1000005][26];
void go(int xi, int c = -1, int pi = -1) {
  Node *x = nod + xi;
  Node *p = nod + pi;
  for (int i = 0; i < 26; ++i) {
    if (!x->ch[i])
      continue;
    go(x->ch[i], i, xi);
    cnt[xi][i] += nod[x->ch[i]].spec;
    for (int j = 0; j < 26; ++j) {
      cnt[xi][j] += cnt[x->ch[i]][j];
    }
  }
  for (int i = 0; i < 26; ++i) {
    if (!x->ch[i])
      continue;
    if (nod[x->ch[i]].leaf) {
      assert(cnt[xi][i] >= 1);
      ans += cnt[xi][i] - 1;
    }
  }
}
int main() {
  int n;
  scanf("%d", &n);
  Node *root = &nod[nid++];
  for (int i = 0; i < n; ++i) {
    scanf("%s", str);
    int len = strlen(str);
    reverse(str, str + len);
    Node *p = root;
    vector<bool> isLas(len), has(26);
    for (int j = len - 1; j >= 0; --j) {
      if (!has[str[j] - 'a']) {
        isLas[j] = true;
        has[str[j] - 'a'] = true;
      }
    }
    for (int j = 0; j < len; ++j) {
      int x = str[j] - 'a';
      if (!p->ch[x]) {
        p->ch[x] = nid++;
      }
      p = &nod[p->ch[x]];
      if (isLas[j])
        p->spec++;
    }
    p->leaf = true;
  }
  go(0);
  printf("%lld\\n", ans);
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define dbv(v)                                                                 \\
  cerr << #v << "=";                                                           \\
  for (auto _x : v)                                                            \\
    cerr << _x << ", ";                                                        \\
  cerr << endl
#define dba(a, n)                                                              \\
  cerr << #a << "=";                                                           \\
  for (int _i = 0; _i < (n); ++_i)                                             \\
    cerr << a[_i] << ", ";                                                     \\
  cerr << endl
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &x) {
  return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
struct Node {
  Node *ch[26];
  int id;
  int spec;
  bool leaf;
};
char str[1000005];
ll ans;
int cnt[1000005][26];
void go(Node *x, int c = -1, Node *p = nullptr, string s = "") {
  for (int i = 0; i < 26; ++i) {
    if (!x->ch[i])
      continue;
    string t = s;
    t += 'a' + i;
    go(x->ch[i], i, x, t);
    cnt[x->id][i] += x->ch[i]->spec;
    for (int j = 0; j < 26; ++j) {
      cnt[x->id][j] += cnt[x->ch[i]->id][j];
    }
  }
  for (int i = 0; i < 26; ++i) {
    if (!x->ch[i])
      continue;
    if (x->ch[i]->leaf) {
      assert(cnt[x->id][i] >= 1);
      ans += cnt[x->id][i] - 1;
    }
  }
}
int main() {
  int n;
  scanf("%d", &n);
  int nid = 1;
  Node *root = new Node();
  for (int i = 0; i < n; ++i) {
    scanf("%s", str);
    int len = strlen(str);
    reverse(str, str + len);
    Node *p = root;
    vector<bool> isLas(len), has(26);
    for (int j = len - 1; j >= 0; --j) {
      if (!has[str[j] - 'a']) {
        isLas[j] = true;
        has[str[j] - 'a'] = true;
      }
    }
    for (int j = 0; j < len; ++j) {
      int x = str[j] - 'a';
      if (!p->ch[x]) {
        p->ch[x] = new Node();
        p->ch[x]->id = nid++;
      }
      p = p->ch[x];
      if (isLas[j])
        p->spec++;
    }
    p->leaf = true;
  }
  go(root);
  printf("%lld\\n", ans);
}