Problem p02589 (C++)

Buggy submission (status: Runtime Error):
//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define st first
#define nd second

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;

const int kAlpha = 26;
const int kMaxSize = 4e5;

struct Node {
  int children[kAlpha];
  bool is_end;
};
int num_nodes = 1;

Node nodes[kMaxSize];
int cnt_alpha[kAlpha];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);

  int n;
  cin >> n;
  vector<string> ss(n);
  for (auto &x : ss) {
    cin >> x;
  }
  sort(ALL(ss),
       [&](const string &lhs, const string &rhs) { return SZ(lhs) < SZ(rhs); });

  ll ans = 0;

  for (string str : ss) {
    for (int i = 0; i < kAlpha; ++i) {
      cnt_alpha[i] = 0;
    }
    for (char ch : str) {
      ++cnt_alpha[ch - 'a'];
    }

    const int len = SZ(str);
    int curv = 0;
    for (int i = len - 1; i >= 0; --i) {
      const char ch = str[i];
      const int chidx = ch - 'a';
      for (int other = 0; other < kAlpha; ++other) {
        if (!cnt_alpha[other]) {
          continue;
        }
        const int s = nodes[curv].children[other];
        if (s && nodes[s].is_end) {
          ++ans;
        }
      }
      --cnt_alpha[chidx];

      if (!nodes[curv].children[chidx]) {
        break;
      } else {
        curv = nodes[curv].children[chidx];
      }
    }

    curv = 0;
    for (int i = len - 1; i >= 0; --i) {
      const char ch = str[i];
      const int chidx = ch - 'a';
      int &s = nodes[curv].children[chidx];
      if (!s) {
        s = num_nodes++;
      }
      curv = s;
    }
    nodes[curv].is_end = true;
  }

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


Fixed submission:
//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define st first
#define nd second

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;

const int kAlpha = 26;
const int kMaxSize = 2e6;

struct Node {
  int children[kAlpha];
  bool is_end;
};
int num_nodes = 1;

Node nodes[kMaxSize];
int cnt_alpha[kAlpha];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);

  int n;
  cin >> n;
  vector<string> ss(n);
  for (auto &x : ss) {
    cin >> x;
  }
  sort(ALL(ss),
       [&](const string &lhs, const string &rhs) { return SZ(lhs) < SZ(rhs); });

  ll ans = 0;

  for (string str : ss) {
    for (int i = 0; i < kAlpha; ++i) {
      cnt_alpha[i] = 0;
    }
    for (char ch : str) {
      ++cnt_alpha[ch - 'a'];
    }

    const int len = SZ(str);
    int curv = 0;
    for (int i = len - 1; i >= 0; --i) {
      const char ch = str[i];
      const int chidx = ch - 'a';
      for (int other = 0; other < kAlpha; ++other) {
        if (!cnt_alpha[other]) {
          continue;
        }
        const int s = nodes[curv].children[other];
        if (s && nodes[s].is_end) {
          ++ans;
        }
      }
      --cnt_alpha[chidx];

      if (!nodes[curv].children[chidx]) {
        break;
      } else {
        curv = nodes[curv].children[chidx];
      }
    }

    curv = 0;
    for (int i = len - 1; i >= 0; --i) {
      const char ch = str[i];
      const int chidx = ch - 'a';
      int &s = nodes[curv].children[chidx];
      if (!s) {
        s = num_nodes++;
      }
      curv = s;
    }
    nodes[curv].is_end = true;
  }

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