Problem p01726 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
#define MOD 1000000007ll
#define INF 1000000000ll
#define EPS 1e-10
#define FOR(i, n, m) for (ll i = n; i < (ll)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define DUMP(a)                                                                \\
  REP(d, a.size()) {                                                           \\
    cout << a[d];                                                              \\
    if (d != a.size() - 1)                                                     \\
      cout << " ";                                                             \\
    else                                                                       \\
      cout << endl;                                                            \\
  }
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v)                                                              \\
  sort(ALL(v));                                                                \\
  v.erase(unique(ALL(v)), v.end());
#define pb push_back

string s, t;
vector<char> _s;
vector<char> _t;
ll m, n;
vector<pair<int, int>> hsh(15600000, P(-1, -1));
ll idx = 0;
const ll base = 100007LL;
const ll mod1 = 1000000007LL, mod2 = 1000000009LL;
vector<ll> fact1(300030, 0);
vector<ll> fact2(300030, 0);
ll rolling1 = 0, rolling2 = 0;

void fill_fact() {
  fact1[0] = 1;
  fact2[0] = 1;
  FOR(i, 1, 300030) {
    fact1[i] = (fact1[i - 1] * base);
    fact1[i] %= mod1;
    fact2[i] = (fact2[i - 1] * base);
    fact2[i] %= mod2;
  }
}

void fill_hash() {
  REP(i, n) {
    rolling1 *= base;
    rolling2 *= base;
    rolling1 += _t[i];
    rolling2 += _t[i];
    rolling1 %= mod1;
    rolling2 %= mod2;
  }
  REP(i, n) {
    rolling1 -= fact1[n - 1 - i] * _t[i];
    rolling1 = (rolling1 % mod1 + mod1) % mod1;
    rolling2 -= fact2[n - 1 - i] * _t[i];
    rolling2 = (rolling2 % mod2 + mod2) % mod2;
    for (ll j = 1; j <= 52; j++) {
      rolling1 += fact1[n - 1 - i] * j;
      rolling1 %= mod1;
      rolling2 += fact2[n - 1 - i] * j;
      rolling2 %= mod2;
      hsh[idx] = pair<int, int>(rolling1, rolling2);
      idx++;
      rolling1 -= fact1[n - 1 - i] * j;
      rolling1 = (rolling1 % mod1 + mod1) % mod1;
      rolling2 -= fact2[n - 1 - i] * j;
      rolling2 = (rolling2 % mod2 + mod2) % mod2;
    }
    rolling1 += fact1[n - 1 - i] * _t[i];
    rolling1 = (rolling1 % mod1 + mod1) % mod1;
    rolling2 += fact2[n - 1 - i] * _t[i];
    rolling2 = (rolling2 % mod2 + mod2) % mod2;
  }
  UNIQUE(hsh);
  auto ite = lower_bound(ALL(hsh), pair<int, int>(rolling1, rolling2));
  hsh.erase(ite);
}

