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

#include <bits/stdc++.h>
#define sz(x) ((int)(x).size())
using namespace std;
typedef long long ll;
const int n = 1e6 + 5;

int N, M, K;

struct node {
  int eos = 0, chi = 0;
  int cc[26];
  int c[26];
};

int root, NC = 0;
node nodes[n];

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

int createNode() {
  int r = NC++;
  for (int i = 0; i < 26; i++)
    nodes[r].c[i] = -1, nodes[r].cc[i] = 0;
  return r;
}

void insertString(string &s, int i, int n = root) {
  nodes[n].chi++;
  if (i == -1) {
    nodes[n].eos++;
    return;
  }
  if (nodes[n].c[getIndex(s[i])] == -1)
    nodes[n].c[getIndex(s[i])] = createNode();
  // cerr << "insert " << s << " i: " << i << " cc: " << (n->chi) <<endl;
  insertString(s, i - 1, nodes[n].c[getIndex(s[i])]);
  // n->cc[getIndex(s[i])]++;
}

void init() { root = createNode(); }

void doDP(int n = root) {
  for (int i = 0; i < 26; ++i) {
    if (nodes[n].c[i] != -1) {
      doDP(nodes[n].c[i]);
      for (int j = 0; j < 26; ++j) {
        int cv = nodes[nodes[n].c[i]].cc[j];
        if (j == i)
          cv = nodes[nodes[n].c[i]].chi;
        // if(cv > 0) cerr << "cv: " << cv << endl;
        nodes[n].cc[j] += cv;
      }
    }
  }
}

int query(string &s, int i, int n = root) {
  if (i == 0) {
    return nodes[n].cc[getIndex(s[i])];
  }
  if (nodes[n].c[getIndex(s[i])] == -1)
    return 0;
  else
    return query(s, i - 1, nodes[n].c[getIndex(s[i])]);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  vector<string> V;
  init();
  for (int i = 0; i < N; ++i) {
    string s;
    cin >> s;
    V.push_back(s);
    insertString(s, sz(s) - 1);
  }
  doDP();
  ll cc = 0;
  for (string s : V) {
    int v = query(s, sz(s) - 1);
    // cerr << s << ": " << v << endl;
    cc += v - 1;
  }
  cout << cc << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define sz(x) ((int)(x).size())
using namespace std;
typedef long long ll;
const int n = 1e6 + 5;

int N, M, K;

struct node {
  int eos = 0, chi = 0;
  int cc[26];
  int c[26];
};

int root, NC = 0;
node nodes[n];

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

int createNode() {
  int r = NC++;
  for (int i = 0; i < 26; i++)
    nodes[r].c[i] = -1, nodes[r].cc[i] = 0;
  return r;
}

void insertString(string &s, int i, int n = root) {
  nodes[n].chi++;
  if (i == -1) {
    nodes[n].eos++;
    return;
  }
  if (nodes[n].c[getIndex(s[i])] == -1)
    nodes[n].c[getIndex(s[i])] = createNode();
  // cerr << "insert " << s << " i: " << i << " cc: " << (n->chi) <<endl;
  insertString(s, i - 1, nodes[n].c[getIndex(s[i])]);
  // n->cc[getIndex(s[i])]++;
}

void init() { root = createNode(); }

void doDP(int n = root) {
  for (int i = 0; i < 26; ++i) {
    if (nodes[n].c[i] != -1) {
      doDP(nodes[n].c[i]);
      for (int j = 0; j < 26; ++j) {
        int cv = nodes[nodes[n].c[i]].cc[j];
        if (j == i)
          cv = nodes[nodes[n].c[i]].chi;
        // if(cv > 0) cerr << "cv: " << cv << endl;
        nodes[n].cc[j] += cv;
      }
    }
  }
}

int query(string s, int i, int n = root) {
  if (i == 0) {
    return nodes[n].cc[getIndex(s[i])];
  }
  if (nodes[n].c[getIndex(s[i])] == -1)
    return 0;
  else
    return query(s, i - 1, nodes[n].c[getIndex(s[i])]);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  vector<string> V;
  init();
  for (int i = 0; i < N; ++i) {
    string s;
    cin >> s;
    V.push_back(s);
    insertString(s, sz(s) - 1);
  }
  doDP();
  ll cc = 0;
  for (string s : V) {
    int v = query(s, sz(s) - 1);
    // cerr << s << ": " << v << endl;
    cc += v - 1;
  }
  cout << cc << endl;
  return 0;
}