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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head

typedef pair<int, int> hashv;
const ll mod1 = 1000000007;
const ll mod2 = 1000000009;

hashv operator+(hashv a, hashv b) {
  int c1 = a.fi + b.fi, c2 = a.se + b.se;
  if (c1 >= mod1)
    c1 -= mod1;
  if (c2 >= mod2)
    c2 -= mod2;
  return mp(c1, c2);
}

hashv operator-(hashv a, hashv b) {
  int c1 = a.fi - b.fi, c2 = a.se - b.se;
  if (c1 < 0)
    c1 += mod1;
  if (c2 < 0)
    c2 += mod2;
  return mp(c1, c2);
}

hashv operator*(hashv a, hashv b) {
  return mp(1ll * a.fi * b.fi % mod1, 1ll * a.se * b.se % mod2);
}
const int N = 210000;
map<hashv, int> st, stt;
int n, cnt[40];
char s[1010000];
hashv val[N];

struct Hash_table {
  static const int V = 50010003;
  int fst[V], nxt[V];
  int ctm, ptm[V], T;
  int val[V];
  hashv key[V];
  void init() {
    T = 0;
    ctm++;
  }
  void add(hashv s) {
    int S = (s.fi * mod2 + s.se) % V;
    if (ptm[S] != ctm)
      ptm[S] = ctm, fst[S] = -1;
    for (int j = fst[S]; j != -1; j = nxt[j])
      if (key[j] == s) {
        val[j]++;
        return;
      }
    nxt[T] = fst[S], fst[S] = T, key[T] = s, val[T] = 1;
    T++;
  }
  int query(hashv s) {
    int S = (s.fi * mod2 + s.se) % V;
    if (ptm[S] != ctm)
      return 0;
    for (int j = fst[S]; j != -1; j = nxt[j])
      if (key[j] == s)
        return val[j];
    return 0;
  }
} Hash;

int main() {
  scanf("%d", &n);
  hashv base(13331, 23333);
  Hash.init();
  rep(i, 0, n) {
    scanf("%s", s);
    int m = strlen(s);
    reverse(s, s + m);
    hashv p(0, 0);
    rep(i, 0, 26) cnt[i] = 0;
    rep(i, 0, m) cnt[s[i] - 'a']++;
    rep(i, 0, m) {
      rep(j, 0, 26) if (cnt[j] > 0) {
        auto q = p;
        q = q * base + hashv(j + 'a', j + 'a');
        Hash.add(q);
      }
      cnt[s[i] - 'a']--;
      p = p * base + hashv(s[i], s[i]);
    }
    stt[p]++;
    val[i] = p;
  }
  ll ans = 0;
  rep(i, 0, n) ans += Hash.query(val[i]);
  for (auto p : stt) {
    ans = ans - (ll)p.se * (p.se + 1) / 2;
    // printf("zz %d\\n",p.se);
  }
  printf("%lld\\n", ans);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head

typedef pair<int, int> hashv;
const ll mod1 = 1000000007;
const ll mod2 = 1000000009;

hashv operator+(hashv a, hashv b) {
  int c1 = a.fi + b.fi, c2 = a.se + b.se;
  if (c1 >= mod1)
    c1 -= mod1;
  if (c2 >= mod2)
    c2 -= mod2;
  return mp(c1, c2);
}

hashv operator-(hashv a, hashv b) {
  int c1 = a.fi - b.fi, c2 = a.se - b.se;
  if (c1 < 0)
    c1 += mod1;
  if (c2 < 0)
    c2 += mod2;
  return mp(c1, c2);
}

hashv operator*(hashv a, hashv b) {
  return mp(1ll * a.fi * b.fi % mod1, 1ll * a.se * b.se % mod2);
}
const int N = 210000;
map<hashv, int> st, stt;
int n, cnt[40];
char s[N];
hashv val[N];

int main() {
  scanf("%d", &n);
  hashv base(13331, 23333);
  rep(i, 0, n) {
    scanf("%s", s);
    int m = strlen(s);
    reverse(s, s + m);
    hashv p(0, 0);
    rep(i, 0, 26) cnt[i] = 0;
    rep(i, 0, m) cnt[s[i] - 'a']++;
    rep(i, 0, m) {
      rep(j, 0, 26) if (cnt[j] > 0) {
        auto q = p;
        q = q * base + hashv(j + 'a', j + 'a');
        st[q]++;
      }
      cnt[s[i] - 'a']--;
      p = p * base + hashv(s[i], s[i]);
    }
    stt[p]++;
    val[i] = p;
  }
  ll ans = 0;
  rep(i, 0, n) ans += st[val[i]];
  for (auto p : stt) {
    ans = ans - (ll)p.se * (p.se + 1) / 2;
    // printf("zz %d\\n",p.se);
  }
  printf("%lld\\n", ans);
}