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

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
#define ll long long int
#define ld long double
#define key pair<ld, ld>
#define ii pair<int, int>
#define si set<int>
#define vii vector<pair<int, int>>
#define vi vector<int>
#define vll vector<ll>
#define vb vector<bool>
#define vvi vector<vector<int>>
#define vs vector<string>
#define all(v) v.begin(), v.end()
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define nu 100001
#define mul(x, y) ((ll)(x) * (y)) % mod
#define tr(c, i) for (auto i = (c).begin(); i != (c).end(); i++)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define forn(i, n) for (int i = 0; i < int(n); i++)
const ll INF = 9223372036854775807;
const ll mod = (ll)1e9 + 7;
#pragma GCC target("avx2")
// #pragma GCC optimization ("O3")
#pragma GCC optimization("unroll-loops")
// Naive Solution as follows...

int MOD(int a, int b) {
  if (a > b)
    return a - b;
  else
    return b - a;
}
ll max3(ll a, ll b, ll c) { return max(c, max(a, b)); }
ll min3(ll a, ll b, ll c) { return min(a, min(b, c)); }
ll power(ll x, ll y, ll mod1) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod1;
    y = y >> 1;
    x = (x * x) % mod1;
  }
  return res;
}
ll logg(ll a) {
  ll x = 0;
  while (a > 1) {
    x++;
    a /= 2;
  }
  return x;
}
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
ll abso(ll x) {
  if (x < 0) {
    return -x;
  }
  return x;
}
ll ceiling(ll a, ll b) {
  if (a % b == 0) {
    return a / b;
  } else {
    return a / b + 1;
  }
}
ll modinv(ll x, ll p) { return power(x, p - 2, p); }
struct point {
  ll x;
  ll y;
};
ll cross(point a, point b) { return a.x * b.y - b.x * a.y; }
bool ccw(point p1, point p2) {
  /* point n1;n1.x=p1.x-p0.x;n1.y=p1.y-p0.y;
   point n2;n2.x=p2.x-p0.x;n2.y=p2.y-p0.y;*/
  if (cross(p1, p2) > 0) {
    return true;
  } else {
    return false;
    // No need to worry about cross=0 case in this question;
  }
}
void ccw_sort(vector<point> &v) {
  vector<point> v1, v2;
  for (int i = 0; i < v.size(); i++) {
    if (v[i].y > 0) {
      v1.push_back(v[i]);
    } else {
      v2.push_back(v[i]);
    }
  }
  sort(all(v1), ccw);
  sort(all(v2), ccw);
  for (int i = 0; i < v2.size(); i++) {
    v1.push_back(v2[i]);
  }
  v = v1;
}
bool check(int x, int y, int n, int m) {
  if (x >= 0 && y >= 0 & x < n && y < m) {
    return true;
  } else {
    return false;
  }
}
struct node {
  node *children[26];
  int cnt[26];
};
node *create() {
  struct node *new_node = new struct node;
  for (int i = 0; i < 26; i++) {
    new_node->children[i] = NULL;
    new_node->cnt[i] = 0;
  }
  return new_node;
}
void insert(struct node *rt, string &ss, int i, vi &g) {
  if (i == (int)ss.size()) {
    return;
  } else {
    int x = ss[i] - 'a';
    if (rt->children[x] == NULL) {
      rt->children[x] = create();
      insert(rt->children[x], ss, i + 1, g);
      g[x]++;
      g[x] = min(1, g[x]);
      for (int i = 0; i < 26; i++) {
        rt->cnt[i] += g[i];
      }
    } else {
      insert(rt->children[x], ss, i + 1, g);
      g[x]++;
      g[x] = min(1, g[x]);
      for (int i = 0; i < 26; i++) {
        rt->cnt[i] += g[i];
      }
    }
  }
}
ll search(struct node *rt, string &ss, char x, int i) {
  if (i == ((int)ss.size())) {
    return rt->cnt[x - 'a'];
  } else {
    int xx = ss[i] - 'a';
    if (rt->children[xx] == NULL) {
      rt->children[xx] = create();
      return search(rt->children[xx], ss, x, i + 1);
    } else {
      return search(rt->children[xx], ss, x, i + 1);
    }
  }
}
int main() {
  fastio;
  int n;
  cin >> n;
  vector<pair<int, string>> g;
  for (int i = 0; i < n; i++) {
    string str;
    cin >> str;
    g.push_back(mp(str.size(), str));
  }
  sort(all(g));
  struct node *rt = create();
  ll ans = 0;
  for (int j = n - 1; j >= 0; j--) {
    vi cnt(26);
    string str = g[j].s;
    reverse(all(str));
    insert(rt, str, 0, cnt);
    char x = str.back();
    str.pop_back();
    ans = ans + search(rt, str, x, 0) - 1;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
#define ll long long int
#define ld long double
#define key pair<ld, ld>
#define ii pair<int, int>
#define si set<int>
#define vii vector<pair<int, int>>
#define vi vector<int>
#define vll vector<ll>
#define vb vector<bool>
#define vvi vector<vector<int>>
#define vs vector<string>
#define all(v) v.begin(), v.end()
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define nu 100001
#define mul(x, y) ((ll)(x) * (y)) % mod
#define tr(c, i) for (auto i = (c).begin(); i != (c).end(); i++)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define forn(i, n) for (int i = 0; i < int(n); i++)
const ll INF = 9223372036854775807;
const ll mod = (ll)1e9 + 7;
#pragma GCC target("avx2")
// #pragma GCC optimization ("O3")
#pragma GCC optimization("unroll-loops")
// Naive Solution as follows...

int MOD(int a, int b) {
  if (a > b)
    return a - b;
  else
    return b - a;
}
ll max3(ll a, ll b, ll c) { return max(c, max(a, b)); }
ll min3(ll a, ll b, ll c) { return min(a, min(b, c)); }
ll power(ll x, ll y, ll mod1) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod1;
    y = y >> 1;
    x = (x * x) % mod1;
  }
  return res;
}
ll logg(ll a) {
  ll x = 0;
  while (a > 1) {
    x++;
    a /= 2;
  }
  return x;
}
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
ll abso(ll x) {
  if (x < 0) {
    return -x;
  }
  return x;
}
ll ceiling(ll a, ll b) {
  if (a % b == 0) {
    return a / b;
  } else {
    return a / b + 1;
  }
}
ll modinv(ll x, ll p) { return power(x, p - 2, p); }
struct point {
  ll x;
  ll y;
};
ll cross(point a, point b) { return a.x * b.y - b.x * a.y; }
bool ccw(point p1, point p2) {
  /* point n1;n1.x=p1.x-p0.x;n1.y=p1.y-p0.y;
   point n2;n2.x=p2.x-p0.x;n2.y=p2.y-p0.y;*/
  if (cross(p1, p2) > 0) {
    return true;
  } else {
    return false;
    // No need to worry about cross=0 case in this question;
  }
}
void ccw_sort(vector<point> &v) {
  vector<point> v1, v2;
  for (int i = 0; i < v.size(); i++) {
    if (v[i].y > 0) {
      v1.push_back(v[i]);
    } else {
      v2.push_back(v[i]);
    }
  }
  sort(all(v1), ccw);
  sort(all(v2), ccw);
  for (int i = 0; i < v2.size(); i++) {
    v1.push_back(v2[i]);
  }
  v = v1;
}
bool check(int x, int y, int n, int m) {
  if (x >= 0 && y >= 0 & x < n && y < m) {
    return true;
  } else {
    return false;
  }
}
struct node {
  node *children[26];
  int cnt[26];
  ll rem = 0;
  ll depth = 0;
};
node *create() {
  struct node *new_node = new struct node;
  for (int i = 0; i < 26; i++) {
    new_node->children[i] = NULL;
    new_node->cnt[i] = 0;
  }
  new_node->rem = 0;
  new_node->depth = 0;
  return new_node;
}
void insert(struct node *rt, string &ss, int i, vi &g) {
  if (i == (int)ss.size()) {
    rt->rem++;
    return;
  } else {
    int x = ss[i] - 'a';
    if (rt->children[x] == NULL) {
      rt->children[x] = create();
      insert(rt->children[x], ss, i + 1, g);
      g[x]++;
      g[x] = min(1, g[x]);
      for (int i = 0; i < 26; i++) {
        rt->cnt[i] += g[i];
      }
    } else {
      insert(rt->children[x], ss, i + 1, g);
      g[x]++;
      g[x] = min(1, g[x]);
      for (int i = 0; i < 26; i++) {
        rt->cnt[i] += g[i];
      }
    }
  }
}
ll search(struct node *rt, string ss, char x, int i) {
  if (i == (int)ss.size()) {
    return rt->cnt[x - 'a'];
  } else {
    int xx = ss[i] - 'a';
    if (rt->children[xx] == NULL) {
      rt->children[xx] = create();
      return search(rt->children[xx], ss, x, i + 1);
    } else {
      return search(rt->children[xx], ss, x, i + 1);
    }
  }
}
void dfs1(struct node *rt, int d, int y) {
  rt->depth = d;
  for (int j = 0; j < 26; j++) {
    if (rt->children[j] != NULL) {
      dfs1(rt->children[j], d + 1, y);
    }
  }
}
ll dfs(struct node *rt, int k) {
  ll ans = 0;
  ll cnt = 0;
  for (int j = 0; j < 26; j++) {
    if (rt->children[j] != NULL) {
      ans = ans + dfs(rt->children[j], k);
      cnt = cnt + rt->children[j]->rem;
    }
  }
  cnt = cnt + rt->rem;
  rt->rem = cnt % k;
  ans = ans + (cnt / k) * (rt->depth);
  return ans;
}
int main() {
  fastio;
  int n;
  cin >> n;
  vector<pair<int, string>> g;
  for (int i = 0; i < n; i++) {
    string str;
    cin >> str;
    g.push_back(mp(str.size(), str));
  }
  sort(all(g));
  struct node *rt = create();
  ll ans = 0;
  for (int j = n - 1; j >= 0; j--) {
    vi cnt(26);
    string str = g[j].s;
    reverse(all(str));
    insert(rt, str, 0, cnt);
    char x = str.back();
    str.pop_back();
    ans = ans + search(rt, str, x, 0) - 1;
  }
  cout << ans << endl;
}