ll solve() {
  fill_fact();
  fill_hash();
  ll r1 = 0, r2 = 0;
  ll ret = 0;
  for (ll i = 0; i < m - n + 1; i++) {
    if (i == 0) {
      REP(j, n) {
        r1 *= base;
        r2 *= base;
        r1 += _s[j];
        r2 += _s[j];
        r1 %= mod1;
        r2 %= mod2;
      }
    } else {
      r1 *= base;
      r2 *= base;
      r1 += _s[n - 1 + i];
      r2 += _s[n - 1 + i];
      r1 -= fact1[n] * _s[i - 1];
      r2 -= fact2[n] * _s[i - 1];
      r1 = (r1 % mod1 + mod1) % mod1;
      r2 = (r2 % mod2 + mod2) % mod2;
    }
    auto ite = lower_bound(ALL(hsh), pair<int, int>(r1, r2));
    if (*ite == pair<int, int>(r1, r2))
      ret++;
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> s >> t;
  m = s.size();
  n = t.size();
  _s.assign(m, 0);
  _t.assign(n, 0);
  REP(i, m) {
    if (s[i] >= 'A' && s[i] <= 'Z')
      _s[i] = 27 + (s[i] - 'A');
    else
      _s[i] = 1 + (s[i] - 'a');
  }

  REP(i, n) {
    if (t[i] >= 'A' && t[i] <= 'Z')
      _t[i] = 27 + (t[i] - 'A');
    else
      _t[i] = 1 + (t[i] - 'a');
  }
  cout << solve() << endl;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
#define MOD 1000000007ll
#define INF 1000000000ll
#define EPS 1e-10
#define FOR(i, n, m) for (ll i = n; i < (ll)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define DUMP(a)                                                                \\
  REP(d, a.size()) {                                                           \\
    cout << a[d];                                                              \\
    if (d != a.size() - 1)                                                     \\
      cout << " ";                                                             \\
    else                                                                       \\
      cout << endl;                                                            \\
  }
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v)                                                              \\
  sort(ALL(v));                                                                \\
  v.erase(unique(ALL(v)), v.end());
#define pb push_back

string s, t;
vector<int> _s;
vector<int> _t;
ll m, n;
vector<pair<int, int>> hsh(15600000, P(-1, -1));
ll idx = 0;
const ll base = 100007LL;
const ll mod1 = 1000000007LL, mod2 = 1000000009LL;
vector<ll> fact1(300030, 0);
vector<ll> fact2(300030, 0);
ll rolling1 = 0, rolling2 = 0;

void fill_fact() {
  fact1[0] = 1;
  fact2[0] = 1;
  FOR(i, 1, 300030) {
    fact1[i] = (fact1[i - 1] * base);
    fact1[i] %= mod1;
    fact2[i] = (fact2[i - 1] * base);
    fact2[i] %= mod2;
  }
}

void fill_hash() {
  REP(i, n) {
    rolling1 *= base;
    rolling2 *= base;
    rolling1 += _t[i];
    rolling2 += _t[i];
    rolling1 %= mod1;
    rolling2 %= mod2;
  }
  REP(i, n) {
    rolling1 -= fact1[n - 1 - i] * _t[i];
    rolling1 = (rolling1 % mod1 + mod1) % mod1;
    rolling2 -= fact2[n - 1 - i] * _t[i];
    rolling2 = (rolling2 % mod2 + mod2) % mod2;
    for (ll j = 1; j <= 52; j++) {
      rolling1 += fact1[n - 1 - i] * j;
      rolling1 %= mod1;
      rolling2 += fact2[n - 1 - i] * j;
      rolling2 %= mod2;
      hsh[idx] = pair<int, int>(rolling1, rolling2);
      idx++;
      rolling1 -= fact1[n - 1 - i] * j;
      rolling1 = (rolling1 % mod1 + mod1) % mod1;
      rolling2 -= fact2[n - 1 - i] * j;
      rolling2 = (rolling2 % mod2 + mod2) % mod2;
    }
    rolling1 += fact1[n - 1 - i] * _t[i];
    rolling1 = (rolling1 % mod1 + mod1) % mod1;
    rolling2 += fact2[n - 1 - i] * _t[i];
    rolling2 = (rolling2 % mod2 + mod2) % mod2;
  }
  UNIQUE(hsh);
  auto ite = lower_bound(ALL(hsh), pair<int, int>(rolling1, rolling2));
  hsh.erase(ite);
}

ll solve() {
  fill_fact();
  fill_hash();
  ll r1 = 0, r2 = 0;
  ll ret = 0;
  for (ll i = 0; i < m - n + 1; i++) {
    if (i == 0) {
      REP(j, n) {
        r1 *= base;
        r2 *= base;
        r1 += _s[j];
        r2 += _s[j];
        r1 %= mod1;
        r2 %= mod2;
      }
    } else {
      r1 *= base;
      r2 *= base;
      r1 += _s[n - 1 + i];
      r2 += _s[n - 1 + i];
      r1 -= fact1[n] * _s[i - 1];
      r2 -= fact2[n] * _s[i - 1];
      r1 = (r1 % mod1 + mod1) % mod1;
      r2 = (r2 % mod2 + mod2) % mod2;
    }
    auto ite = lower_bound(ALL(hsh), pair<int, int>(r1, r2));
    if (*ite == pair<int, int>(r1, r2))
      ret++;
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> s >> t;
  m = s.size();
  n = t.size();
  _s.assign(m, 0);
  _t.assign(n, 0);
  REP(i, m) {
    if (s[i] >= 'A' && s[i] <= 'Z')
      _s[i] = 27 + (s[i] - 'A');
    else
      _s[i] = 1 + (s[i] - 'a');
  }

  REP(i, n) {
    if (t[i] >= 'A' && t[i] <= 'Z')
      _t[i] = 27 + (t[i] - 'A');
    else
      _t[i] = 1 + (t[i] - 'a');
  }
  cout << solve() << endl;
}