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

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#ifdef DEBUG
void debug() { cerr << "\\n"; }
template <class T> void debug(const T &x) { cerr << x << "\\n"; }
template <class T, class... Args> void debug(const T &x, const Args &...args) {
  cerr << x << " ";
  debug(args...);
}
template <class T> void debugVector(const vector<T> &v) {
  for (const T &x : v) {
    cerr << x << " ";
  }
  cerr << "\\n";
}
#else
template <class T, class... Args> void debug(const T &x, const Args &...args) {}
template <class T> void debugVector(const vector<T> &v) {}
#endif

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

struct Trie {
  const char base;
  struct Node {
    int child[26]; // 子の頂点番号 存在しなければ-1
    ll cnt[26];
    Node() {
      for (int i = 0; i < 26; i++) {
        child[i] = -1;
        cnt[i] = 0LL;
      }
    }
  };
  vector<Node> nodes;
  Trie(char base) : base(base) { nodes.emplace_back(Node()); }
  void insert(const string &s) {
    int now = 0;
    int n = (int)s.size();
    for (int i = 0; i < n - 1; i++) {
      int nxt = nodes[now].child[s[i] - base];
      if (nxt == -1) {
        nodes[now].child[s[i] - base] = (int)nodes.size();
        nxt = (int)nodes.size();
        nodes.emplace_back(Node());
      }
      now = nxt;
    }
    nodes[now].cnt[s.back() - base]++;
  }
  ll get(const string &s, vector<int> &exist) {
    ll res = 0;
    int now = 0;
    int n = (int)s.size();
    for (int i = 0; i < n; i++) {
      for (int k = 0; k < 26; k++) {
        if (exist[i] & (1 << k)) {
          res += nodes[now].cnt[k];
        }
      }
      int nxt = nodes[now].child[s[i] - base];
      if (nxt == -1) {
        return res;
      }
      now = nxt;
    }
    return res;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<string> s(N);
  for (int i = 0; i < N; i++) {
    cin >> s[i];
    reverse(ALL(s[i]));
  }
  sort(ALL(s),
       [](const string &a, const string &b) { return (a.size() < b.size()); });

  Trie trie('a');
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    int n = (int)s[i].size();
    vector<int> exist(n + 1, 0);
    for (int j = n - 1; j >= 0; j--) {
      exist[j] = exist[j + 1] | (1 << (s[i][j] - 'a'));
    }
    ans += trie.get(s[i], exist);
    trie.insert(s[i]);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#ifdef DEBUG
void debug() { cerr << "\\n"; }
template <class T> void debug(const T &x) { cerr << x << "\\n"; }
template <class T, class... Args> void debug(const T &x, const Args &...args) {
  cerr << x << " ";
  debug(args...);
}
template <class T> void debugVector(const vector<T> &v) {
  for (const T &x : v) {
    cerr << x << " ";
  }
  cerr << "\\n";
}
#else
template <class T, class... Args> void debug(const T &x, const Args &...args) {}
template <class T> void debugVector(const vector<T> &v) {}
#endif

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

struct Trie {
  const char base;
  struct Node {
    array<int, 26> child; // 子の頂点番号 存在しなければ-1
    array<ll, 26> cnt;
    Node() {
      for (int i = 0; i < 26; i++) {
        child[i] = -1;
        cnt[i] = 0;
      }
    }
  };
  vector<Node> nodes;
  Trie(char base) : base(base) { nodes.emplace_back(Node()); }
  void insert(const string &s) {
    int now = 0;
    int n = (int)s.size();
    for (int i = 0; i < n - 1; i++) {
      int &nxt = nodes[now].child[s[i] - base];
      if (nxt == -1) {
        nxt = (int)nodes.size();
        nodes.emplace_back(Node());
      }
      now = nxt;
    }
    nodes[now].cnt[s.back() - base]++;
  }
  ll get(const string &s, vector<int> &exist) {
    ll res = 0;
    int now = 0;
    int n = (int)s.size();
    for (int i = 0; i < n; i++) {
      for (int k = 0; k < 26; k++) {
        if (exist[i] & (1 << k)) {
          res += nodes[now].cnt[k];
        }
      }
      int &nxt = nodes[now].child[s[i] - base];
      if (nxt == -1) {
        return res;
      }
      now = nxt;
    }
    return res;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<string> s(N);
  for (int i = 0; i < N; i++) {
    cin >> s[i];
    reverse(ALL(s[i]));
  }
  sort(ALL(s),
       [](const string &a, const string &b) { return (a.size() < b.size()); });

  Trie trie('a');
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    int n = (int)s[i].size();
    vector<int> exist(n + 1, 0);
    for (int j = n - 1; j >= 0; j--) {
      exist[j] = exist[j + 1] | (1 << (s[i][j] - 'a'));
    }
    ans += trie.get(s[i], exist);
    trie.insert(s[i]);
  }
  cout << ans << endl;
}