alexjercan/bugnet huggingface dataset:
Problem p01724 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INF 100
using namespace std;
const int w = 15;
const int h = 19;
typedef pair<int, int> P;
vector<string> mp(h);
map<P, int> M;
bool inmp(int x, int y) { return 0 <= x && 0 <= y && x < w && y < h; }
bool goal(int x, int y) { return (y == h || (y == h - 1 && 0 <= x && x < w)); }

int mem[1 << 20], used[1 << 20];
int dfs(int x, int y, int bit) {
  if (goal(x, y))
    return 0;
  if (!inmp(x, y))
    return INF;
  if (used[bit]++)
    return mem[bit];

  int res = INF;
  for (int i = -1; i <= 1; i++)
    for (int j = -1; j <= 1; j++) {
      int nx = x + i;
      int ny = y + j;
      int nbit = bit;
      if (!inmp(nx, ny) || mp[ny][nx] != 'X')
        continue;

      vector<string> tmp = mp;
      while (inmp(nx, ny) && mp[ny][nx] == 'X') {
        mp[ny][nx] = '.';
        nbit |= 1 << M[P(nx, ny)];
        nx += i, ny += j;
      }
      res = min(res, 1 + dfs(nx, ny, nbit));
      mp = tmp;
    }
  return mem[bit] = res;
}

