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

/*
{

AuThOr Gwj
*/
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define rb(a, b, c) for (int a = b; a <= c; ++a)
#define rl(a, b, c) for (int a = b; a >= c; --a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a, b) make_pair(a, b)
#define FIR first
#define SEC second
#define FREO freopen("check.out", "w", stdout)
#define rep(a, b) for (int a = 0; a < b; ++a)
#define SRAND                                                                  \\
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng() % a
#define ALL(a) a.begin(), a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define R(a) cin >> a
#define R2(a, b) cin >> a >> b
using namespace std;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> mp;
/*}
 */
int n = 200000 + 20;
string s[200000 + 20];
int son[1000000 + 1][27], cnt[1000000 + 1][27], old[1000000 + 1][27];
int root = 0;
int cnt_ = 0;
int have[27];
void go(int now, int is, int t) {
  if (is == s[t].length())
    return;
  if (son[now][s[t][is]]) {
    if (have[s[t][is]] == 1) {
      old[son[now][s[t][is]]][s[t][is]]++;
      cnt[son[now][s[t][is]]][s[t][is]]++;
    }
    have[s[t][is]]--;
    go(son[now][s[t][is]], is + 1, t);
  } else {
    son[now][s[t][is]] = ++cnt_;
    if (have[s[t][is]] == 1) {
      old[son[now][s[t][is]]][s[t][is]]++;
      cnt[son[now][s[t][is]]][s[t][is]]++;
    }
    have[s[t][is]]--;
    go(son[now][s[t][is]], is + 1, t);
  }
}
void calc(int now) {
  rb(i, 1, 26) {
    if (son[now][i]) {
      calc(son[now][i]);
      rb(k, 1, 26) cnt[now][k] += cnt[son[now][i]][k];
    }
  }
}
int rest(int now, int is, string &s_, int need) {
  if (is == s_.length())
    return cnt[now][need] - old[now][need] - 1;
  return rest(son[now][s_[is]], is + 1, s_, need);
}
int main() {
  fastio;
  R(n);
  rb(i, 1, n) {
    rb(j, 1, 26) have[j] = 0;
    R(s[i]);
    reverse(ALL(s[i]));
    rep(j, s[i].length()) { s[i][j] -= 'a' - 1; }
    rb(j, 0, s[i].length() - 1) have[s[i][j]]++;
    go(root, 0, i);
  }
  calc(root);
  LL res = 0;
  rb(i, 1, n) {
    string ss = "";
    rep(j, s[i].length()) {
      if (j != s[i].length() - 1)
        ss += s[i][j];
    }
    res += rest(root, 0, ss, s[i][s[i].length() - 1]);
  }
  cout << res << endl;
  return 0;
}


Error: -11

Fixed submission:

/*
{

AuThOr Gwj
*/
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define rb(a, b, c) for (int a = b; a <= c; ++a)
#define rl(a, b, c) for (int a = b; a >= c; --a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a, b) make_pair(a, b)
#define FIR first
#define SEC second
#define FREO freopen("check.out", "w", stdout)
#define rep(a, b) for (int a = 0; a < b; ++a)
#define SRAND                                                                  \\
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng() % a
#define ALL(a) a.begin(), a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define R(a) cin >> a
#define R2(a, b) cin >> a >> b
using namespace std;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> mp;
/*}
 */
int n = 200000 + 20;
string s[200000 + 20];
int son[1000000 + 1][27], cnt[1000000 + 1][27], old[1000000 + 1][27];
int root = 0;
int cnt_ = 0;
int have[27];
void go(int now, int is, string s_) {
  if (is == s_.length())
    return;
  if (son[now][s_[is]]) {
    //	if(have[s_[is]]==1){
    //		old[son[now][s_[is]]][s_[is]]++;
    //		cnt[son[now][s_[is]]][s_[is]]++;
    //	}
    //	have[s_[is]]--;
    go(son[now][s_[is]], is + 1, s_);
  } else {
    son[now][s_[is]] = ++cnt_;
    //		if(have[s_[is]]==1){
    //		old[son[now][s_[is]]][s_[is]]++;
    //		cnt[son[now][s_[is]]][s_[is]]++;
    //		}
    //		have[s_[is]]--;
    go(son[now][s_[is]], is + 1, s_);
  }
}
void calc(int now) {
  rb(i, 1, 26) {
    if (son[now][i]) {
      calc(son[now][i]);
      rb(k, 1, 26) cnt[now][k] += cnt[son[now][i]][k];
    }
  }
}
int rest(int now, int is, string s_, int need) {
  if (is == s_.length())
    return cnt[now][need] - old[now][need] - 1;
  return rest(son[now][s_[is]], is + 1, s_, need);
}
int main() {
  fastio;
  R(n);
  rb(i, 1, n) {
    rb(j, 1, 26) have[j] = 0;
    R(s[i]);
    reverse(ALL(s[i]));
    rep(j, s[i].length()) { s[i][j] -= 'a' - 1; }
    rb(j, 0, s[i].length() - 1) have[s[i][j]]++;
    go(root, 0, s[i]);
  }
  return 0;
  calc(root);
  LL res = 0;
  rb(i, 1, n) {
    string ss = "";
    rep(j, s[i].length()) {
      if (j != s[i].length() - 1)
        ss += s[i][j];
    }
    res += rest(root, 0, ss, s[i][s[i].length() - 1]);
  }
  cout << res << endl;
  return 0;
}