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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<long long> vl;
typedef vector<vector<ll>> vll;
typedef vector<bool> vb;
typedef set<int> si;
typedef queue<int> qi;
typedef stack<int> sti;
typedef priority_queue<int> pqi;
#define fori(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, b, a) for (int i = (b); i >= (a); --i)
#define all(a) (a).begin(), (a).end()
// #define __builtin_popcountll bitCountLL
// #define __builtin_popcount bitCount
//__builtin_ctz 末尾0的个数, __builtin_clz 前导0的个数
//__builtin_ffs 最后一个1的位置
#define maxn 2000005
#define Mod 1000000007
const int INF = 2e9;
const ll ML = 4e18;

struct trieNode {
  int val;
  int sum;
  bool isWord;
  int next[26], count[26];
  trieNode() {
    memset(next, -1, sizeof(next));
    memset(count, 0, sizeof(count));
    sum = val = 0;
    isWord = false;
  }
} N[maxn * 2];
int cnt;
void insert(string word) {
  if (!word.size())
    return;
  int cur = 0;
  fori(i, 0, word.size()) {
    int v = word[i] - 'a';
    if (N[cur].next[v] == -1) {
      N[cur].next[v] = cnt++;
    }
    cur = N[cur].next[v];
    N[cur].val = v;
  }
  N[cur].isWord = true;
}
ll res;
void dfs(int i) {
  fori(k, 0, 26) {
    int cur = N[i].next[k];
    if (cur != -1) {
      dfs(cur);
      N[i].sum += N[cur].sum;
      fori(j, 0, 26) { N[i].count[j] += N[cur].count[j]; }
    }
  }
  fori(k, 0, 26) {
    int cur = N[i].next[k];
    if (cur != -1 && N[cur].isWord) {
      res += N[i].count[k] - N[cur].count[k];
    }
  }
  if (N[i].isWord) {
    res += N[i].sum;
    N[i].sum++;
  }
  N[i].count[N[i].val] = N[i].sum;
}

void solve() {
  int n;
  cin >> n;
  vector<string> a(n);
  cnt = 1;
  fori(i, 0, n) {
    cin >> a[i];
    reverse(all(a[i]));
    insert(a[i]);
  }
  res = 0;
  dfs(0);
  cout << res << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // cout.precision(30); cout << fixed;
  //	freopen("D:\\\\data_in.txt", "r", stdin);

  solve();

  /*
  clock_t start,finish;
  start = clock();

  finish = clock();
  cout << "\\n" << (double)(finish - start) / (double)(CLOCKS_PER_SEC) * 1000 <<
  "ms" << endl;
  */
  return 0;
}

/***************************************************************
 question:
 idea:
 tag:
 record:
 ***************************************************************/


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<long long> vl;
typedef vector<vector<ll>> vll;
typedef vector<bool> vb;
typedef set<int> si;
typedef queue<int> qi;
typedef stack<int> sti;
typedef priority_queue<int> pqi;
#define fori(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, b, a) for (int i = (b); i >= (a); --i)
#define all(a) (a).begin(), (a).end()
// #define __builtin_popcountll bitCountLL
// #define __builtin_popcount bitCount
//__builtin_ctz 末尾0的个数, __builtin_clz 前导0的个数
//__builtin_ffs 最后一个1的位置
#define maxn 200005
#define Mod 1000000007
const int INF = 2e9;
const ll ML = 4e18;

struct trieNode {
  int val;
  int sum;
  bool isWord;
  int next[26], count[26];
  trieNode() {
    memset(next, -1, sizeof(next));
    memset(count, 0, sizeof(count));
    sum = val = 0;
    isWord = false;
  }
} N[maxn * 2];
int cnt;
void insert(string word) {
  if (!word.size())
    return;
  int cur = 0;
  fori(i, 0, word.size()) {
    int v = word[i] - 'a';
    if (N[cur].next[v] == -1) {
      N[cur].next[v] = cnt++;
    }
    cur = N[cur].next[v];
    N[cur].val = v;
  }
  N[cur].isWord = true;
}
ll res;
void dfs(int i) {
  fori(k, 0, 26) {
    int cur = N[i].next[k];
    if (cur != -1) {
      dfs(cur);
      N[i].sum += N[cur].sum;
      fori(j, 0, 26) { N[i].count[j] += N[cur].count[j]; }
    }
  }
  fori(k, 0, 26) {
    int cur = N[i].next[k];
    if (cur != -1 && N[cur].isWord) {
      res += N[i].count[k] - N[cur].count[k];
    }
  }
  if (N[i].isWord) {
    res += N[i].sum;
    N[i].sum++;
  }
  N[i].count[N[i].val] = N[i].sum;
}

void solve() {
  int n;
  cin >> n;
  vector<string> a(n);
  cnt = 1;
  fori(i, 0, n) {
    cin >> a[i];
    reverse(all(a[i]));
    insert(a[i]);
  }
  res = 0;
  dfs(0);
  cout << res << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // cout.precision(30); cout << fixed;
  // freopen("D:\\\\data_in.txt", "r", stdin);

  solve();

  /*
  clock_t start,finish;
  start = clock();

  finish = clock();
  cout << "\\n" << (double)(finish - start) / (double)(CLOCKS_PER_SEC) * 1000 <<
  "ms" << endl;
  */
  return 0;
}

/***************************************************************
 question:
 idea:
 tag:
 record:
 ***************************************************************/