int main() {

  for (int i = 0; i < h; i++)
    cin >> mp[i];

  for (int i = 0, c = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (mp[i][j] == 'X')
        M[P(j, i)] = c++;

  int ans;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (mp[i][j] == 'O')
        ans = dfs(j, i, 0);
  if (ans == INF)
    ans = -1;
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int w = 15;
const int h = 19;
vector<string> mp(h);

bool inmp(int x, int y) { return 0 <= x && 0 <= y && x < w && y < h; }

int ans = 1e9;
void dfs(int x, int y, int cost) {
  if (y == h || (y == h - 1 && 0 <= x && x < w))
    ans = min(ans, cost);
  if (!inmp(x, y) || ans <= cost)
    return;

  for (int i = -1; i <= 1; i++)
    for (int j = -1; j <= 1; j++) {
      int nx = x + i;
      int ny = y + j;
      if (!inmp(nx, ny) || mp[ny][nx] != 'X')
        continue;
      vector<string> tmp = mp;
      while (inmp(nx, ny) && mp[ny][nx] == 'X')
        mp[ny][nx] = '.', nx += i, ny += j;
      dfs(nx, ny, cost + 1);
      mp = tmp;
    }
}

int main() {

  for (int i = 0; i < h; i++)
    cin >> mp[i];

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (mp[i][j] == 'O')
        dfs(j, i, 0);

  if (ans == 1e9)
    ans = -1;
  cout << ans << endl;

  return 0;
}
Problem p01724 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define INF (1e9)
#define N 19
#define M 15
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
int sy, sx, cnt;
map<P1, int> memo;
map<P, int> num;
string s[N];
int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

int dfs(int A, int y, int x) {
  if (memo.count(P1(A, P(y, x))))
    return memo[P1(A, P(y, x))];
  if ((N - 1 == y && 0 <= x && x < M) || N <= y)
    return 1;
  if (y < 0 || x < 0 || N <= y || M <= x)
    return INF;
  int res = INF;
  for (int i = 0; i < 8; i++) {
    int ny = y + dy[i], nx = x + dx[i], nA = A;
    if (ny < 0 || nx < 0 || N <= ny || M <= nx || s[ny][nx] != 'X')
      continue;
    while (0 <= ny && ny < N && 0 <= nx && nx < M && s[ny][nx] == 'X') {
      nA |= (1 << num[P(ny, nx)]);
      s[ny][nx] = '.';
      ny += dy[i];
      nx += dx[i];
    }
    res = min(res, dfs(nA, ny, nx));
    while (ny != y || nx != x) {
      ny -= dy[i];
      nx -= dx[i];
      s[ny][nx] = 'X';
    }
    s[y][x] = '.';
  }
  return memo[P1(A, P(y, x))] = res + 1;
}

int main() {
  for (int i = 0; i < N; i++) {
    cin >> s[i];
    for (int j = 0; j < M; j++) {
      if (s[i][j] == 'O') {
        sy = i, sx = j;
        s[i][j] = '.';
      }
      if (s[i][j] == 'X')
        num[P(i, j)] = cnt++;
    }
  }
  int ans = dfs(0, sy, sx);
  cout << (ans >= INF ? -1 : ans - 1) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define INF (1e9)
#define N 19
#define M 15
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
int sy, sx, cnt;
map<P1, int> memo;
map<P, int> num;
string s[N];
int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

int dfs(int A, int y, int x) {
  if (memo.count(P1(A, P(y, x))))
    return memo[P1(A, P(y, x))];
  if (N - 1 <= y)
    return 1;
  if (y < 0 || x < 0 || N <= y || M <= x)
    return INF;
  int res = INF;
  memo[P1(A, P(y, x))] = INF;
  for (int i = 0; i < 8; i++) {
    int ny = y + dy[i], nx = x + dx[i], nA = A;
    if (s[ny][nx] != 'X')
      continue;
    while (0 <= ny && ny < N && 0 <= nx && nx < M && s[ny][nx] == 'X') {
      nA |= (1 << num[P(ny, nx)]);
      s[ny][nx] = '.';
      ny += dy[i];
      nx += dx[i];
    }
    res = min(res, dfs(nA, ny, nx));
    while (ny != y || nx != x) {
      ny -= dy[i];
      nx -= dx[i];
      s[ny][nx] = 'X';
    }
  }
  return memo[P1(A, P(y, x))] = res + 1;
}

int main() {
  for (int i = 0; i < N; i++) {
    cin >> s[i];
    for (int j = 0; j < M; j++) {
      if (s[i][j] == 'O') {
        sy = i, sx = j;
        s[i][j] = '.';
      }
      if (s[i][j] == 'X')
        num[P(i, j)] = cnt++;
    }
  }
  int ans = dfs(0, sy, sx);
  cout << (ans == INF + 1 ? -1 : ans - 1) << endl;
  return 0;
}
Problem p01725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long int64;

string S, T = "+-*";
int ts[3], idx;

int64 latte();

int64 calc(int64 a, char b, int64 c) {
  if (b == '+')
    return (a + c);
  if (b == '-')
    return (a - c);
  return (a * c);
}

int64 ei() {
  if (S[idx] == '(') {
    ++idx;
    int64 ret = latte();
    ++idx;
    return (ret);
  } else {
    int64 ret = 0;
    while (idx < S.size() && isdigit(S[idx])) {
      ret = ret * 10 + S[idx++] - '0';
    }
    return (ret);
  }
}

int64 beet() {
  int64 ret = ei();
  while (idx < S.size() && T.find(S[idx]) != string::npos &&
         ts[T.find(S[idx])] == 2) {
    char op = S[idx++];
    auto get = ei();
    ret = calc(ret, op, get);
  }
  return (ret);
}

int64 malta() {
  int64 ret = beet();
  while (idx < S.size() && T.find(S[idx]) != string::npos &&
         ts[T.find(S[idx])] == 1) {
    char op = S[idx++];
    auto get = beet();
    ret = calc(ret, op, get);
  }
  return (ret);
}

int64 latte() {
  int64 ret = malta();
  while (idx < S.size() && T.find(S[idx]) != string::npos &&
         ts[T.find(S[idx])] == 0) {
    char op = S[idx++];
    auto get = malta();
    ret = calc(ret, op, get);
  }
  return (ret);
}

int main() {
  cin >> S;
  int64 ret = numeric_limits<int64>::min();
  for (ts[0] = 0; ts[0] < 3; ts[0]++) {
    for (ts[1] = 0; ts[1] < 3; ts[1]++) {
      for (ts[2] = 0; ts[2] < 3; ts[2]++) {
        idx = 0;
        ret = max(ret, latte());
      }
    }
  }
  cout << ret << endl;
}

Error: 0
-
*
-
-
*
*
-
*
-
-
*
*
-
*
-
-
*
*


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int64;

string S, T = "+-*";
int s[3], idx;

int64 latte();

int64 calc(int64 a, char b, int64 c) {
  if (b == '+')
    return (a + c);
  if (b == '-')
    return (a - c);
  return (a * c);
}

int64 ei() {
  if (S[idx] == '(') {
    ++idx;
    int64 ret = latte();
    ++idx;
    return (ret);
  } else {
    int64 ret = 0;
    while (idx < S.size() && isdigit(S[idx])) {
      ret = ret * 10 + S[idx++] - '0';
    }
    return (ret);
  }
}

int64 beet() {
  int64 ret = ei();
  while (idx < S.size() && s[T.find(S[idx])] == 2) {
    char op = S[idx++];
    ret = calc(ret, op, ei());
  }
  return (ret);
}

int64 malta() {
  int64 ret = beet();
  while (idx < S.size() && s[T.find(S[idx])] == 1) {
    char op = S[idx++];
    ret = calc(ret, op, beet());
  }
  return (ret);
}

int64 latte() {
  int64 ret = malta();
  while (idx < S.size() && s[T.find(S[idx])] == 0) {
    char op = S[idx++];
    cerr << op << endl;
    ret = calc(ret, op, malta());
  }
  return (ret);
}

int main() {
  cin >> S;
  int64 ret = numeric_limits<int64>::min();
  for (s[0] = 0; s[0] < 3; s[0]++) {
    for (s[1] = 0; s[1] < 3; s[1]++) {
      for (s[2] = 0; s[2] < 3; s[2]++) {
        idx = 0;
        ret = max(ret, latte());
      }
    }
  }
  cout << ret << endl;
}
Problem p01726 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
int A[610000], B[610000];
void calcZ(string s, int *A) {
  A[0] = s.size();
  int i = 1, j = 0;
  while (i < s.size()) {
    while (i + j < s.size() && s[j] == s[i + j])
      ++j;
    A[i] = j;
    if (j == 0) {
      ++i;
      continue;
    }
    int k = 1;
    while (i + k < s.size() && k + A[k] < j)
      A[i + k] = A[k], ++k;
    i += k, j -= k;
  }
}
int main() {
  string s, t;
  cin >> s >> t;
  calcZ(t + "$" + s, A);
  reverse(all(s));
  reverse(all(t));
  calcZ(t + "$" + s, B);
  int N = s.size(), K = t.size();
  int ans = 0;
  rep(i, N - K + 1) {
    //		show(A[K+1+i]);
    //		show(B[K+1+(N-K-i)]);
    int v = A[K + 1 + i] + B[K + 1 + (N - K - i)];
    if (v == K - 1)
      ans++;
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll B = 1e8 + 7;
vector<int> hs;
ull us[300000 * 50];
int I = 0;
bool ok[300000];
string s, t;
int N, K;
void solveu() {
  ull ex = 1, exh = 1;
  rep(i, K) exh = exh * B;
  rep(i, N) {
    for (int j = -25; j <= 25; j++) {
      if (j == 0)
        continue;
      ull x = ex * j;
      us[I++] = x;
    }
    ex = ex * B;
  }
  sort(us, us + I);
  ull H = 0;
  rep(i, K) H = H * B + t[i];
  ull h = 0;
  rep(i, K) h = h * B + s[i];
  rep(i, N - K + 1) {
    ull d = H - h;
    if (!binary_search(us, us + I, d))
      ok[i] = 0;
    h = h * B - exh * s[i] + s[i + K];
  }
}
void solve(ll mod) {
  ll ex = 1, exh = 1;
  rep(i, K) exh = exh * B % mod;
  rep(i, N) {
    for (int j = -25; j <= 25; j++) {
      if (j == 0)
        continue;
      ll x = ex * j % mod;
      if (x < 0)
        x += mod;
      hs.pb(x);
    }
    ex = ex * B % mod;
  }
  sort(all(hs));
  ll H = 0;
  rep(i, K) H = (H * B + t[i]) % mod;
  ll h = 0;
  rep(i, K) h = (h * B + s[i]) % mod;
  rep(i, N - K + 1) {
    ll d = (H - h) % mod;
    if (d < 0)
      d += mod;
    if (!binary_search(all(hs), d))
      ok[i] = 0;
    h = h * B - exh * s[i] + s[i + K];
    h %= mod;
    if (h < 0)
      h += mod;
  }
}
int main() {
  cin >> s >> t;
  N = s.size(), K = t.size();
  rep(i, N - K + 1) ok[i] = 1;
  int mods[3] = {(int)1e9 + 7, (int)1e9 + 9, 1234567891};
  solveu();
  rep(i, 3) {
    hs.clear();
    solve(mods[i]);
  }
  int ans = 0;
  rep(i, N - K + 1) if (ok[i]) ans++;
  cout << ans << endl;
}
Problem p01726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

typedef pair<int, int> Pi;
typedef tuple<int, int, int> Ti;
typedef vector<int> vint;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

vector<int> Zalgorithm(string S) {
  vector<int> A(S.size());
  A[0] = S.size();
  int i = 1, j = 0;
  while (i < (int)S.size()) {
    while (i + j < (int)S.size() && S[j] == S[i + j])
      j++;
    A[i] = j;
    if (j == 0) {
      i++;
      continue;
    }
    int k = 1;
    while (i + k < (int)S.size() && k + A[k] < j)
      A[i + k] = A[k], k++;
    i += k, j -= k;
  }
  return A;
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  string S, T;
  cin >> S >> T;
  int n = S.size(), m = T.size();
  string rS = S, rT = T;
  reverse(all(rS));
  reverse(all(rT));
  string Z = T + S, rZ = rT + rS;
  auto A = Zalgorithm(Z);
  auto rA = Zalgorithm(rZ);

  int ans = 0;
  rep(i, n - m + 1) {
    if (n - i - m < 0)
      continue;
    if (min(m, A[i + m]) + min(m, rA[n - i]) == m - 1)
      ans++;
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

typedef pair<int, int> Pi;
typedef tuple<int, int, int> Ti;
typedef vector<int> vint;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

vector<int> Zalgorithm(string S) {
  vector<int> A(S.size());
  A[0] = S.size();
  int i = 1, j = 0;
  while (i < (int)S.size()) {
    while (i + j < (int)S.size() && S[j] == S[i + j])
      j++;
    A[i] = j;
    if (j == 0) {
      i++;
      continue;
    }
    int k = 1;
    while (i + k < (int)S.size() && k + A[k] < j)
      A[i + k] = A[k], k++;
    i += k, j -= k;
  }
  return A;
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  string S, T;
  cin >> S >> T;
  int n = S.size(), m = T.size();
  string rS = S, rT = T;
  reverse(all(rS));
  reverse(all(rT));
  string Z = T + S, rZ = rT + rS;
  auto A = Zalgorithm(Z);
  auto rA = Zalgorithm(rZ);
  auto lcp = [&](vector<int> v, int x) { return min(m, v[x + m]); };
  int ans = 0;
  rep(i, n - m + 1) {
    if (lcp(A, i) + lcp(rA, n - i - m) == m - 1)
      ans++;
  }
  cout << ans << endl;

  return 0;
}
Problem p01726 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define K 971
using namespace std;
typedef unsigned long long ull;

string s, t;
map<ull, int> cnt;
ull d[300005], H[300005];

signed main() {

  cin >> s >> t;

  ull h = 0, x = 1;

  for (int i = 0; i < t.size() - 1; i++)
    x = x * K;

  for (int i = 0; i < s.size(); i++) {
    h = h * K + s[i];
    if (i >= t.size() - 1) {
      cnt[h]++;
      h = h - s[i - (t.size() - 1)] * x;
    }
  }

  d[0] = 1;

  for (int i = 1; i <= t.size(); i++)
    d[i] = d[i - 1] * K;

  h = 0;

  for (int i = 0; i < t.size(); i++) {
    h = h * K + t[i];
    H[i] = h;
  }

  int ans = 0;

  for (int i = 0; i < t.size(); i++) {
    for (int j = 'a'; j <= 'z'; j++) {
      if ((char)j == t[i])
        continue;
      ull hash = j;
      if (i != 0)
        hash = H[i - 1] * d[1] + hash;
      if (i != t.size() - 1)
        hash = hash * d[t.size() - i - 1] + H[t.size() - 1] -
               H[i] * d[t.size() - i - 1];

      if (cnt.count(hash))
        ans += cnt[hash];
    }
    for (int j = 'A'; j <= 'Z'; j++) {
      if ((char)j == t[i])
        continue;
      ull hash = j;
      if (i != 0)
        hash = H[i - 1] * d[1] + hash;
      if (i != t.size() - 1)
        hash = hash * d[t.size() - i - 1] + H[t.size() - 1] -
               H[i] * d[t.size() - i - 1];

      if (cnt.count(hash))
        ans += cnt[hash];
    }
  }

  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define K 443
#define M 1000000007
using namespace std;
typedef long long ll;

string s, t;
map<int, int> cnt;
int d[300005], H[300005];

signed main() {

  cin >> s >> t;

  int h = 0, x = 1;

  for (int i = 0; i < t.size() - 1; i++)
    x = (ll)x * K % M;

  for (int i = 0; i < s.size(); i++) {
    h = ((ll)h * K + s[i]) % M;
    if (i >= t.size() - 1) {
      cnt[h]++;
      h = (h - (ll)s[i - (t.size() - 1)] * x % M + M) % M;
    }
  }

  d[0] = 1;

  for (int i = 1; i <= t.size(); i++)
    d[i] = ((ll)d[i - 1] * K) % M;

  h = 0;

  for (int i = 0; i < t.size(); i++) {
    h = ((ll)h * K + t[i]) % M;
    H[i] = h;
  }

  int ans = 0;

  for (int i = 0; i < t.size(); i++) {
    for (int j = 'a'; j <= 'z'; j++) {
      if ((char)j == t[i])
        continue;
      int hash = j;
      if (i != 0)
        hash = ((ll)H[i - 1] * d[1] + hash) % M;
      if (i != t.size() - 1)
        hash =
            ((ll)hash * d[t.size() - i - 1] % M +
             (H[t.size() - 1] - (ll)H[i] * d[t.size() - i - 1] % M + M) % M) %
            M;

      ans += cnt[hash];
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p01726 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ull = unsigned __int128;

const ull B = 100000007;

vector<ull> rang;
int res;

ull Pow(ull a, ull b) {
  ull aa = a, ans = 1;
  for (ull i = 0; i < (ull)64; i++) {
    if ((b >> i) & (ull)1) {
      ans *= aa;
    }
    aa *= aa;
  }
  return ans;
}

void Rolling_Hash(int al, const string &b) {
  int bl = b.length();
  if (al > bl)
    return;

  ull t = 1, bh = 0;
  for (int i = 0; i < al; i++)
    t *= B;
  for (int i = 0; i < al; i++)
    bh = bh * B + b[i];
  for (int i = 0; i + al <= bl; i++) {
    if (binary_search(rang.begin(), rang.end(), bh)) {
      res++;
    }
    if (i + al < bl)
      bh = bh * B + b[i + al] - b[i] * t;
  }
}

int main() {
  string S, T;
  ull Th = 0;
  cin >> S >> T;
  rang.reserve(52 * T.size());
  for (ull i = 0; i < T.size(); i++)
    Th = Th * B + T[i];
  for (ull i = T.size() - 1, kei = 1;; i--, kei *= B) {
    for (int j = 'a'; j <= 'z'; j++) {
      if (j != T[i]) {
        rang.push_back(Th + (j - T[i]) * kei);
      }
    }
    for (int j = 'A'; j <= 'Z'; j++) {
      if (j != T[i]) {
        rang.push_back(Th + (j - T[i]) * kei);
      }
    }
    if (i == 0)
      break;
  }
  sort(rang.begin(), rang.end());
  Rolling_Hash(T.size(), S);
  cout << res << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ull = unsigned __int128;

const ull B = 100000007;

vector<ull> rang;
int res;

ull Pow(ull a, ull b) {
  ull aa = a, ans = 1;
  for (ull i = 0; i < (ull)128; i++) {
    if ((b >> i) & (ull)1) {
      ans *= aa;
    }
    aa *= aa;
  }
  return ans;
}

void Rolling_Hash(const string &a, const string &b) {
  int al = a.length(), bl = b.length();
  if (al > bl)
    return;

  ull t = 1, ah = 0, bh = 0;
  for (int i = 0; i < al; i++)
    t *= B;
  for (int i = 0; i < al; i++)
    ah = ah * B + a[i];
  for (int i = 0; i < al; i++)
    bh = bh * B + b[i];
  for (int i = 0; i + al <= bl; i++) {
    if (binary_search(rang.begin(), rang.end(), ah - bh)) {
      res++;
    }
    if (i + al < bl)
      bh = bh * B + b[i + al] - b[i] * t;
  }
}

int main() {
  string S, T;
  cin >> S >> T;
  for (ull i = 0; i < T.size(); i++) {
    ull kei = Pow(B, i);
    for (int j = 'a' - 'z'; j <= 'z' - 'a'; j++) {
      if (j)
        rang.push_back(kei * j);
    }
    for (int j = 'A'; j <= 'Z'; j++) {
      for (int k = 'a'; k <= 'z'; k++) {
        rang.push_back(kei * (j - k));
        rang.push_back(kei * (k - j));
      }
    }
  }
  sort(rang.begin(), rang.end());
  rang.erase(unique(rang.begin(), rang.end()), rang.end());
  Rolling_Hash(T, S);
  cout << res << endl;
  return 0;
}
Problem p01726 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cctype>
#include <iostream>
#include <vector>

using namespace std;

using ull = unsigned long long;
constexpr ull B = 53173ULL;

vector<ull> rolling_hash(const string &s, int N, vector<ull> &p) {
  vector<ull> _hash(N + 1);
  p.resize(N + 1);
  p[0] = 1;
  for (int i = 0; i < N; i++) {
    _hash[i + 1] = _hash[i] * B + s[i];
    p[i + 1] = p[i] * B;
  }
  return _hash;
}

bool check(const vector<ull> &v, ull x) {
  return binary_search(v.begin(), v.end(), x);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string S, T;
  cin >> S >> T;

  int s = S.size(), t = T.size();

  vector<ull> p;
  auto ht = rolling_hash(T, t, p);

  auto gen_ch = [](int x) -> char {
    if (x < 26)
      return (char)('a' + x);
    x -= 26;
    return (char)('A' + x);
  };

  vector<ull> v;
  for (int i = 0; i < t; i++) {
    for (int j = 0; j < 52; j++) {
      char ch = gen_ch(j);
      if (ch == T[i])
        continue;
      auto hv = ht[t] + (ch - T[i]) * p[t - i - 1];
      v.emplace_back(hv);
    }
  }

  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());

  auto hs = rolling_hash(S, s, p);

  int res = 0;
  for (int i = 0; i <= s - t; i++) {

    if (check(v, hs[i + t] - hs[i] * p[t])) {
      ++res;
    }
  }
  cout << res << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <cctype>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

using ull = unsigned long long;
constexpr ull B = 10000007ULL;

vector<ull> rolling_hash(const string &s, int N, vector<ull> &p) {
  vector<ull> _hash(N + 1);
  p.resize(N + 1);
  p[0] = 1;
  for (int i = 0; i < N; i++) {
    _hash[i + 1] = _hash[i] * B + s[i];
    p[i + 1] = p[i] * B;
  }
  return _hash;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string S, T;
  cin >> S >> T;

  int s = S.size(), t = T.size();

  vector<ull> p;
  auto ht = rolling_hash(T, t, p);

  auto gen_ch = [](int x) -> char {
    if (x < 26)
      return (char)('a' + x);
    x -= 26;
    return (char)('A' + x);
  };

  set<ull> st;
  for (int i = 0; i < t; i++) {
    for (int j = 0; j < 52; j++) {
      char ch = gen_ch(j);
      if (ch == T[i])
        continue;
      auto hv = ht[t] - T[i] * p[i] + ch * p[i];
      st.insert(hv);
    }
  }

  auto hs = rolling_hash(S, s, p);

  int res = 0;
  for (int i = 0; i <= s - t; i++) {
    res += st.count(hs[i + t] - hs[i] * p[t]) > 0;
  }
  cout << res << endl;
  return 0;
}
Problem p01726 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ulli;
const int MAXN = 300005;
const ulli B = 777737377ULL;
ulli Bn[MAXN];

int main() {
  Bn[0] = 1;
  for (int i = 1; i < MAXN; ++i)
    Bn[i] = Bn[i - 1] * B;
  for (string S, T; cin >> S >> T;) {
    map<ulli, ulli> num;
    ulli hash = 0;
    for (int i = 0; i < S.size(); ++i) {
      hash = hash * B + S[i];
      if (i >= T.size())
        hash -= S[i - T.size()] * Bn[T.size()];
      if (i + 1 >= T.size())
        num[hash]++;
    }
    hash = 0;
    for (int i = 0; i < T.size(); ++i) {
      hash = hash * B + T[i];
    }
    ulli res = 0;
    for (int i = 0; i < T.size(); ++i) {
      for (char c = 'A'; c <= 'z'; ++c) {
        if (!isalpha(c))
          continue;
        if (c == T[i])
          continue;
        ulli nhash = hash;
        nhash += c * Bn[T.size() - i - 1];
        nhash -= T[i] * Bn[T.size() - i - 1];
        if (num.count(nhash)) {
          res += num[nhash];
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ulli;
const int MAXN = 300005;
const ulli B = 1000000007ULL;
ulli Bn[MAXN];

int main() {
  Bn[0] = 1;
  for (int i = 1; i < MAXN; ++i)
    Bn[i] = Bn[i - 1] * B;
  for (string S, T; cin >> S >> T;) {
    map<ulli, ulli> num;
    ulli hash = 0;
    for (int i = 0; i < S.size(); ++i) {
      hash = hash * B + S[i];
      if (i >= T.size())
        hash -= S[i - T.size()] * Bn[T.size()];
      if (i + 1 >= T.size())
        num[hash]++;
    }
    hash = 0;
    for (int i = 0; i < T.size(); ++i) {
      hash = hash * B + T[i];
    }
    ulli res = 0;
    for (int i = 0; i < T.size(); ++i) {
      for (char c = 'a'; c <= 'z'; ++c) {
        if (c == T[i])
          continue;
        res += num[hash + (c - T[i]) * Bn[T.size() - i - 1]];
      }
    }
    cout << res << endl;
  }
  return 0;
}