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

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#if DEBUG && !ONLINE_JUDGE
ifstream input_from_file("long_input.txt");
#define cin input_from_file
#else

#endif

struct Vertex {
  //~ bool isLeaf;
  vector<Vertex *> child;
  vector<int> qq;

  Vertex() {
    //~ isLeaf = true;
    child.resize(26, nullptr);
    qq.resize(26, 0);
    //~ for (int k = 0; k < 26; k++) child[k] = nullptr, qq[k] = 0;
  }

  void make_child(int c) {
    if (child[c] != nullptr)
      return;
    child[c] = new Vertex();
    //~ if (!isLeaf) return;
    //~ isLeaf = false;
    //~ for (int k = 0; k < 26; k++) child[k] = new Vertex();
  }
};

LL res = 0;
Vertex *trie = new Vertex();

void insert(const string &s) {
  //~ cout << s << endl;
  Vertex *at = trie;
  vector<int> qq_tr(26, 0);
  for (int k = 0; k < 26; k++)
    qq_tr[k] = at->qq[k];
  for (int i = (int)s.size() - 1; i >= 0; i--) {
    int c = s[i] - 'a';
    //~ cout << s[i] << endl;
    if (i == 0)
      at->qq[c]++;
    res += qq_tr[c];
    qq_tr[c] = 0;
    at->make_child(c);
    at = at->child[c];
    //~ cout << "+ " << qq_tr[c] << endl;
    for (int k = 0; k < 26; k++)
      qq_tr[k] += at->qq[k];
  }
  //~ cout << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0); // Togliere nei problemi con query online

  int N;
  cin >> N;
  vector<string> S(N);
  for (int i = 0; i < N; i++)
    cin >> S[i];
  vector<pair<int, int>> oo;
  for (int i = 0; i < N; i++)
    oo.push_back({S[i].size(), i});
  sort(oo.begin(), oo.end());
  //~ for (string s : S) cout << s << endl;
  for (int i = 0; i < N; i++) {
    insert(S[oo[i].second]);
  }
  cout << res << "\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#if DEBUG && !ONLINE_JUDGE
ifstream input_from_file("input.txt");
#define cin input_from_file
#else

#endif

struct Vertex {
  bool isLeaf;
  Vertex *child[26];
  int qq[26];

  Vertex() {
    isLeaf = true;
    for (int k = 0; k < 26; k++)
      child[k] = nullptr, qq[k] = 0;
  }

  void make_child() {
    if (!isLeaf)
      return;
    isLeaf = false;
    for (int k = 0; k < 26; k++)
      child[k] = new Vertex();
  }
};

LL res = 0;
Vertex *trie = new Vertex();

void insert(string s) {
  //~ cout << s << endl;
  Vertex *at = trie;
  vector<int> qq_tr(26, 0);
  for (int k = 0; k < 26; k++)
    qq_tr[k] = at->qq[k];
  for (int i = (int)s.size() - 1; i >= 0; i--) {
    int c = s[i] - 'a';
    //~ cout << s[i] << endl;
    if (i == 0)
      at->qq[c]++;
    res += qq_tr[c];
    qq_tr[c] = 0;
    at->make_child();
    at = at->child[c];
    //~ cout << "+ " << qq_tr[c] << endl;
    for (int k = 0; k < 26; k++)
      qq_tr[k] += at->qq[k];
  }
  //~ cout << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0); // Togliere nei problemi con query online

  int N;
  cin >> N;
  vector<string> S(N);
  for (int i = 0; i < N; i++)
    cin >> S[i];
  vector<pair<int, int>> oo;
  for (int i = 0; i < N; i++)
    oo.push_back({S[i].size(), i});
  sort(oo.begin(), oo.end());
  //~ for (string s : S) cout << s << endl;
  for (int i = 0; i < N; i++) {
    insert(S[oo[i].second]);
  }
  cout << res << "\\n";
}