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

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

using ull = unsigned long long;
constexpr int MAX = 4000;
constexpr ull B = 100000007ULL;

vector<vector<int>> make(const string &str) {
  int N = str.size();
  vector<vector<int>> cnt(N + 1, vector<int>(26));
  for (int i = 0; i < N; i++) {
    cnt[i + 1][str[i] - 'a']++;
    for (int j = 0; j < 26; j++) {
      cnt[i + 1][j] += cnt[i][j];
    }
  }
  return cnt;
}

ull get_hash(int len, int s, const vector<vector<int>> &cnt) {
  auto l = cnt[s];
  auto r = cnt[s + len];

  ull res = 1;
  for (int i = 0; i < 26; i++) {
    res = res * B + (r[i] - l[i]);
  }
  return res;
}

int main() {
  string S, T;
  cin >> S >> T;

  if (S.size() < T.size())
    swap(S, T);

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

  auto sc = make(S);
  auto tc = make(T);

  vector<ull> vec[MAX + 1];
  for (int i = s; i >= 1; i--) {
    for (int j = 0; j <= s - i; j++) {
      vec[i].emplace_back(get_hash(i, j, sc));
    }
    sort(vec[i].begin(), vec[i].end());
    vec[i].erase(unique(vec[i].begin(), vec[i].end()), vec[i].end());
  }

  int res = 0;
  for (int i = t; i >= 1; i--) {
    for (int j = 0; j <= t - i; j++) {
      if (binary_search(vec[i].begin(), vec[i].end(), get_hash(i, j, tc))) {
        res = max(res, i);
      }
    }
  }
  cout << res << endl;
  return 0;
}

Error: MLE

Fixed submission:

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

using namespace std;

using ull = unsigned long long;
constexpr int MAX = 4000;
constexpr ull B = 100000007ULL;

vector<vector<int>> make(const string &str) {
  int N = str.size();
  vector<vector<int>> cnt(N + 1, vector<int>(26));
  for (int i = 0; i < N; i++) {
    cnt[i + 1][str[i] - 'a']++;
    for (int j = 0; j < 26; j++) {
      cnt[i + 1][j] += cnt[i][j];
    }
  }
  return cnt;
}

ull get_hash(int len, int s, const vector<vector<int>> &cnt) {
  auto l = cnt[s];
  auto r = cnt[s + len];

  ull res = 1;
  for (int i = 0; i < 26; i++) {
    res = res * B + (r[i] - l[i]);
  }
  return res;
}

