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

#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define pb push_back
#define mp make_pair
#define clr(v, d) memset(v, d, sizeof(v))
#define PI acos(-1)
typedef long long ll;
typedef unsigned long long ull;
const double eps = (1e-9);
using namespace std;
int dcmp(long double a, long double b) {
  return fabsl(a - b) <= eps ? 0 : (a > b) ? 1 : -1;
}
int getBit(ll num, int idx) { return ((num >> idx) & 1ll) == 1; }
int setBit1(int num, int idx) { return num | (1 << idx); }
ll setBit0(ll num, int idx) { return num & ~(1ll << idx); }
ll flipBit(ll num, int idx) { return num ^ (1ll << idx); }
void GO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int countNumBit1(int mask) {
  int ret = 0;
  while (mask) {
    mask &= (mask - 1);
    ++ret;
  }
  return ret;
}
const int N = 200000 + 5, M = 1000000 + 5;
string s[N];
int ans = 0;
int vis[M][100], tree[M][30], cnt[M][30];
int rng(int f, int t, int ch) {
  if (t < f)
    return 0;
  return vis[t][ch] - (f ? vis[f - 1][ch] : 0);
}
int id = 1;
void insertt(string word, int chh) {

  int node = 1;

  for (int i = 0; i < sz(word); i++) {
    int ch = word[i] - 'a';
    if (tree[node][ch] == 0) {
      tree[node][ch] = ++id;
    }
    node = tree[node][ch];
  }
  cnt[node][chh]++;
}
void query(string word, int cid, int ex) {

  int node = 1;
  for (int j = 0; j < 26; j++) {
    if (rng(0, cid, j) == 0 || (cid == 0 && j == ex))
      continue;
    ans += cnt[node][j];
  }

  for (int i = 0; i < sz(word); i++) {
    int ch = word[i] - 'a';
    node = tree[node][ch];
    cid--;
    for (int j = 0; j < 26; j++) {
      if (rng(0, cid, j) == 0 || (cid == 0 && j == ex))
        continue;
      ans += cnt[node][j];
    }
  }
}

int main() {
  GO();
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    string ss = "";
    int siz = sz(s[i]);
    for (int j = siz - 1; j >= 1; j--)
      ss += s[i][j];
    insertt(ss, s[i][0] - 'a');
  }

  for (int i = 0; i < n; i++) {

    int siz = sz(s[i]);
    string ss = "";
    for (int j = 0; j < siz; j++) {
      for (int k = 0; k < 26; k++)
        vis[j][k] = (j - 1 >= 0 ? vis[j - 1][k] : 0);
      vis[j][s[i][j] - 'a']++;
    }
    for (int j = siz - 1; j >= 1; j--)
      ss += s[i][j];
    query(ss, siz - 1, s[i][0] - 'a');
  }

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


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define pb push_back
#define mp make_pair
#define clr(v, d) memset(v, d, sizeof(v))
#define PI acos(-1)
typedef long long ll;
typedef unsigned long long ull;
const double eps = (1e-9);
using namespace std;
int dcmp(long double a, long double b) {
  return fabsl(a - b) <= eps ? 0 : (a > b) ? 1 : -1;
}
int getBit(ll num, int idx) { return ((num >> idx) & 1ll) == 1; }
int setBit1(int num, int idx) { return num | (1 << idx); }
ll setBit0(ll num, int idx) { return num & ~(1ll << idx); }
ll flipBit(ll num, int idx) { return num ^ (1ll << idx); }
void GO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int countNumBit1(int mask) {
  int ret = 0;
  while (mask) {
    mask &= (mask - 1);
    ++ret;
  }
  return ret;
}
const int N = 200000 + 5, M = 1000000 + 5;
string s[N];
int ans = 0;
int vis[M][100];
int rng(int f, int t, int ch) {
  if (t < f)
    return 0;
  return vis[t][ch] - (f ? vis[f - 1][ch] : 0);
}
struct trie {

  trie *child[30];
  vector<int> ch;
  trie() { clr(child, 0); }
};
trie *getNode() {
  trie *pNode = new trie();
  vector<int> vv;
  pNode->ch = vv;

  for (int i = 0; i < 30; i++)
    pNode->child[i] = NULL;

  return pNode;
}
void insertt(trie *root, string key, int chh) {

  struct trie *tmp = root;
  for (int i = 0; i < sz(key); i++) {
    int ch = key[i] - 'a';
    if (tmp->child[ch] == NULL) {
      tmp->child[ch] = getNode();
      tmp = tmp->child[ch];
    }
  }
  tmp->ch.pb(chh);
}
void query(trie *root, string key, int cid, int ex) {
  trie *tmp = root;

  for (int i = 0; i < sz(key); i++) {
    int ch = key[i] - 'a';

    for (int j = 0; j < sz(tmp->ch); j++) {
      int no = tmp->ch[j];
      if (rng(0, cid, no) == 0 || (cid == 0 && no == ex))
        continue;
      ans++;
    }

    tmp = tmp->child[ch];
    cid--;
  }
  for (int j = 0; tmp != NULL && j < sz(tmp->ch); j++) {
    int no = tmp->ch[j];
    if (rng(0, cid, no) == 0 || (cid == 0 && no == ex))
      continue;
    ans++;
  }
}

int main() {
  GO();
  int n;
  cin >> n;
  trie *root = getNode();
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    string ss = "";
    int siz = sz(s[i]);
    for (int j = siz - 1; j >= 1; j--)
      ss += s[i][j];
    insertt(root, ss, s[i][0] - 'a');
  }

  for (int i = 0; i < n; i++) {

    int siz = sz(s[i]);
    string ss = "";
    for (int j = 0; j < siz; j++) {
      for (int k = 0; k < 26; k++)
        vis[j][k] = (j - 1 >= 0 ? vis[j - 1][k] : 0);
      vis[j][s[i][j] - 'a']++;
    }
    for (int j = siz - 1; j >= 1; j--)
      ss += s[i][j];
    query(root, ss, siz - 1, s[i][0] - 'a');
  }

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