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

#ifdef __APPLE__
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <immintrin.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#else
#include <bits/stdc++.h>
#endif

#define F first
#define S second
#define MP make_pair
#define PB push_back

#define all(a) a.begin(), a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

const int max_n = 1e6 + 10, inf = 1000111222;

struct node {
  array<int, 26> go;
  array<int, 26> cnt;

  node() {
    for (int i = 0; i < 26; i++) {
      go[i] = 0;
      cnt[i] = 0;
    }
  }
};

int root = 1;
int last_used_node = 1;
node a[max_n];

int go_bor(string t) {
  int cur = root;
  for (auto i : t) {
    if (!a[cur].go[i - 'a']) {
      return -1;
    }
    cur = a[cur].go[i - 'a'];
  }
  return cur;
}

void add_bor(string s) {
  array<int, 26> cur_cnt;
  for (int j = 0; j < 26; j++) {
    cur_cnt[j] = 0;
  }
  for (auto i : s) {
    cur_cnt[i - 'a']++;
  }

  int cur = root;
  for (auto i : s) {
    /// add this
    for (int j = 0; j < 26; j++) {
      if (cur_cnt[j]) {
        //                cout<<"kek :: "<<cur<<" "<<j<<"\\n";
        a[cur].cnt[j]++;
      }
    }
    cur_cnt[i - 'a']--;
    if (!a[cur].go[i - 'a']) {
      a[cur].go[i - 'a'] = ++last_used_node;
    }
    cur = a[cur].go[i - 'a'];
  }
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n;
  cin >> n;
  vector<string> kek(n);
  for (int i = 0; i < n; i++) {
    cin >> kek[i];
  }

  ll ans = 0;
  for (int it = 0; it < 2; it++) {
    for (auto i : kek) {
      string s = i;
      reverse(all(s));

      string t = s;
      int last_char = t.back();
      t.pop_back();
      int cur = go_bor(t);
      if (cur != -1) {
        //                cout<<"lol :: "<<(last_char-'a')<<"
        //                "<<a[cur].cnt[last_char-'a']<<"\\n";
        ans += a[cur].cnt[last_char - 'a'];
      }
      add_bor(s);
    }
    for (int i = 0; i < max_n; i++) {
      a[i] = *new node();
    }
    last_used_node = 1;
    reverse(all(kek));
  }

  map<string, int> kek_cnt;
  for (auto i : kek) {
    kek_cnt[i]++;
  }
  for (auto i : kek_cnt) {
    ans -= 1ll * i.sec * (i.sec - 1) / 2;
  }

  cout << ans << "\\n";
}


Error: -11

Fixed submission:

#ifdef __APPLE__
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <immintrin.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#else
#include <bits/stdc++.h>
#endif

#define F first
#define S second
#define MP make_pair
#define PB push_back

#define all(a) a.begin(), a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

const int max_n = 1e6 + 10, inf = 1000111222;

struct node {
  array<int, 26> go;
  array<int, 26> cnt;

  node() {
    for (int i = 0; i < 26; i++) {
      go[i] = 0;
      cnt[i] = 0;
    }
  }
};

int root = 1;
int last_used_node = 1;
node a[max_n];

int go_bor(string t) {
  int cur = root;
  for (auto i : t) {
    if (!a[cur].go[i - 'a']) {
      return -1;
    }
    cur = a[cur].go[i - 'a'];
  }
  return cur;
}

void add_bor(string s) {
  array<int, 26> cur_cnt;
  for (int j = 0; j < 26; j++) {
    cur_cnt[j] = 0;
  }
  for (auto i : s) {
    cur_cnt[i - 'a']++;
  }

  int cur = root;
  for (auto i : s) {
    /// add this
    for (int j = 0; j < 26; j++) {
      if (cur_cnt[j]) {
        //                cout<<"kek :: "<<cur<<" "<<j<<"\\n";
        a[cur].cnt[j]++;
      }
    }
    cur_cnt[i - 'a']--;
    if (!a[cur].go[i - 'a']) {
      a[cur].go[i - 'a'] = ++last_used_node;
    }
    cur = a[cur].go[i - 'a'];
  }
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n;
  cin >> n;
  vector<string> kek(n);
  for (int i = 0; i < n; i++) {
    cin >> kek[i];
  }

  ll ans = 0;
  for (int it = 0; it < 2; it++) {
    for (auto i : kek) {
      string s = i;
      reverse(all(s));

      string t = s;
      int last_char = t.back();
      t.pop_back();
      int cur = go_bor(t);
      if (cur != -1) {
        //                cout<<"lol :: "<<(last_char-'a')<<"
        //                "<<a[cur].cnt[last_char-'a']<<"\\n";
        ans += a[cur].cnt[last_char - 'a'];
      }
      add_bor(s);
    }
    for (int i = 0; i < max_n; i++) {
      a[i] = *new node();
    }
    reverse(all(kek));
  }

  map<string, int> kek_cnt;
  for (auto i : kek) {
    kek_cnt[i]++;
  }
  for (auto i : kek_cnt) {
    ans -= 1ll * i.sec * (i.sec - 1) / 2;
  }

  cout << ans << "\\n";
}