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

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
using namespace std;
typedef unsigned long long ull;
const int MAXN = 26 + 8;
const ull p = 1e12 + 7; //!

struct HASH {
  ull Hash[MAXN], xp[MAXN];
  void inithash() { memset(Hash, 0, sizeof Hash); }
  void init1(int sz) {
    xp[0] = 1;
    for (int i = 1; i <= sz; i++)
      xp[i] = xp[i - 1] * p;
  }
  void init2(int *s) { // 0~n-1 based
    int sz = 26;
    Hash[sz] = 0;
    for (int i = sz - 1; i >= 0; i--) {
      Hash[i] = Hash[i + 1] * p + (s[i] - '0' + 1);
    }
  }
  ull get_Hash(const int &st, const int &len) {
    return Hash[st] - Hash[st + len] * xp[len];
  }

} hh;

string s1, s2;
int ch[26], sz1, sz2;
map<ull, bool> mp;
inline bool check(int x) {
  mp.clear();
  int i, j;
  for (i = 0; i < 26; i++)
    ch[i] = 0;

  for (i = 0; i < x; i++) {
    ch[s1[i] - 'a']++;
  }
  hh.inithash();
  hh.init2(ch);
  mp[hh.get_Hash(0, 26)] = true;
  for (i = x; i < sz1; i++) {
    ch[s1[i] - 'a']++;
    ch[s1[i - x] - 'a']--;
    hh.inithash();
    hh.init2(ch);
    mp[hh.get_Hash(0, 26)] = true;
  }
  //
  for (i = 0; i < 26; i++)
    ch[i] = 0;

  for (i = 0; i < x; i++) {
    ch[s2[i] - 'a']++;
  }
  hh.inithash();
  hh.init2(ch);
  if (mp.find(hh.get_Hash(0, 26)) != mp.end())
    return true;
  for (i = x; i < sz2; i++) {
    ch[s2[i] - 'a']++;
    ch[s2[i - x] - 'a']--;
    hh.inithash();
    hh.init2(ch);
    if (mp.find(hh.get_Hash(0, 26)) != mp.end())
      return true;
  }
  return false;
}

int main() {
#ifdef LOCAL
  freopen("d.txt", "r", stdin);
  // freopen("d.out", "w", stdout);
  int T = 4;
  while (T--) {
#endif // LOCAL
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> s1 >> s2;
    sz1 = s1.size(), sz2 = s2.size();
    hh.init1(30);
    int ans = 0, i;
    for (i = min(sz1, sz2); i >= 1; i--) {
      if (check(i)) {
        ans = i;
        break;
      }
    }
    cout << ans << "\\n";

#ifdef LOCAL
    cout << endl;
  }
#endif // LOCAL
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define L long long int
map<L, int> ara[4005];
int mark[27];
string a, b;
void get_hash(int n, int lena) {
  // cout<<n<<"**" <<endl;
  memset(mark, 0, sizeof(mark));
  L hash_val = 0;
  L base = 313;
  L MOD = 1000000007;
  int l = 0, r = 0;
  for (int r = 0; r < lena;) {
    mark[a[r] - 'a' + 1]++;
    r++;
    if (r - l > n) {
      mark[a[l] - 'a' + 1]--;
      l++;
    }
    // if(n==2)cout<< r <<' '<<l<< endl;
    if (r - l == n) {
      for (int i = 1; i <= 26; i++) {
        if (mark[i] == 0)
          continue;
        hash_val += hash_val * base + ((i)*mark[i]);
        hash_val %= MOD;
      }
      // cout<<endl;
      // if(n==18)cout<< hash_val <<'*';
      // cout<<endl;
      ara[n][hash_val] = 1;
      hash_val = 0;
      // cout<<endl<<endl;
    }
  }
  return;
}
bool check(int n) {
  // cout<<endl<<endl<<endl;
  int lenb = b.size();
  memset(mark, 0, sizeof(mark));
  L hash_val = 0;
  L base = 313;
  L MOD = 1000000007;
  int l = 0, r = 0;
  for (r = 0; r < lenb;) {
    mark[b[r] - 'a' + 1]++;
    r++;
    if (r - l > n) {
      mark[b[l] - 'a' + 1]--;
      l++;
    }
    // if(n==2)cout<< r <<' '<<l<< endl;
    if (r - l == n) {
      for (int i = 1; i <= 26; i++) {
        if (mark[i] == 0)
          continue;
        hash_val += hash_val * base + ((i)*mark[i]);
        hash_val %= MOD;
      }
      // cout<<endl;
      // if(n==18) cout<< hash_val<<' ';
      // cout<<endl;
      if (ara[n][hash_val] == 1)
        return true;
      hash_val = 0;
      // cout<<endl<<endl;
    }
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> a >> b;
  if (a.size() > b.size())
    swap(a, b);
  int lena = a.size();
  for (int i = 1; i <= lena; i++)
    get_hash(i, lena);
  int ans = 0;
  for (int i = a.size(); i >= 0; i--) {
    if (check(i)) {
      ans = i;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}