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

#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X)                                                                 \\
  int(X);                                                                      \\
  scanf("%d", &X)
#define DRII(X, Y)                                                             \\
  int X, Y;                                                                    \\
  scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z)                                                         \\
  int X, Y, Z;                                                                 \\
  scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET                                                                  \\
  int ___T, case_n = 1;                                                        \\
  scanf("%d ", &___T);                                                         \\
  while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define RF(x) freopen(x, "r", stdin)
#define WF(x) freopen(x, "w", stdout)
typedef long long LL;
using namespace std;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PII;
const LL MOD = (LL)1e9 + 7;
const int SIZE = 2e5 + 5;
const LL INF = 1LL << 60;
const double eps = 1e-4;
const double PI = 3.1415926535897932;

int cur = 1;

int adj[1000009][26];
int cnt[1000009][26]; // extra lol
string s[200009];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  REP(i, n) {
    cin >> s[i];
    reverse(ALL(s[i]));
  }
  LL ans = 0;
  for (int iter = 0; iter < 2; iter++) {
    MS1(adj);
    REP(i, n) {
      int x = 0;
      bool has = 1;
      REP(j, SZ(s[i]) - 1) {
        if (adj[x][s[i][j] - 'a'] == -1) {
          has = 0;
          break;
        } else
          x = adj[x][s[i][j] - 'a'];
      }
      if (has)
        ans += cnt[x][s[i].back() - 'a'];
      int bm[SZ(s[i]) + 1];
      bm[SZ(s[i])] = 0;
      for (int j = SZ(s[i]) - 1; j >= 0; j--) {
        bm[j] = bm[j + 1] | 1 << (s[i][j] - 'a');
      }
      x = 0;
      REP(j, SZ(s[i])) {
        for (int k = 0; k < 26; k++) {
          if (bm[j] & (1 << k))
            cnt[x][k]++;
        }
        if (adj[x][s[i][j] - 'a'] == -1) {
          adj[x][s[i][j] - 'a'] = cur;
          x = cur++;
        } else
          x = adj[x][s[i][j] - 'a'];
      }
    }
    // cout << "BIG" << ans << endl;
    if (iter == 1)
      break;
    cur = 1;
    MS0(cnt);
    reverse(s, s + n);
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X)                                                                 \\
  int(X);                                                                      \\
  scanf("%d", &X)
#define DRII(X, Y)                                                             \\
  int X, Y;                                                                    \\
  scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z)                                                         \\
  int X, Y, Z;                                                                 \\
  scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET                                                                  \\
  int ___T, case_n = 1;                                                        \\
  scanf("%d ", &___T);                                                         \\
  while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define RF(x) freopen(x, "r", stdin)
#define WF(x) freopen(x, "w", stdout)
typedef long long LL;
using namespace std;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PII;
const LL MOD = (LL)1e9 + 7;
const int SIZE = 2e5 + 5;
const LL INF = 1LL << 60;
const double eps = 1e-4;
const double PI = 3.1415926535897932;

int cur = 1;

int adj[100009][26];
int cnt[100009][26]; // extra lol
string s[200009];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  REP(i, n) cin >> s[i];
  LL ans = 0;
  for (int iter = 0; iter < 2; iter++) {
    MS1(adj);
    REP(i, n) {
      reverse(ALL(s[i]));
      int x = 0;
      bool has = 1;
      REP(j, SZ(s[i]) - 1) {
        if (adj[x][s[i][j] - 'a'] == -1) {
          has = 0;
          break;
        } else
          x = adj[x][s[i][j] - 'a'];
      }
      if (has)
        ans += cnt[x][s[i].back() - 'a'];
      int bm[SZ(s[i]) + 1];
      bm[SZ(s[i])] = 0;
      for (int j = SZ(s[i]) - 1; j >= 0; j--) {
        bm[j] = bm[j + 1] | 1 << (s[i][j] - 'a');
      }
      x = 0;
      REP(j, SZ(s[i])) {
        for (int k = 0; k < 26; k++) {
          if (bm[j] & (1 << k))
            cnt[x][k]++;
        }
        if (adj[x][s[i][j] - 'a'] == -1) {
          adj[x][s[i][j] - 'a'] = cur++;
          x = cur;
        } else
          x = adj[x][s[i][j] - 'a'];
      }
      // cout << i << " " << ans << endl;
    }
    // cout << "BIG" << ans << endl;
    if (iter == 1)
      break;
    cur = 1;
    MS0(cnt);
    reverse(s, s + n);
  }
  cout << ans;
}