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

// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx2")
//  293206GT
#include <algorithm>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

typedef long long ll;
#define int ll
using namespace std;
#define put(a) cout << (a) << '\\n'
#define sqr(x) (x) * (x)
typedef pair<int, int> pii;
typedef long double ld;
typedef pair<ld, ld> pld;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef vector<pii> vpii;
#define rep(x, y, a) for (int x = (y); x < (int)(a); ++x)
#define all(a) a.begin(), a.end()
#define chkmax(a, b) a = max(a, (b))
#define chkmin(a, b) a = min(a, (b))

namespace IO {

template <class A, class B>
ostream &operator<<(ostream &out, vector<pair<A, B>> a);

template <class A> ostream &operator<<(ostream &out, vector<A> a);

template <class A, class B> ostream &operator<<(ostream &out, pair<A, B> a) {
  out << a.first << " " << a.second;
  return out;
}

template <class A, class B>
ostream &operator<<(ostream &out, vector<pair<A, B>> a) {
  for (pair<A, B> x : a)
    out << x.first << " " << x.second << '\\n';
  return out;
}

template <class A> ostream &operator<<(ostream &out, vector<A> a) {
  for (A x : a)
    out << x << ' ';
  return out;
}

template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) {
  in >> a.first >> a.second;
  return in;
}

template <class A> istream &operator>>(istream &in, vector<A> &a) {
  for (A &x : a)
    in >> x;
  return in;
}
} // namespace IO
using namespace IO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

struct Node {
  vector<int> next;
  int is_term;
  Node() {
    next.resize(26);
    next.assign(26, -1);
    is_term = 0;
  }
};
const int N = 1e6 + 3e5;
// const int N = 13;
vector<Node> trie(N);
int sz = 0;
void add(string s) {
  int v = 0;
  for (char c2 : s) {
    char c = c2 - 'a';
    if (trie[v].next[c] == -1) {
      trie[v].next[c] = ++sz;
    }
    v = trie[v].next[c];
  }
  ++trie[v].is_term;
}
void solve() {
  int n;
  cin >> n;
  vector<string> s(n);
  cin >> s;
  rep(i, 0, n) {
    reverse(all(s[i]));
    add(s[i]);
  }
  int ans = 0;
  rep(i, 0, n) {
    int v = 0;
    vi cnt(26);
    rep(j, 0, s[i].size())++ cnt[s[i][j] - 'a'];
    rep(j, 0, s[i].size()) {
      rep(k, 0, 26) {
        if (trie[v].next[k] != -1 && cnt[k])
          ans += trie[trie[v].next[k]].is_term;
      }
      v = trie[v].next[s[i][j] - 'a'];
      --cnt[s[i][j] - 'a'];
    }
  }
  put(ans - n);
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(30);
  //    freopen("input.txt", "r", stdin);
  int q = 1;
  //    cin >> q;

  while (q--)
    solve();
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx2")
//  293206GT
#include <algorithm>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

typedef long long ll;
#define int ll
using namespace std;
#define put(a) cout << (a) << '\\n'
#define sqr(x) (x) * (x)
typedef pair<int, int> pii;
typedef long double ld;
typedef pair<ld, ld> pld;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef vector<pii> vpii;
#define rep(x, y, a) for (int x = (y); x < (int)(a); ++x)
#define all(a) a.begin(), a.end()
#define chkmax(a, b) a = max(a, (b))
#define chkmin(a, b) a = min(a, (b))

namespace IO {

template <class A, class B>
ostream &operator<<(ostream &out, vector<pair<A, B>> a);

template <class A> ostream &operator<<(ostream &out, vector<A> a);

template <class A, class B> ostream &operator<<(ostream &out, pair<A, B> a) {
  out << a.first << " " << a.second;
  return out;
}

template <class A, class B>
ostream &operator<<(ostream &out, vector<pair<A, B>> a) {
  for (pair<A, B> x : a)
    out << x.first << " " << x.second << '\\n';
  return out;
}

template <class A> ostream &operator<<(ostream &out, vector<A> a) {
  for (A x : a)
    out << x << ' ';
  return out;
}

template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) {
  in >> a.first >> a.second;
  return in;
}

template <class A> istream &operator>>(istream &in, vector<A> &a) {
  for (A &x : a)
    in >> x;
  return in;
}
} // namespace IO
using namespace IO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

struct Node {
  vector<int> next;
  int is_term;
  Node() {
    next.resize(26);
    next.assign(26, -1);
    is_term = 0;
  }
};
const int N = 1e6 + 3e5;
// const int N = 13;
vector<Node> trie(N);
int sz = 0;
void add(string s) {
  int v = 0;
  for (char c2 : s) {
    char c = c2 - 'a';
    if (trie[v].next[c] == -1) {
      trie[v].next[c] = ++sz;
    }
    v = trie[v].next[c];
  }
  ++trie[v].is_term;
}
void solve() {
  int n;
  cin >> n;
  vector<string> s(n);
  cin >> s;
  rep(i, 0, n) {
    reverse(all(s[i]));
    add(s[i]);
  }
  int ans = 0;
  rep(i, 0, n) {
    int v = 0;
    vi cnt(26);
    rep(j, 0, s[i].size())++ cnt[s[i][j] - 'a'];
    rep(j, 0, s[i].size()) {
      rep(k, 0, 26) {
        if (trie[v].next[k] != -1 && cnt[k])
          ans += trie[trie[v].next[k]].is_term;
      }
      v = trie[v].next[s[i][j] - 'a'];
      --cnt[s[i][j]];
    }
  }
  put(ans - n);
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(30);
  //    freopen("input.txt", "r", stdin);
  int q = 1;
  //    cin >> q;

  while (q--)
    solve();
}