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

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
typedef long long ll;
#define pb(x) push_back(x)

ll mod1 = 1e9 + 7, mod2 = 1e9 + 9;
ll base1 = 1007, base2 = 1311;
ll power1[N], power2[N];
void InitPower(int n) {
  power1[0] = power2[0] = 1;
  for (int i = 1; i <= n; ++i) {
    power1[i] = power1[i - 1] * base1 % mod1;
    power2[i] = power2[i - 1] * base2 % mod2;
  }
}
struct Hash {
  Hash(ll hash1 = 0, ll hash2 = 0) : hash1_(hash1), hash2_(hash2) {}
  bool operator<(Hash h) const {
    if (hash1_ == h.hash1_)
      return hash2_ < h.hash2_;
    else
      return hash1_ < h.hash1_;
  }
  ll hash1_, hash2_;
};
vector<Hash> ha[N];
Hash GetHash(int i, int x, int y) {
  return Hash(
      ((ha[i][y].hash1_ - ha[i][x - 1].hash1_ * power1[y - x + 1]) % mod1 +
       mod1) %
          mod1,
      ((ha[i][y].hash2_ - ha[i][x - 1].hash2_ * power2[y - x + 1]) % mod2 +
       mod2) %
          mod2);
}
vector<int> str[N];
map<Hash, vector<int>> mp;

int cnt[26];
int main() {
  InitPower(N - 1);
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    str[i].pb(0);
    int ch;
    do {
      ch = getchar();
    } while (isspace(ch));
    do {
      str[i].pb(ch - 'a');
    } while (!isspace(ch = getchar()));
    ha[i].resize(str[i].size());

    for (int j = 1; j < str[i].size(); ++j) {
      ha[i][j].hash1_ = (ha[i][j - 1].hash1_ * base1 + str[i][j] + 17) % mod1;
      ha[i][j].hash2_ = (ha[i][j - 1].hash2_ * base2 + str[i][j] + 11) % mod2;
    }
    Hash hash(0, 0);
    if (str[i].size() != 2)
      hash = GetHash(i, 2, str[i].size() - 1);
    if (mp.find(hash) == mp.end())
      mp[hash] = vector<int>(26, 0);
    ++mp[hash][str[i][1]];
  }
  ll ans = 0;
  for (int i = 1; i <= n; ++i) {
    memset(cnt, 0, sizeof(cnt));
    for (int j = 1; j < str[i].size(); ++j)
      ++cnt[str[i][j]];
    for (int j = str[i].size(); j > 1; --j) {
      Hash hash(0, 0);
      if (j != str[i].size()) {
        hash = GetHash(i, j, str[i].size() - 1);
        --cnt[str[i][j]];
      }
      if (mp.find(hash) == mp.end())
        continue;
      vector<int> &tmp = mp[hash];
      for (int k = 0; k < 26; ++k)
        if (cnt[k] > 0) {
          // assert(tmp[k]<=1);
          ans += tmp[k];
        }
    }
  }
  ans -= n;
  printf("%lld", ans);
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
typedef long long ll;
#define pb(x) push_back(x)

ll mod1 = 1e9 + 7, mod2 = 1e9 + 9;
ll base1 = 1007, base2 = 0;
ll power1[N], power2[N];
void InitPower(int n) {
  power1[0] = power2[0] = 1;
  for (int i = 1; i <= n; ++i) {
    power1[i] = power1[i - 1] * base1 % mod1;
    power2[i] = power2[i - 1] * base2 % mod2;
  }
}
struct Hash {
  Hash(ll hash1 = 0, ll hash2 = 0) : hash1_(hash1), hash2_(hash2) {}
  bool operator<(Hash h) const {
    if (hash1_ == h.hash1_)
      return hash2_ < h.hash2_;
    else
      return hash1_ < h.hash1_;
  }
  ll hash1_, hash2_;
};
vector<Hash> ha[N];
Hash GetHash(int i, int x, int y) {
  return Hash(
      ((ha[i][y].hash1_ - ha[i][x - 1].hash1_ * power1[y - x + 1]) % mod1 +
       mod1) %
          mod1,
      ((ha[i][y].hash2_ - ha[i][x - 1].hash2_ * power2[y - x + 1]) % mod2 +
       mod2) %
          mod2);
}

vector<int> str[N];

map<Hash, vector<int>> mp;
int cnt[26];
int main() {
  InitPower(N - 1);
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    str[i].pb(0);
    int ch;
    do {
      ch = getchar();
    } while (isspace(ch));
    do {
      str[i].pb(ch - 'a');
    } while (!isspace(ch = getchar()));
    ha[i].resize(str[i].size());

    for (int j = 1; j < str[i].size(); ++j) {
      ha[i][j].hash1_ = (ha[i][j - 1].hash1_ * base1 + str[i][j] + 17) % mod1;
      ha[i][j].hash2_ = (ha[i][j - 1].hash2_ * base2 + str[i][j] + 11) % mod2;
    }
    Hash hash(0, 0);
    if (str[i].size() != 2)
      hash = GetHash(i, 2, str[i].size() - 1);
    if (mp.find(hash) == mp.end())
      mp[hash] = vector<int>(26, 0);
    ++mp[hash][str[i][1]];
  }
  ll ans = 0;
  for (int i = 1; i <= n; ++i) {
    memset(cnt, 0, sizeof(cnt));
    for (int j = 1; j < str[i].size(); ++j)
      ++cnt[str[i][j]];
    for (int j = str[i].size(); j > 1; --j) {
      Hash hash(0, 0);
      if (j != str[i].size()) {
        hash = GetHash(i, j, str[i].size() - 1);
        --cnt[str[i][j]];
      }
      if (mp.find(hash) == mp.end())
        continue;
      vector<int> &tmp = mp[hash];
      for (int k = 0; k < 26; ++k)
        if (cnt[k] > 0) {
          // assert(tmp[k]<=1);
          ans += tmp[k];
        }
    }
  }
  ans -= n;
  printf("%lld", ans);
  return 0;
}