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

/*
Arthor : Ender_zzm
E-mail zzm_ender_wiggin@outlook.com
Blog ender-zzm.enbar.cn
*/
#include <bits/stdc++.h>
using namespace std;

inline int Isdigit(char c) {
  if (c < '0' || c > '9')
    return 0;
  return 1;
}
inline int readint() {
  int x = 0, flag = 1;
  char ch;
  while (!Isdigit(ch = getchar()))
    if (ch == '-')
      flag = -1;
  while (x = x * 10 + (ch & 15), Isdigit(ch = getchar()))
    ;
  return x * flag;
}

inline long long readlong() {
  long long x = 0, flag = 1;
  char ch;
  while (!Isdigit(ch = getchar()))
    if (ch == '-')
      flag = -1;
  while (x = x * 10 + (ch & 15), Isdigit(ch = getchar()))
    ;
  return x * flag;
}
template <class T> inline void write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}

template <class T> inline bool Chkmax(T &x, const T &y) {
  return x < y ? x = y, true : false;
}
template <class T> inline bool Chkmin(T &x, const T &y) {
  return x > y ? x = y, true : false;
}

#define For(i, x, y) for (int i = (x); i <= (y); i++)
#define Rep(i, x, y) for (int i = (x); i >= (y); i--)
#define ri readint
#define rl readlong

const int maxn = 2e5 + 100;
const int maxS = 1e6 + 30;
int n;
string s[maxn], rev[maxn];
int go[maxS][30], cnt;
int Size[maxS][30], fa[maxS];
int pd[maxS], js[maxn];
inline void insert(string s, int i) {
  int pp = 0;
  for (int i = 1; i <= 26; i++)
    pd[i] = -1;
  for (int i = s.size(); i >= 0; i--) {
    if (pd[s[i] - 'a' + 1] == -1) {
      pd[s[i] - 'a' + 1] = i;
    }
  }
  vector<int> pos;
  for (int i = 0; i < s.size(); i++) {
    if (go[pp][s[i] - 'a' + 1] == 0)
      go[pp][s[i] - 'a' + 1] = ++cnt, fa[cnt] = pp;
    if (pd[s[i] - 'a' + 1] == i)
      Size[pp][s[i] - 'a' + 1]++;
    pp = go[pp][s[i] - 'a' + 1];
  }
  js[i] = pp;
}
void dfs(int x) {
  for (int i = 1; i <= 26; i++) {
    if (go[x][i] == 0)
      continue;
    dfs(go[x][i]);
    for (int p = 1; p <= 26; p++) {
      Size[x][p] += Size[go[x][i]][p];
    }
  }
}
int main() {
  n = ri();
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    rev[i] = s[i];
    reverse(rev[i].begin(), rev[i].end());
    insert(rev[i], i);
  }
  dfs(0);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int tag = js[i];
    int faa = fa[tag];
    ans += Size[faa][s[i][0] - 'a' + 1] - 1;
  }
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

/*
Arthor : Ender_zzm
E-mail zzm_ender_wiggin@outlook.com
Blog ender-zzm.enbar.cn
*/
#include <bits/stdc++.h>
using namespace std;

inline int Isdigit(char c) {
  if (c < '0' || c > '9')
    return 0;
  return 1;
}
inline int readint() {
  int x = 0, flag = 1;
  char ch;
  while (!Isdigit(ch = getchar()))
    if (ch == '-')
      flag = -1;
  while (x = x * 10 + (ch & 15), Isdigit(ch = getchar()))
    ;
  return x * flag;
}

inline long long readlong() {
  long long x = 0, flag = 1;
  char ch;
  while (!Isdigit(ch = getchar()))
    if (ch == '-')
      flag = -1;
  while (x = x * 10 + (ch & 15), Isdigit(ch = getchar()))
    ;
  return x * flag;
}
template <class T> inline void write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}

template <class T> inline bool Chkmax(T &x, const T &y) {
  return x < y ? x = y, true : false;
}
template <class T> inline bool Chkmin(T &x, const T &y) {
  return x > y ? x = y, true : false;
}

#define For(i, x, y) for (int i = (x); i <= (y); i++)
#define Rep(i, x, y) for (int i = (x); i >= (y); i--)
#define ri readint
#define rl readlong

const int maxn = 2e5 + 100;
const int maxS = 1e6 + 30;
int n;
string s[maxn], rev[maxn];
int go[maxS][30], cnt;
int Size[maxS][30], fa[maxn];
int tag[maxn], pd[maxn], js[maxn];
inline void insert(string s, int i) {
  int pp = 0;
  for (int i = 1; i <= 26; i++)
    pd[i] = -1;
  for (int i = s.size(); i >= 0; i--) {
    if (pd[s[i] - 'a' + 1] == -1) {
      pd[s[i] - 'a' + 1] = i;
    }
  }
  vector<int> pos;
  for (int i = 0; i < s.size(); i++) {
    if (go[pp][s[i] - 'a' + 1] == 0)
      go[pp][s[i] - 'a' + 1] = ++cnt, fa[cnt] = pp;
    pp = go[pp][s[i] - 'a' + 1];
    if (pd[s[i] - 'a' + 1] == i)
      Size[pp][s[i] - 'a' + 1]++;
  }
  js[i] = pp;
}
void dfs(int x) {
  for (int i = 1; i <= 26; i++) {
    if (go[x][i] == 0)
      continue;
    dfs(go[x][i]);
    for (int p = 1; p <= 26; p++) {
      Size[x][p] += Size[go[x][i]][p];
    }
  }
}
int main() {
  n = ri();
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    rev[i] = s[i];
    reverse(rev[i].begin(), rev[i].end());
    insert(rev[i], i);
  }
  dfs(0);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int tag = js[i];
    int faa = fa[tag];
    ans += Size[faa][s[i][0] - 'a' + 1] - 1;
  }
  cout << ans << endl;
  return 0;
}