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

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <set>
#include <string>
#define MAXIOLG 25
#define FILENAME(x)                                                            \\
  freopen(x ".in", "r", stdin);                                                \\
  freopen(x ".out", "w", stdout);
#define MAXN 200005
#define MAXL 1000005
#define BASE 29
#define P1 1000000097
#define P2 1000000021

#define MD(x, pp) (((x) >= (pp)) ? ((x) -= (pp)) : (0))
using namespace std;

typedef long long ll;
typedef long double ld;
typedef ll io_t;
io_t shin[MAXIOLG];
io_t seto(void);
void ayano(io_t x, char spliter = '\\n');

typedef pair<int, int> pii;

set<pii> st;
string strs[MAXN];
pii hana[MAXL];
int char_cnt[MAXN];

void get_hana(const string &str);
bool cmp(const string &lhs, const string &rhs);

int main(void) {
  int n = seto();
  for (int i = 1; i <= n; i++)
    cin >> strs[i], reverse(strs[i].begin(), strs[i].end());
  sort(strs + 1, strs + n + 1, cmp);
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    get_hana(strs[i]);
    for (int k = 1; k <= 26; k++)
      char_cnt[k] = 0;
    for (auto ch : strs[i])
      char_cnt[ch - 'a' + 1]++;
    for (int k = 1; k <= 26; k++)
      if (char_cnt[k])
        ans += st.count(make_pair(k, k));
    for (int j = 1, tren = strs[i].size(); j < tren; j++) {
      if (j > 1)
        ans += st.count(hana[j]);
      char_cnt[strs[i][j - 1] - 'a' + 1]--;
      for (int k = 1; k <= 26; k++) {
        if (('a' + k - 1) == strs[i][j] || (!char_cnt[k]))
          continue;
        int x1 = 1ll * hana[j].first * BASE % P1 + k;
        MD(x1, P1);
        int x2 = 1ll * hana[j].second * BASE % P2 + k;
        MD(x2, P2);
        ans += st.count(make_pair(x1, x2));
      }
    }
    st.insert(hana[strs[i].size()]);
  }
  ayano(ans);
  return 0;
}

void get_hana(const string &str) {
  hana[0] = make_pair(0, 0);
  for (int i = 0, j = 1, tren = str.size(); i < tren; i++, j++) {
    int x1 = 1ll * hana[j - 1].first * BASE % P1 + (str[i] - 'a' + 1);
    MD(x1, P1);
    int x2 = 1ll * hana[j - 1].second * BASE % P2 + (str[i] - 'a' + 1);
    MD(x2, P2);
    hana[j] = make_pair(x1, x2);
  }
}
bool cmp(const string &lhs, const string &rhs) {
  return lhs.size() < rhs.size();
}

io_t seto(void) {
  io_t x = 0;
  int symbol = 0;
  char ch = getchar();
  while (!isdigit(ch))
    (ch == '-') ? (symbol = 1) : (0), ch = getchar();
  while (isdigit(ch))
    x = (x * 10) + (ch - '0'), ch = getchar();
  return (symbol) ? (-x) : (x);
}
void ayano(io_t x, char spliter) {
  if (!x) {
    putchar('0'), putchar(spliter);
    return;
  }
  if (x < 0)
    putchar('-'), x = -x;
  int len = 0;
  while (x) {
    io_t d = x / 10;
    shin[len++] = x - (d * 10);
    x = d;
  }
  while (len--)
    putchar(shin[len] + '0');
  putchar(spliter);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <set>
#include <string>
#define MAXIOLG 25
#define FILENAME(x)                                                            \\
  freopen(x ".in", "r", stdin);                                                \\
  freopen(x ".out", "w", stdout);
#define MAXN 200005
#define BASE 29
#define P1 1000000097
#define P2 1000000021

#define MD(x, pp) (((x) >= (pp)) ? ((x) -= (pp)) : (0))
using namespace std;

typedef long long ll;
typedef long double ld;
typedef ll io_t;
io_t shin[MAXIOLG];
io_t seto(void);
void ayano(io_t x, char spliter = '\\n');

typedef pair<int, int> pii;

set<pii> st;
string strs[MAXN];
pii hana[MAXN];
int char_cnt[MAXN];

void get_hana(const string &str);
bool cmp(const string &lhs, const string &rhs);

int main(void) {
  int n = seto();
  for (int i = 1; i <= n; i++)
    cin >> strs[i], reverse(strs[i].begin(), strs[i].end());
  sort(strs + 1, strs + n + 1, cmp);
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    get_hana(strs[i]);
    for (int k = 1; k <= 26; k++)
      char_cnt[k] = 0;
    for (auto ch : strs[i])
      char_cnt[ch - 'a' + 1]++;
    for (int k = 1; k <= 26; k++)
      if (char_cnt[k])
        ans += st.count(make_pair(k, k));
    for (int j = 1, tren = strs[i].size(); j < tren; j++) {
      if (j > 1)
        ans += st.count(hana[j]);
      char_cnt[strs[i][j - 1] - 'a' + 1]--;
      for (int k = 1; k <= 26; k++) {
        if (('a' + k - 1) == strs[i][j] || (!char_cnt[k]))
          continue;
        int x1 = 1ll * hana[j].first * BASE % P1 + k;
        MD(x1, P1);
        int x2 = 1ll * hana[j].second * BASE % P2 + k;
        MD(x2, P2);
        ans += st.count(make_pair(x1, x2));
      }
    }
    st.insert(hana[strs[i].size()]);
  }
  ayano(ans);
  return 0;
}

void get_hana(const string &str) {
  hana[0] = make_pair(0, 0);
  for (int i = 0, j = 1, tren = str.size(); i < tren; i++, j++) {
    int x1 = 1ll * hana[j - 1].first * BASE % P1 + (str[i] - 'a' + 1);
    MD(x1, P1);
    int x2 = 1ll * hana[j - 1].second * BASE % P2 + (str[i] - 'a' + 1);
    MD(x2, P2);
    hana[j] = make_pair(x1, x2);
  }
}
bool cmp(const string &lhs, const string &rhs) {
  return lhs.size() < rhs.size();
}

io_t seto(void) {
  io_t x = 0;
  int symbol = 0;
  char ch = getchar();
  while (!isdigit(ch))
    (ch == '-') ? (symbol = 1) : (0), ch = getchar();
  while (isdigit(ch))
    x = (x * 10) + (ch - '0'), ch = getchar();
  return (symbol) ? (-x) : (x);
}
void ayano(io_t x, char spliter) {
  if (!x) {
    putchar('0'), putchar(spliter);
    return;
  }
  if (x < 0)
    putchar('-'), x = -x;
  int len = 0;
  while (x) {
    io_t d = x / 10;
    shin[len++] = x - (d * 10);
    x = d;
  }
  while (len--)
    putchar(shin[len] + '0');
  putchar(spliter);
}