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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, n + 1)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define whole(x) (x).begin(), (x).end()
#define rwhole(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define P pair<int, int>
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define eb emplace_back
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

const int NMAX = 1000005;
bool isend[NMAX];
int child[NMAX][26];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<string> s(n);
  rep(i, n) cin >> s[i];
  rep(i, n) reverse(whole(s[i]));
  auto comp = [](string a, string b) {
    if (a.size() == b.size())
      return a < b;
    return a.size() < b.size();
  };
  sort(whole(s), comp);

  // TRIE
  int nxt = 1;
  vector<vector<int>> path(n);
  rep(i, n) {
    int node = 0;
    for (char c : s[i]) {
      path[i].pb(node);
      if (child[node][c - 'a'] == 0) {
        child[node][c - 'a'] = nxt++;
      }
      node = child[node][c - 'a'];
    }
    isend[node] = true;
  }

  ll ans = 0;

  rep(i, n) {
    set<char> suf;
    rrep(j, s[i].size()) {
      suf.insert(s[i][j]);
      int node = path[i][j];
      for (char c : suf) {
        if (isend[child[node][c - 'a']])
          ans++;
      }
    }
  }
  ans -= n;

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, n + 1)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define whole(x) (x).begin(), (x).end()
#define rwhole(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define P pair<int, int>
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define eb emplace_back
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

const int NMAX = 200005;
bool isend[NMAX];
int child[NMAX][26];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<string> s(n);
  rep(i, n) cin >> s[i];
  rep(i, n) reverse(whole(s[i]));
  auto comp = [](string a, string b) {
    if (a.size() == b.size())
      return a < b;
    return a.size() < b.size();
  };
  sort(whole(s), comp);

  // TRIE
  int nxt = 1;
  vector<vector<int>> path(n);
  rep(i, n) {
    int node = 0;
    for (char c : s[i]) {
      path[i].pb(node);
      if (child[node][c - 'a'] == 0) {
        child[node][c - 'a'] = nxt++;
      }
      node = child[node][c - 'a'];
    }
    isend[node] = true;
  }

  ll ans = 0;

  rep(i, n) {
    set<char> suf;
    rrep(j, s[i].size()) {
      suf.insert(s[i][j]);
      int node = path[i][j];
      for (char c : suf) {
        if (isend[child[node][c - 'a']])
          ans++;
      }
    }
  }
  ans -= n;

  cout << ans << endl;
  return 0;
}