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

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define ff first
#define ss second
#define pii pair<int, int>
#define pb emplace_back
#define pf emplace_front
#define mp make_pair
#define ld long double
#define all(x) x.begin(), x.end()
#define uniq(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())

const int maxn = 1e6 + 9;
int g[maxn][26];
int dp[maxn][26];
int cnt[maxn];
string s[maxn];
int n, cnt_v;
bool used[maxn];

void add_ver() {
  for (int j = 0; j < 26; j++)
    g[cnt_v][j] = -1;
  cnt_v++;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  add_ver();
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    reverse(all(s[i]));
    int v = 0;
    vector<int> cur;
    cur.pb(v);
    for (int e = 0; e < (int)s[i].size(); e++) {
      if (g[v][s[i][e] - 'a'] == -1) {
        g[v][s[i][e] - 'a'] = cnt_v;
        add_ver();
      }
      v = g[v][s[i][e] - 'a'];
      cur.pb(v);
    }
    cnt[v]++;
    for (int j = 0; j < 26; j++)
      used[j] = 0;
    for (int e = (int)s[i].size() - 1; e >= 0; e--) {
      if (!used[s[i][e] - 'a']) {
        dp[cur[e]][s[i][e] - 'a']++;
        used[s[i][e] - 'a'] = 1;
      }
    }
  }
  for (int v = cnt_v - 1; v >= 0; v--)
    for (int j = 0; j < 26; j++)
      for (int e = 0; e < 26; e++)
        dp[v][e] += (g[v][j] >= 0 ? dp[g[v][j]][e] : 0);
  int ans = 0;
  for (int v = 0; v < cnt_v; v++)
    for (int j = 0; j < 26; j++)
      ans += (g[v][j] >= 0 ? cnt[g[v][j]] * (dp[v][j] - cnt[g[v][j]]) : 0);
  for (int i = 0; i < cnt_v; i++)
    ans += cnt[i] * (cnt[i] - 1) / 2;
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define ff first
#define ss second
#define pii pair<int, int>
#define pb emplace_back
#define pf emplace_front
#define mp make_pair
#define ld long double
#define all(x) x.begin(), x.end()
#define uniq(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())

const int maxn = 2e5 + 9;
int g[maxn][26];
int dp[maxn][26];
int cnt[maxn];
string s[maxn];
int n, cnt_v;
bool used[maxn];

void add_ver() {
  for (int j = 0; j < 26; j++)
    g[cnt_v][j] = -1;
  cnt_v++;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  add_ver();
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    reverse(all(s[i]));
    int v = 0;
    vector<int> cur;
    cur.pb(v);
    for (int e = 0; e < (int)s[i].size(); e++) {
      if (g[v][s[i][e] - 'a'] == -1) {
        g[v][s[i][e] - 'a'] = cnt_v;
        add_ver();
      }
      v = g[v][s[i][e] - 'a'];
      cur.pb(v);
    }
    cnt[v]++;
    for (int j = 0; j < 26; j++)
      used[j] = 0;
    for (int e = (int)s[i].size() - 1; e >= 0; e--) {
      if (!used[s[i][e] - 'a']) {
        dp[cur[e]][s[i][e] - 'a']++;
        used[s[i][e] - 'a'] = 1;
      }
    }
  }
  for (int v = cnt_v - 1; v >= 0; v--)
    for (int j = 0; j < 26; j++)
      for (int e = 0; e < 26; e++)
        dp[v][e] += (g[v][j] >= 0 ? dp[g[v][j]][e] : 0);
  int ans = 0;
  for (int v = 0; v < cnt_v; v++)
    for (int j = 0; j < 26; j++)
      ans += (g[v][j] >= 0 ? cnt[g[v][j]] * (dp[v][j] - cnt[g[v][j]]) : 0);
  for (int i = 0; i < cnt_v; i++)
    ans += cnt[i] * (cnt[i] - 1) / 2;
  cout << ans;
}