int main() {
  string S, T;
  cin >> S >> T;

  if (S.size() < T.size())
    swap(S, T);

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

  auto sc = make(S);
  auto tc = make(T);

  set<ull> st[MAX + 1];
  for (int i = s; i >= 1; i--) {
    for (int j = 0; j <= s - i; j++) {
      st[i].insert(get_hash(i, j, sc));
    }
  }

  int res = 0;
  for (int i = t; i >= 1; i--) {
    for (int j = 0; j <= t - i; j++) {
      if (st[i].count(get_hash(i, j, tc)) > 0) {
        res = max(res, i);
      }
    }
  }
  cout << res << endl;
  return 0;
}
Problem p00949 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
long long mod = 177777777777777, power[300], cnt;
string S, T;
vector<long long> U1[4400], U2[4400];
void solve(string S1, int r) {
  for (int i = 0; i < S1.size(); i++) {
    long long ret = 0;
    for (int j = i; j < S1.size(); j++) {
      ret += power[S1[j]];
      ret %= mod;
      if (r == 1)
        U1[j - i + 1].push_back(ret);
      if (r == 2)
        U2[j - i + 1].push_back(ret);
    }
  }
}
int main() {
  cin >> S >> T;
  power[0] = 1;
  for (int i = 1; i < 300; i++)
    power[i] = power[i - 1] * 3141LL % mod;
  solve(S, 1);
  solve(T, 2);
  int maxn = 0;
  for (int i = 1; i < 4400; i++) {
    cnt = 0;
    sort(U2[i].begin(), U2[i].end());
    for (int j = 0; j < U1[i].size(); j++) {
      int pos1 =
          lower_bound(U2[i].begin(), U2[i].end(), U1[i][j]) - U2[i].begin();
      int pos2 =
          lower_bound(U2[i].begin(), U2[i].end(), U1[i][j] + 1) - U2[i].begin();
      cnt += (long long)(pos2 - pos1);
    }
    if (cnt >= 1) {
      maxn = max(maxn, i);
    }
  }
  cout << maxn << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
long long mod = 177777777777777, power[300], cnt;
string S, T;
vector<long long> U1[4400], U2[4400];
vector<vector<long long>> solve(string S1) {
  vector<vector<long long>> R(4400);
  for (int i = 0; i < S1.size(); i++) {
    long long ret = 0;
    for (int j = i; j < S1.size(); j++) {
      ret += power[S1[j]];
      ret %= mod;
      R[j - i + 1].push_back(ret);
    }
  }
  return R;
}
int main() {
  cin >> S >> T;
  power[0] = 1;
  for (int i = 1; i < 300; i++)
    power[i] = power[i - 1] * 3141LL % mod;
  vector<vector<long long>> Y1 = solve(S), Y2 = solve(T);
  int maxn = 0;
  for (int i = 0; i < 4400; i++) {
    U1[i] = Y1[i];
    U2[i] = Y2[i];
  }
  for (int i = 1; i < 4400; i++) {
    cnt = 0;
    sort(U2[i].begin(), U2[i].end());
    for (int j = 0; j < U1[i].size(); j++) {
      int pos1 =
          lower_bound(U2[i].begin(), U2[i].end(), U1[i][j]) - U2[i].begin();
      int pos2 =
          lower_bound(U2[i].begin(), U2[i].end(), U1[i][j] + 1) - U2[i].begin();
      cnt += (long long)(pos2 - pos1);
    }
    if (cnt >= 1) {
      maxn = max(maxn, i);
    }
  }
  cout << maxn << endl;
  return 0;
}
Problem p00949 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define int long long
using namespace std;

string s[2];
int cnt[2][4001][26];
vector<vector<int>> dict;

//[l, r)
vector<int> getArray(int type, int l, int r) {
  vector<int> ret(26);
  for (int i = 0; i < 26; i++) {
    ret[i] = cnt[type][r][i] - cnt[type][l][i];
  }
  return ret;
}

signed main() {
  int l, r;

  cin >> s[0] >> s[1];
  for (int i = 0; i < s[0].length(); i++)
    cnt[0][i + 1][s[0][i] - 'a']++;
  for (int i = 0; i < s[1].length(); i++)
    cnt[1][i + 1][s[1][i] - 'a']++;
  for (int i = 1; i <= s[0].length(); i++)
    for (int j = 0; j < 26; j++)
      cnt[0][i][j] += cnt[0][i - 1][j];
  for (int i = 1; i <= s[1].length(); i++)
    for (int j = 0; j < 26; j++)
      cnt[1][i][j] += cnt[1][i - 1][j];

  for (int length = min(s[0].length(), s[1].length()); length > 0; length--) {
    dict.clear();
    for (l = 0; l < s[1].length(); l++) {
      r = l + length;
      if (r > s[1].length())
        break;
      dict.push_back(getArray(1, l, r));
    }
    sort(dict.begin(), dict.end());

    for (l = 0; l < s[0].length(); l++) {
      r = l + length;
      if (r > s[0].length())
        break;

      vector<int> a = getArray(0, l, r);
      if (*lower_bound(dict.begin(), dict.end(), a) == a) {
        cout << length << endl;
        return 0;
      }
    }
  }

  cout << 0 << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_set>
#define int long long
using namespace std;

string s[2];
int cnt[2][4001][26];
unordered_set<int> dict;

//[l, r]
int getHash(int type, int l, int r) {
  const int base = 330983239;
  const int mod = 1000000009;
  int mul = 1;
  int ret = 0;

  for (int i = 0; i < 26; i++) {
    ret += mul * (cnt[type][r + 1][i] - cnt[type][l][i]);
    mul *= base;
    mul %= mod;
  }
  ret %= mod;
  return ret;
}

signed main() {
  int l, r;

  cin >> s[0] >> s[1];
  for (int i = 0; i < s[0].length(); i++)
    cnt[0][i + 1][s[0][i] - 'a']++;
  for (int i = 0; i < s[1].length(); i++)
    cnt[1][i + 1][s[1][i] - 'a']++;
  for (int i = 1; i <= s[0].length(); i++)
    for (int j = 0; j < 26; j++)
      cnt[0][i][j] += cnt[0][i - 1][j];
  for (int i = 1; i <= s[1].length(); i++)
    for (int j = 0; j < 26; j++)
      cnt[1][i][j] += cnt[1][i - 1][j];

  for (l = 0; l < s[1].length(); l++) {
    for (r = l; r < s[1].length(); r++) {
      dict.insert(getHash(1, l, r));
    }
  }

  int ans = 0;
  for (l = 0; l < s[0].length(); l++) {
    for (r = l; r < s[0].length(); r++) {
      if (dict.find(getHash(0, l, r)) != dict.end()) {
        ans = max(ans, r - l + 1);
      }
    }
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
ull B = 5575777;
int K;

int n, m;
char a[5000];
char b[5000];
ull po[5000];

int main() {
  po[0] = 1;
  for (int i = 1; i < 500; i++)
    po[i] = po[i - 1] * B;

  scanf("%s %s", a, b);
  n = strlen(a);
  m = strlen(b);

  int ans = 0;
  for (int K = 1; K <= min(n, m); K++) {

    set<ull> st;
    ull key, d;

    key = 0, d = 1;
    for (int i = 0; i < n; i++) {
      key += po[(int)a[i]];
      if (i - K >= 0) {
        key -= po[(int)a[i - K]];
      } else
        d *= B;
      if (i >= K - 1)
        st.insert(key);
    }

    key = 0, d = 1;
    for (int i = 0; i < m; i++) {
      key += po[(int)b[i]];
      if (i - K >= 0) {
        key -= po[(int)b[i - K]];
      } else
        d *= B;

      if (i >= K - 1)
        if (st.count(key) > 0)
          ans = K;
    }
  }
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
ull B = 5575777;

ull C = 1950527;
ull D = 1777771;

int K;

int n, m;
char a[5000];
char b[5000];

int t[1950527];
ull T[1950527];

void add(ull key) {

  ull dx = key % D;

  ull x = key % C;
  while (t[x] == K) {
    if (T[x] != key)
      return;
    x = (x + dx) % C;
  }
  t[x] = K;
  T[x] = key;
}

bool check(ull key) {
  ull dx = key % D;
  ull x = key % C;
  while (t[x] == K) {
    if (T[x] == key)
      return true;
    x = (x + dx) % C;
  }
  return false;
}

ull po[500];

int main() {
  po[0] = 1;
  for (int i = 1; i < 500; i++)
    po[i] = po[i - 1] * B;

  scanf("%s %s", a, b);
  n = strlen(a);
  m = strlen(b);

  int ans = 0;

  for (K = 1; K <= min(n, m); K++) {
    ull key, d;

    key = 0, d = 1;
    for (int i = 0; i < n; i++) {
      key += po[(int)a[i]];
      if (i - K >= 0) {
        key -= po[(int)a[i - K]];
      } else
        d *= B;
      add(key);
    }

    key = 0, d = 1;
    for (int i = 0; i < m; i++) {
      key += po[(int)b[i]];
      if (i - K >= 0) {
        key -= po[(int)b[i - K]];
      } else
        d *= B;
      if (check(key))
        ans = K;
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p00949 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
  string S1, S2;
  cin >> S1 >> S2;
  int l = min(S1.length(), S2.length());
  vector<vector<int>> c1(S1.length() + 1, vector<int>(26, 0)),
      c2(S2.length() + 1, vector<int>(26, 0));
  for (int i = 0; i < S1.length(); ++i) {
    for (int j = 0; j < 26; ++j) {
      c1[i + 1][j] = c1[i][j];
    }
    ++c1[i + 1][S1[i] - 'a'];
  }
  for (int i = 0; i < S2.length(); ++i) {
    for (int j = 0; j < 26; ++j) {
      c2[i + 1][j] = c2[i][j];
    }
    ++c2[i + 1][S2[i] - 'a'];
  }
  for (int i = l; i >= 0; --i) {
  }
  for (int i = l; i >= 0; --i) {
    set<vector<int>> S;
    for (int j = i; j < S1.length(); ++j) {
      vector<int> t = c1[j + 1];
      for (int k = 0; k < 26; ++k)
        t[k] -= c1[j - i][k];
      S.insert(t);
    }
    for (int j = i; j < S2.length(); ++j) {
      vector<int> t = c2[j + 1];
      for (int k = 0; k < 26; ++k)
        t[k] -= c2[j - i][k];
      if (S.find(t) != S.end()) {
        cout << i + 1 << endl;
        return 0;
      }
    }
  }
  cout << 0 << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
  string S1, S2;
  cin >> S1 >> S2;
  int l = min(S1.length(), S2.length());
  set<vector<int>> S;
  vector<vector<int>> c1(S1.length() + 1, vector<int>(26, 0)),
      c2(S2.length() + 1, vector<int>(26, 0));
  for (int i = 0; i < S1.length(); ++i) {
    for (int j = 0; j < 26; ++j) {
      c1[i + 1][j] = c1[i][j];
    }
    ++c1[i + 1][S1[i] - 'a'];
  }
  for (int i = 0; i < S2.length(); ++i) {
    for (int j = 0; j < 26; ++j) {
      c2[i + 1][j] = c2[i][j];
    }
    ++c2[i + 1][S2[i] - 'a'];
  }
  for (int i = l; i >= 0; --i) {
    for (int j = i; j < S1.length(); ++j) {
      vector<int> t = c1[j + 1];
      for (int k = 0; k < 26; ++k)
        t[k] -= c1[j - i][k];
      S.insert(t);
    }
  }
  for (int i = l; i >= 0; --i) {
    for (int j = i; j < S2.length(); ++j) {
      vector<int> t = c2[j + 1];
      for (int k = 0; k < 26; ++k)
        t[k] -= c2[j - i][k];
      if (S.find(t) != S.end()) {
        cout << i + 1 << endl;
        return 0;
      }
    }
  }
  cout << 0 << endl;
  return 0;
}
Problem p00949 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string a, b;
  cin >> a >> b;
  int ans = 0, n = a.size(), m = b.size();

  for (int i = 1; i <= n; i++) {
    set<vector<int>> S;
    vector<int> v(26, 0);
    for (int j = 0; j < n; j++) {
      v[a[j] - 'a']++;
      if (j >= i) {
        v[a[j - i] - 'a']--;
        S.insert(v);
      }
    }
    S.insert(v);
    v = vector<int>(26, 0);
    for (int j = 0; j < m; j++) {
      if (i == j && S.count(v))
        ans = i;
      v[b[j] - 'a']++;
      if (j >= i) {
        v[b[j - i] - 'a']--;
        if (S.count(v))
          ans = i;
      }
    }
    if (S.count(v))
      ans = i;
  }
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

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

int main() {
  string a, b;
  cin >> a >> b;
  int ans = 0, n = min(a.size(), b.size());
  for (int i = 1; i <= n; i++) {
    set<vector<int>> S;
    vector<int> v(26, 0);
    for (int j = 0; j <= n; j++) {
      if (i == j)
        S.insert(v);
      v[a[j] - 'a']++;
      if (j >= i) {
        v[a[j - i] - 'a']--;
        S.insert(v);
      }
    }
    v = vector<int>(26, 0);
    for (int j = 0; j <= n; j++) {
      if (i == j && S.count(v))
        ans = i;
      v[b[j] - 'a']++;
      if (j >= i) {
        v[b[j - i] - 'a']--;
        if (S.count(v))
          ans = i;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00949 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
string s, t, p;
bool ch(int x) {
  set<vector<int>> v;
  vector<int> y(26, 0);

  for (int i = 0; i + x <= s.size(); i++) {
    if (!i)
      for (int j = 0; j < x; j++)
        y[s[j] - 'a']++;
    else
      y[s[i + x - 1] - 'a']++, y[s[i - 1] - 'a']--;
    v.insert(y);
  }
  for (int i = 0; i < 26; i++)
    y[i] = 0;
  for (int i = 0; i + x <= t.size(); i++) {
    if (!i)
      for (int j = 0; j < x; j++)
        y[t[j] - 'a']++;
    else
      y[t[i + x - 1] - 'a']++, y[t[i - 1] - 'a']--;
    if (v.count(y))
      return 1;
  }
  return 0;
}
int main() {
  cin >> s >> t;
  int sum = min((int)s.size(), (int)t.size());
  while (!ch(sum))
    sum--;
  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string s, t;
bool ch(int x) {
  unordered_set<string> v;
  for (int i = 0; i + x <= s.size(); i++) {
    string p = s.substr(i, x);
    sort(p.begin(), p.end());
    v.insert(p);
  }
  for (int i = 0; i + x <= t.size(); i++) {
    string p = t.substr(i, x);
    sort(p.begin(), p.end());
    if (v.count(p))
      return 1;
  }
  return 0;
}
int main() {
  cin >> s >> t;
  int sum = min((int)s.size(), (int)t.size());
  while (!ch(sum))
    sum--;
  cout << sum << endl;
}
Problem p00949 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int main() {
  string s, t;
  cin >> s >> t;

  vector<vector<short>> vec(t.size() + 1, vector<short>(26));
  for (int i = 1; i <= t.size(); i++) {
    vec[i][t[i - 1] - 'a']++;

    for (int j = 0; j < 26; j++) {
      vec[i][j] += vec[i - 1][j];
    }
  }

  vector<vector<short>> v(s.size() + 1, vector<short>(26));
  for (int i = 1; i <= s.size(); i++) {
    v[i][s[i - 1] - 'a']++;

    for (int j = 0; j < 26; j++) {
      v[i][j] += v[i - 1][j];
    }
  }

  for (int i = s.size(); i > 0; i--) {
    set<vector<short>> sv;
    for (int j = 0; j + i < vec.size(); j++) {
      vector<short> sub(26);
      for (int k = 0; k < 26; k++) {
        sub[k] += vec[i + j][k] - vec[j][k];
      }

      sv.insert(sub);
    }

    for (int j = 0; j + i < v.size(); j++) {
      vector<short> sub(26);
      for (int k = 0; k < 26; k++) {
        sub[k] = v[i + j][k] - v[j][k];
      }

      if (sv.find(sub) != sv.end()) {
        cout << i << endl;
        return 0;
      }
    }
  }

  cout << 0 << endl;

  return 0;
}

Error: MLE

Fixed submission:

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

int main() {
  string s, t;
  cin >> s >> t;

  vector<vector<short>> vec(t.size(), vector<short>(26));
  for (int i = 0; i < t.size(); i++) {
    if (i < t.size())
      vec[i][t[i] - 'a']++;

    for (int j = 0; i > 0 && j < 26; j++) {
      vec[i][j] += vec[i - 1][j];
    }
  }

  set<vector<short>> sv;
  for (int i = 0; i < t.size(); i++) {
    sv.insert(vec[i]);
    for (int j = i + 1; j < vec.size(); j++) {
      vector<short> sub(26);
      for (int k = 0; k < 26; k++) {
        sub[k] = vec[j][k] - vec[i][k];
      }
      sv.insert(sub);
    }
  }

  vec.resize(0);

  vector<vector<short>> v(s.size(), vector<short>(26));
  for (int i = 0; i < s.size(); i++) {
    if (i < s.size())
      v[i][s[i] - 'a']++;

    for (int j = 0; i > 0 && j < 26; j++) {
      v[i][j] += v[i - 1][j];
    }
  }

  int ans = 0;
  for (int i = 0; i < s.size(); i++) {
    if (sv.find(v[i]) != sv.end()) {
      ans = max(ans, i + 1);
    }

    for (int j = i + 1; j < v.size(); j++) {
      vector<short> sub(26);
      for (int k = 0; k < 26; k++) {
        sub[k] = v[j][k] - v[i][k];
      }

      if (sv.find(sub) != sv.end()) {
        ans = max(ans, j - i);
      }
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
using ll = __int128;
const ll b = 1713302033171ll;

ll Pow(ll x, ll n) {
  ll res = 1;
  while (n) {
    if (n & 1) {
      res = (res * x) % b;
    }
    x = (x * x) % b;
    n >>= 1;
  }
  return res;
}

int main() {
  vector<ll> prime;
  for (ll i = 100000; prime.size() < 26; i++) {
    bool flag = true;
    for (ll j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        flag = false;
        break;
      }
    }
    if (flag) {
      prime.push_back(i);
    }
  }

  string s1, s2;
  cin >> s1 >> s2;
  int n1 = s1.size(), n2 = s2.size();
  vector<ll> rh1(n1 + 1), rrh1(n1 + 1), rh2(n2 + 1), rrh2(n2 + 1);
  rh1[0] = rrh1[0] = rh2[0] = rrh2[0] = 1;
  for (int i = 1; i <= n1; i++) {
    rh1[i] = (rh1[i - 1] * prime[s1[i - 1] - 'a']) % b;
    rrh1[i] = Pow(rh1[i], b - 2);
  }
  for (int i = 1; i <= n2; i++) {
    rh2[i] = (rh2[i - 1] * prime[s2[i - 1] - 'a']) % b;
    rrh2[i] = Pow(rh2[i], b - 2);
  }
  bool fini = true;
  for (int k = min(n1, n2); k > 0; k--) {
    bool can = false;
    vector<ll> hs(n1 - k + 1);
    for (int i = 0; i + k <= n1; i++) {
      hs[i] = (rrh1[i] * rh1[i + k]) % b;
    }
    sort(hs.begin(), hs.end());
    for (int j = 0; j + k <= n2; j++) {
      if (binary_search(hs.begin(), hs.end(), (rrh2[j] * rh2[j + k]) % b)) {
        can = true;
        break;
      }
    }
    if (can) {
      fini = false;
      cout << k << endl;
      break;
    }
  }
  if (fini) {
    cout << 0 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = __int128;
const ll b = 1713302033171ll;

ll Pow(ll x, ll n) {
  ll res = 1;
  while (n) {
    if (n & 1) {
      res = (res * x) % b;
    }
    x = (x * x) % b;
    n >>= 1;
  }
  return res;
}

int main() {
  vector<ll> prime;
  for (ll i = 100000; prime.size() < 26; i++) {
    bool flag = true;
    for (ll j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        flag = false;
        break;
      }
    }
    if (flag) {
      prime.push_back(i);
    }
  }

  string s1, s2;
  cin >> s1 >> s2;
  int n1 = s1.size(), n2 = s2.size();
  vector<ll> rh1(n1 + 1), rh2(n2 + 1);
  rh1[0] = rh2[0] = 1;
  for (int i = 1; i <= n1; i++) {
    rh1[i] = (rh1[i - 1] * prime[s1[i - 1] - 'a']) % b;
  }
  for (int i = 1; i <= n2; i++) {
    rh2[i] = (rh2[i - 1] * prime[s2[i - 1] - 'a']) % b;
  }
  bool fini = true;
  for (int k = min(n1, n2); k > 0; k--) {
    bool can = false;
    vector<ll> hs(n1 - k + 1);
    for (int i = 0; i + k <= n1; i++) {
      hs[i] = (Pow(rh1[i], b - 2) * rh1[i + k]) % b;
    }
    sort(hs.begin(), hs.end());
    for (int j = 0; j + k <= n2; j++) {
      if (binary_search(hs.begin(), hs.end(),
                        (Pow(rh2[j], b - 2) * rh2[j + k]) % b)) {
        can = true;
        break;
      }
    }
    if (can) {
      fini = false;
      cout << k << endl;
      break;
    }
  }
  if (fini) {
    cout << 0 << endl;
  }
  return 0;
}
Problem p00949 (C++) — buggy submission, verdict: Memory Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF (1 << 29)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define uniq(v) v.erase(unique(all(v)), v.end())

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

  string a, b;
  cin >> a >> b;
  int ans = 0;
  for (int len = min(a.size(), b.size()); len > 0; len--) {
    set<array<int, 26>> s;
    array<int, 26> ar;
    ar.fill(0);
    rep(i, len) { ar[a[i] - 'a']++; }
    s.insert(ar);
    for (int i = len; i < a.size(); i++) {
      ar[a[i - len] - 'a']--;
      ar[a[i] - 'a']++;
      s.insert(ar);
    }

    ar.fill(0);
    rep(i, len) { ar[b[i] - 'a']++; }
    if (s.count(ar))
      ans = len;
    else {
      for (int i = len; i < b.size(); i++) {
        ar[b[i - len] - 'a']--;
        ar[b[i] - 'a']++;
        if (s.count(ar)) {
          ans = len;
          break;
        }
      }
    }
    if (ans)
      break;
  }
  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF (1 << 29)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define uniq(v) v.erase(unique(all(v)), v.end())

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

  string a, b;
  set<array<int, 26>> s;

  cin >> a >> b;
  int ans = 0;
  rep(i, a.size()) {
    array<int, 26> ar;
    ar.fill(0);
    for (int j = i; j < a.size(); j++) {
      ar[a[j] - 'a']++;
      s.insert(ar);
    }
  }
  rep(i, b.size()) {
    array<int, 26> ar;
    ar.fill(0);
    for (int j = i; j < b.size(); j++) {
      ar[b[j] - 'a']++;
      if (ans < j - i + 1 && s.count(ar)) {
        ans = j - i + 1;
      }
    }
  }
  cout << ans << endl;

  return 0;
}