Problem p02589 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define IN freopen("alchemy_input.txt", "r", stdin)
#define OUT freopen("output.txt", "w", stdout)

#define pb push_back
#define mp make_pair
#define FOR(i, a, b) for (i = a; i <= b; i++)
#define DBG printf("Hi\n")
#define i64 long long int
#define ui64 unsigned long long int
#define xx first
#define yy second
#define ln 17
#define off 2

#define sq(x) ((x) * (x))

#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

using namespace __gnu_pbds;
using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<i64, null_type, less<i64>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef pair<int, int> pii;

// #define log 20
#define mod1 1000000007LL
#define mod2 1000000009LL
#define INF 2000000000
#define maxn 200005

const long double eps = 1e-9;

vector<string> vs;

bool comp(string s1, string s2) { return s1.size() > s2.size(); }

map<pair<pair<int, int>, int>, int> Map;

int cnt[maxn][28];
int yes[maxn][28];

int main() {
  FASTIO;

  int n;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    vs.pb(s);
  }

  sort(vs.begin(), vs.end(), comp);

  //   reverse(vs.begin(),vs.end()) ;

  int cur = 0;

  i64 ans = 0;

  for (int i = 0; i < n; i++) {
    int sz = vs[i].size();
    for (int j = sz - 1; j >= 0; j--)
      for (int k = 0; k < 26; k++)
        yes[j][k] = 0;

    for (int j = sz - 1; j >= 0; j--) {
      for (int k = 0; k < 26; k++)
        yes[j][k] |= yes[j + 1][k];
      yes[j][vs[i][j] - 'a'] = 1;
    }

    int b1 = 29, b2 = 43;
    int h1 = 0, h2 = 0;

    for (int j = 0; j < sz - 1; j++) {
      h1 = (1LL * h1 * b1 + vs[i][j] - 'a') % mod1;
      h2 = (1LL * h2 * b2 + vs[i][j] - 'a') % mod2;
    }
    if (Map.find(mp(mp(h1, h2), sz - 1)) != Map.end()) {
      int idx = Map[mp(mp(h1, h2), sz - 1)];
      ans += cnt[idx][vs[i][sz - 1] - 'a'];
    }

    h1 = 0;
    h2 = 0;

    for (int j = 0; j < sz; j++) {
      if (Map.find(mp(mp(h1, h2), j)) == Map.end()) {
        Map[mp(mp(h1, h2), j)] = ++cur;
      }
      int c = Map[mp(mp(h1, h2), j)];
      for (int k = 0; k < 26; k++)
        cnt[c][k] += yes[j][k];
      h1 = (1LL * h1 * b1 + vs[i][j] - 'a') % mod1;
      h2 = (1LL * h2 * b2 + vs[i][j] - 'a') % mod2;
    }

    for (int j = sz - 1; j >= 0; j--) {
      for (int k = 0; k < 26; k++)
        yes[j][k] = 0;
    }

    //  cout<<vs[i]<<" "<<ans<<endl ;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define IN freopen("alchemy_input.txt", "r", stdin)
#define OUT freopen("output.txt", "w", stdout)

#define pb push_back
#define mp make_pair
#define FOR(i, a, b) for (i = a; i <= b; i++)
#define DBG printf("Hi\n")
#define i64 long long int
#define ui64 unsigned long long int
#define xx first
#define yy second
#define ln 17
#define off 2

#define sq(x) ((x) * (x))

#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

using namespace __gnu_pbds;
using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<i64, null_type, less<i64>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef pair<int, int> pii;

// #define log 20
#define mod1 1000000007LL
#define mod2 1000000009LL
#define INF 2000000000
#define maxn 1000005

const long double eps = 1e-9;

vector<string> vs;

bool comp(string s1, string s2) { return s1.size() > s2.size(); }

map<pair<pair<int, int>, int>, int> Map;

int cnt[maxn][28];
int yes[maxn][28];

int main() {
  FASTIO;

  int n;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    vs.pb(s);
  }

  sort(vs.begin(), vs.end(), comp);

  //   reverse(vs.begin(),vs.end()) ;

  int cur = 0;

  i64 ans = 0;

  for (int i = 0; i < n; i++) {
    int sz = vs[i].size();
    for (int j = sz - 1; j >= 0; j--)
      for (int k = 0; k < 26; k++)
        yes[j][k] = 0;

    for (int j = sz - 1; j >= 0; j--) {
      for (int k = 0; k < 26; k++)
        yes[j][k] |= yes[j + 1][k];
      yes[j][vs[i][j] - 'a'] = 1;
    }

    int b1 = 29, b2 = 43;
    int h1 = 0, h2 = 0;

    for (int j = 0; j < sz - 1; j++) {
      h1 = (1LL * h1 * b1 + vs[i][j] - 'a') % mod1;
      h2 = (1LL * h2 * b2 + vs[i][j] - 'a') % mod2;
    }
    if (Map.find(mp(mp(h1, h2), sz - 1)) != Map.end()) {
      int idx = Map[mp(mp(h1, h2), sz - 1)];
      ans += cnt[idx][vs[i][sz - 1] - 'a'];
    }

    h1 = 0;
    h2 = 0;

    for (int j = 0; j < sz; j++) {
      if (Map.find(mp(mp(h1, h2), j)) == Map.end()) {
        Map[mp(mp(h1, h2), j)] = ++cur;
      }
      int c = Map[mp(mp(h1, h2), j)];
      for (int k = 0; k < 26; k++)
        cnt[c][k] += yes[j][k];
      h1 = (1LL * h1 * b1 + vs[i][j] - 'a') % mod1;
      h2 = (1LL * h2 * b2 + vs[i][j] - 'a') % mod2;
    }

    for (int j = sz - 1; j >= 0; j--) {
      for (int k = 0; k < 26; k++)
        yes[j][k] = 0;
    }

    //  cout<<vs[i]<<" "<<ans<<endl ;
  }

  cout << ans << endl;

  return 0;
}