Problem p02589 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fir first
#define sec second
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrp(i, a, b) for (int i = (a); i >= (b); --i)
#define gc() getchar()
template <typename tp> inline void read(tp &x) {
  x = 0;
  char tmp;
  bool key = 0;
  for (tmp = gc(); !isdigit(tmp); tmp = gc())
    key = (tmp == '-');
  for (; isdigit(tmp); tmp = gc())
    x = (x << 3) + (x << 1) + (tmp ^ '0');
  if (key)
    x = -x;
}
template <typename tp> inline void ckmn(tp &x, tp y) { x = x < y ? x : y; }
template <typename tp> inline void ckmx(tp &x, tp y) { x = x < y ? y : x; }
typedef double db;

const int N = 200010;
int n;
string str[N];
int ch[N * 4][26], cnt = 1, sz[N * 4][26], num[N * 4];
void ins(string s) {
  int p = 1;
  int hav[26];
  rep(i, 0, 25) hav[i] = 0;
  rep(i, 0, (int)s.size() - 1) hav[s[i] - 'a']++;
  rep(i, 0, (int)s.size() - 1) {
    rep(j, 0, 25) sz[p][j] += (hav[j] > 0);
    num[p]++;
    if (!ch[p][s[i] - 'a'])
      ch[p][s[i] - 'a'] = ++cnt;
    p = ch[p][s[i] - 'a'];
    --hav[s[i] - 'a'];
  }
  ++num[p];
}
ll ans = 0;
void doit(string s) {
  int p = 1;
  rep(i, 0, (int)s.size()) {
    if (i + 1 == (int)s.size()) {
      int c = s[i] - 'a';
      rep(a, 0, 25) if (a != c) { ans += sz[ch[p][a]][c]; }
    }
    if (i == (int)s.size()) {
      ans += num[p];
      break;
    }
    p = ch[p][s[i] - 'a'];
  }
}
int main() {
  read(n);
  rep(i, 1, n) cin >> str[i];
  rep(i, 1, n) reverse(str[i].begin(), str[i].end());
  sort(str + 1, str + n + 1,
       [=](string a, string b) -> bool { return a.size() > b.size(); });
  rep(i, 1, n) {
    doit(str[i]);
    ins(str[i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fir first
#define sec second
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrp(i, a, b) for (int i = (a); i >= (b); --i)
#define gc() getchar()
template <typename tp> inline void read(tp &x) {
  x = 0;
  char tmp;
  bool key = 0;
  for (tmp = gc(); !isdigit(tmp); tmp = gc())
    key = (tmp == '-');
  for (; isdigit(tmp); tmp = gc())
    x = (x << 3) + (x << 1) + (tmp ^ '0');
  if (key)
    x = -x;
}
template <typename tp> inline void ckmn(tp &x, tp y) { x = x < y ? x : y; }
template <typename tp> inline void ckmx(tp &x, tp y) { x = x < y ? y : x; }
typedef double db;

const int N = 200010;
int n;
string str[N];
int ch[N * 5][26], cnt = 1, sz[N * 5][26], num[N * 5];
void ins(string s) {
  int p = 1;
  int hav[26];
  rep(i, 0, 25) hav[i] = 0;
  rep(i, 0, (int)s.size() - 1) hav[s[i] - 'a']++;
  rep(i, 0, (int)s.size() - 1) {
    rep(j, 0, 25) sz[p][j] += (hav[j] > 0);
    num[p]++;
    if (!ch[p][s[i] - 'a'])
      ch[p][s[i] - 'a'] = ++cnt;
    p = ch[p][s[i] - 'a'];
    --hav[s[i] - 'a'];
  }
  ++num[p];
}
ll ans = 0;
void doit(string s) {
  int p = 1;
  rep(i, 0, (int)s.size()) {
    if (i + 1 == (int)s.size()) {
      int c = s[i] - 'a';
      rep(a, 0, 25) if (a != c) { ans += sz[ch[p][a]][c]; }
    }
    if (i == (int)s.size()) {
      ans += num[p];
      break;
    }
    p = ch[p][s[i] - 'a'];
  }
}
int main() {
  read(n);
  rep(i, 1, n) cin >> str[i];
  rep(i, 1, n) reverse(str[i].begin(), str[i].end());
  sort(str + 1, str + n + 1,
       [=](string a, string b) -> bool { return a.size() > b.size(); });
  rep(i, 1, n) {
    doit(str[i]);
    ins(str[i]);
  }
  cout << ans << endl;
  return 0;
}