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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll(c, d)));
#define re(c, b) for (ll c = 0; c < b; c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

typedef __int128_t ull;
const ull h = 92709568269121;
const ull B = 912121313;
int main(int argc, char const *argv[]) {
  ll n;
  std::cin >> n;
  // x -> y
  //(x-y)回の操作
  // 見る範囲 0 ~ (x-y)にyの先頭があり、残りが末尾で一致している
  vector<string> v(n);
  re(i, n) std::cin >> v[i];
  sort(all(v), [](string a, string b) { return a.size() < b.size(); });
  ll ans = 0;
  // hash -> num

  map<ull, ll> mp;
  for (int i = 0; i < n; i++) {
    ull ret = 0;
    ll m = v[i].size();
    vector<ll> ok(m + 1, 0);
    for (int j = 0; j < m; j++) {
      ll now = j + 1;
      ll num = v[i][j] - 'a';
      ok[now] = ok[now - 1] | (1 << num);
    }
    // iより左にjが存在するか
    for (int j = m; j >= 0; j--) {
      if (j == m) {
        // 26種類
        for (int k = 0; k < 26; k++) {
          if ((ok[j] >> k) & 1) {
            ull ad = 'a' + k;
            // 付け加えたもの
            ull tmp = ((ret * B) % h + ad) % h;
            auto itr = mp.find(tmp);
            if (itr != mp.end())
              ans += (*itr).second;
          }
        }
        continue;
      }

      ull num = v[i][j];
      ret = ((ret * B) % h + num) % h;

      for (int k = 0; k < 26; k++) {
        if ((ok[j] >> k) & 1) {
          // 付け加えたもの
          ull ad = 'a' + k;
          ull tmp = ((ret * B) % h + ad) % h;
          auto itr = mp.find(tmp);
          if (itr != mp.end())
            ans += (*itr).second;
        }
      }
    }
    auto itr = mp.find(ret);
    if (itr == mp.end())
      mp.emplace(ret, 1);
    else
      (*itr).second++;
  }

  std::cout << ans << '\\n';
  return 0;
}


Error: -6
malloc(): corrupted top size


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll(c, d)));
#define re(c, b) for (ll c = 0; c < b; c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

typedef unsigned long long ull;
const ull h = 1312131113;
const ull B = 912121313;
int overlap(string a, string b) {
  int al = a.length(), bl = b.length();
  int ans = 0;
  ull ah = 0, bh = 0, t = 1;
  for (int i = 1; i <= min(al, bl); i++) {
    ah = (ah + (a[al - i] * t) % h) % h;
    bh = ((bh * B) % h + b[i - 1]) % h;
    if (ah == bh)
      ans = i;
    t = (t * B) % h;
  }
  return ans;
}
int main(int argc, char const *argv[]) {
  ll n;
  std::cin >> n;
  // x -> y
  //(x-y)回の操作
  // 見る範囲 0 ~ (x-y)にyの先頭があり、残りが末尾で一致している
  vector<string> v(n);
  re(i, n) std::cin >> v[i];
  sort(all(v), [](string a, string b) { return a.size() < b.size(); });
  ll ans = 0;
  // hash -> num

  map<ull, ll> mp;
  for (int i = 0; i < n; i++) {
    ull ret = 0;
    ll m = v[i].size();

    vector<ll> ok(m + 1, 0);
    for (int j = 0; j <= m; j++) {
      ll now = j + 1;
      ll num = v[i][j] - 'a';
      ok[now] = ok[now - 1] | (1 << num);
    }

    // iより左にjが存在するか
    for (int j = m; j >= 0; j--) {
      if (j == m) {
        // 26種類
        for (int k = 0; k < 26; k++) {
          if ((ok[j] >> k) & 1) {
            // 付け加えたもの
            ull tmp = ((ret * B) % h + (k + 1)) % h;
            auto itr = mp.find(tmp);
            if (itr != mp.end())
              ans += (*itr).second;
          }
        }
        continue;
      }
      ull num = v[i][j] - 'a' + 1;
      ret = ((ret * B) % h + num) % h;

      for (int k = 0; k < 26; k++) {
        if ((ok[j] >> k) & 1) {
          // 付け加えたもの
          ull tmp = ((ret * B) % h + (k + 1)) % h;
          auto itr = mp.find(tmp);
          if (itr != mp.end())
            ans += (*itr).second;
        }
      }
    }
    auto itr = mp.find(ret);
    if (itr == mp.end())
      mp.emplace(ret, 1);
    else
      (*itr).second++;
  }

  std::cout << ans << '\\n';
  return 0;
}