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

using i64 = long long;
#define _overload(_1, _2, _3, _4, name, ...) name
#define _rep1(Itr, N) _rep3(Itr, 0, N, 1)
#define _rep2(Itr, a, b) _rep3(Itr, a, b, 1)
#define _rep3(Itr, a, b, step) for (i64(Itr) = a; (Itr) < b; (Itr) += step)
#define repeat(...) _overload(__VA_ARGS__, _rep3, _rep2, _rep1)(__VA_ARGS__)

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;
using llong = long long;

struct Trie {
  using State = string::iterator;
  struct Node {
    Node *ch[26] = {};
    int cnt[26] = {};
    int s = 0;
  };

  Node *root = nullptr;
  Trie() { root = new Node(); };

  void insert(string &s, int cnt[]) {
    root->s++;
    repeat(i, 26) {
      if (cnt[i])
        root->cnt[i]++;
    }

    if (root->ch[s[0] - 'a'] == nullptr)
      root->ch[s[0] - 'a'] = new Node();
    insert(root->ch[s[0] - 'a'], s.begin(), cnt);
  };

  void insert(Node *u, State s, int cnt[]) {
    cnt[*s - 'a']--;
    u->s++;
    repeat(i, 26) {
      if (cnt[i])
        u->cnt[i]++;
    }

    s++;
    if (*s == '\\0')
      return;
    if (u->ch[*s - 'a'] == nullptr)
      u->ch[*s - 'a'] = new Node();
    insert(u->ch[*s - 'a'], s, cnt);
  };

  int find(string &s) {
    if (s.size() == 1) {
      return root->cnt[s[0] - 'a'];
    } else {
      return find(root->ch[s[0] - 'a'], s.begin());
    }
  };
  int find(Node *u, State s) {
    if (u == nullptr)
      return 0;

    ++s;
    if (s[1] == '\\0') {
      return u->cnt[*s - 'a'];
    } else {
      return find(u->ch[*s - 'a'], s);
    }
  };
};

llong n;
vector<string> s;
Trie tr;

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

  cin >> n;
  s.resize(n);
  for (auto &vs : s)
    cin >> vs;

  repeat(i, n) {
    int cnt[26] = {};
    reverse(s[i].begin(), s[i].end());
    for (auto ch : s[i]) {
      cnt[ch - 'a']++;
    }
    tr.insert(s[i], cnt);
  }

  llong ans = 0;
  repeat(i, n) { ans += tr.find(s[i]) - 1; }

  cout << ans << '\\n';

  return 0;
};


Error: 0

Fixed submission:


using i64 = long long;
#define _overload(_1, _2, _3, _4, name, ...) name
#define _rep1(Itr, N) _rep3(Itr, 0, N, 1)
#define _rep2(Itr, a, b) _rep3(Itr, a, b, 1)
#define _rep3(Itr, a, b, step) for (i64(Itr) = a; (Itr) < b; (Itr) += step)
#define repeat(...) _overload(__VA_ARGS__, _rep3, _rep2, _rep1)(__VA_ARGS__)

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;
using llong = long long;

struct Trie {
  using State = string::iterator;
  struct Node {
    Node *ch[26] = {};
    int cnt[26] = {};
    int s = 0;
  };

  Node *root = nullptr;

  void insert(string &s, int cnt[]) {
    if (root == nullptr)
      root = new Node();
    root->s++;
    repeat(i, 26) {
      if (cnt[i])
        root->cnt[i]++;
    }

    if (root->ch[s[0] - 'a'] == nullptr)
      root->ch[s[0] - 'a'] = new Node();
    insert(root->ch[s[0] - 'a'], s.begin(), cnt);
  };

  void insert(Node *u, State s, int cnt[]) {
    cnt[*s - 'a']--;
    u->s++;
    repeat(i, 26) {
      if (cnt[i])
        u->cnt[i]++;
    }

    s++;
    if (*s == '\\0')
      return;
    if (u->ch[*s - 'a'] == nullptr)
      u->ch[*s - 'a'] = new Node();
    insert(u->ch[*s - 'a'], s, cnt);
  };

  int find(string &s) {
    if (s.size() == 1) {
      return root->cnt[s[0] - 'a'];
    } else {
      return find(root->ch[s[0] - 'a'], s.begin());
    }
  };
  int find(Node *u, State s) {
    if (u == nullptr)
      return 0;

    ++s;
    if (s[1] == '\\0') {
      return u->cnt[*s - 'a'];
    } else {
      return find(u->ch[*s - 'a'], s);
    }
  };
};

llong n;
vector<string> s;
Trie tr;

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

  cin >> n;
  s.resize(n);
  for (auto &vs : s)
    cin >> vs;

  repeat(i, n) {
    int cnt[26] = {};
    reverse(s[i].begin(), s[i].end());
    for (auto ch : s[i]) {
      cnt[i]++;
    }
    tr.insert(s[i], cnt);
  }

  llong ans = 0;
  repeat(i, n) { ans += tr.find(s[i]); }

  cout << ans << '\\n';

  return 0;
};