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

/********************************************
 *                                           *
 *    Solved By : Bir Bahadur Khatri(B'ru)   *
 *      Be Positive,be Happy.                *
 *                                           *
 *********************************************/
#define ff first
#define D double
#define sz size()
#define ss second
#define PB push_back
#define MP make_pair
#define SQR(n) (n * n)
#define CHR getchar()
#define NL printf("\\n")
#include <bits/stdc++.h>
#define ULL unsigned LL
#define PI 2.0 * acos(0.0)
#define LL long long int
#define S1(a) a = in<int>()
#define SL1(a) a = in<LL>()
#define Max(a, b) ((a > b) ? a : b)
#define Min(a, b) ((a < b) ? a : b)
#define all(a) a.begin(), a.end()
#define _Max(a, b, c) Max(a, Max(b, c))
#define _Min(a, b, c) Min(a, Min(b, c))
#define SL2(a, b) a = in<LL>(), b = in<LL>()
#define F(i, a, b) for (int i = a; i < b; i++)
#define S2(a, b) a = in<int>(), b = in<int>()
#define R(i, a, b) for (int i = a - 1; i >= b; i--)
#define BitCnt(a) __builtin_popcountll(a)
#define MEM(a, val) memset(a, val, sizeof(a))
#define SL3(a, b, c) a = in<LL>(), b = in<LL>(), c = in<LL>()
#define S3(a, b, c) a = in<int>(), b = in<int>(), c = in<int>()
#define _cin                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
#define cp printf("***** here here here here *****\\n");
#define trace1(x) cerr << #x << ": " << x << endl;
#define InpOut freopen("A.in", "r", stdin), freopen("A1.out", "w", stdout)
#define trace2(x, y)                                                           \\
  cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                                                        \\
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \\
       << z << endl;
#define trace4(a, b, c, d)                                                     \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl;

using namespace std;
template <typename T> T in() {
  char ch;
  T n = 0;
  bool ng = false;
  while (1) {
    ch = getchar();
    if (ch == '-') {
      ng = true;
      ch = getchar();
      break;
    }
    if (ch >= '0' && ch <= '9')
      break;
  }
  while (1) {
    n = n * 10 + (ch - '0');
    ch = getchar();
    if (ch < '0' || ch > '9')
      break;
  }
  return (ng ? -n : n);
}
template <typename T> inline T POW(T B, T P) {
  if (P == 0)
    return 1;
  if (P & 1)
    return B * POW(B, P - 1);
  else
    return SQR(POW(B, P / 2));
}
template <typename T> inline T Gcd(T a, T b) {
  if (a < 0)
    return Gcd(-a, b);
  if (b < 0)
    return Gcd(a, -b);
  return (b == 0) ? a : Gcd(b, a % b);
}
template <typename T> inline T Lcm(T a, T b) {
  if (a < 0)
    return Lcm(-a, b);
  if (b < 0)
    return Lcm(a, -b);
  return a * (b / Gcd(a, b));
}
long long Bigmod(long long base, long long power, long long MOD) {
  long long ret = 1;
  while (power) {
    if (power & 1)
      ret = (ret * base) % MOD;
    base = (base * base) % MOD;
    power >>= 1;
  }
  return ret;
}
bool isVowel(char ch) {
  ch = toupper(ch);
  if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
    return true;
  return false;
}
template <typename T> LL isLeft(T a, T b, T c) {
  return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y);
}
long long ModInverse(long long number, long long MOD) {
  return Bigmod(number, MOD - 2, MOD);
}
bool isConst(char ch) {
  if (isalpha(ch) && !isVowel(ch))
    return true;
  return false;
}
int toInt(string s) {
  int sm;
  stringstream ss(s);
  ss >> sm;
  return sm;
}
///**********************************************************//

#define MD1 1000000007ULL
#define MD2 1000000009ULL
#define MD3 1000000021ULL
#define BS1 10000019ULL
#define BS2 10000079ULL
#define BS3 10000103ULL
#define PUL pair<ULL, ULL>
#define PLL pair<LL, LL>
#define PII pair<int, int>

///         0123456789
#define MX 4005
#define MOD 1000000007
#define INF 2000000000
#define EPS 1e-9

/// ==========================================////

vector<ULL> A[MX][2];

int main() {
  string a, b;
  cin >> a >> b;
  int ans = 0;

  for (int i = 0; i < a.sz; i++) {
    int fr[26] = {0};
    for (int j = i; j < a.sz; j++) {
      fr[a[j] - 'a']++;
      ULL hs = 0;
      F(k, 0, 26) hs = hs * BS1 + (fr[k]);
      A[j - i + 1][0].PB(hs);
    }
  }

  for (int i = 0; i < b.sz; i++) {
    int fr[26] = {0};
    for (int j = i; j < b.sz; j++) {
      fr[b[j] - 'a']++;
      ULL hs = 0;
      F(k, 0, 26) hs = hs * BS1 + (fr[k]);
      A[j - i + 1][1].PB(hs);
    }
  }
  int n = a.sz;
  int m = b.sz;
  n = min(n, m);

  F(i, 1, n + 1) {
    sort(all(A[i][0]));
    sort(all(A[i][1]));
    int lf = 0, rg = 0;

    int fl = 0;

    while (lf < A[i][0].sz && rg < A[i][1].sz) {
      if (A[i][0][lf] == A[i][1][rg]) {
        fl = 1;
        break;
      }
      if (A[i][0][lf] < A[i][1][rg]) {
        lf++;
      } else
        rg++;
    }
    if (fl)
      ans = max(ans, i);
  }
  printf("%d\\n", ans);

  return 0;
}
///============= Thank You ===================///

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define PR1 43
#define MOD1 1000000007
#define PR2 37
#define MOD2 1000000009
#define mp(a, b) make_pair(a, b)

map<pair<int, int>, int> all;

char a[4005], b[4005];
int pwr1[30], pwr2[30];

int mod_plus(int a, int b, int MOD) { return (a + b) % MOD; }

int mod_mul(long long a, long long b, int MOD) { return (a * b) % MOD; }

void solve(void) {
  int lena = strlen(a), lenb = strlen(b), i, j;
  pwr1[0] = 1;
  pwr2[0] = 1;
  all.clear();
  for (i = 1; i < 30; i++) {
    pwr1[i] = mod_mul(pwr1[i - 1], PR1, MOD1);
    pwr2[i] = mod_mul(pwr2[i - 1], PR2, MOD2);
  }
  for (i = 0; i < lena; i++) {
    int h1 = 0, h2 = 0;
    for (j = i; j < lena; j++) {
      h1 = mod_plus(h1, pwr1[a[j] - 'a'], MOD1);
      h2 = mod_plus(h2, pwr2[a[j] - 'a'], MOD2);
      all[mp(h1, h2)] = 1;
    }
  }
  int ans = 0;
  for (i = 0; i < lenb; i++) {
    int h1 = 0, h2 = 0;
    for (j = i; j < lenb; j++) {
      h1 = mod_plus(h1, pwr1[b[j] - 'a'], MOD1);
      h2 = mod_plus(h2, pwr2[b[j] - 'a'], MOD2);
      if (j - i + 1 <= ans)
        continue;
      if (all.find(mp(h1, h2)) != all.end())
        ans = max(ans, j - i + 1);
    }
  }
  cout << ans << endl;
  return;
}

int main() {
  // freopen("input.txt","r",stdin);
  while (scanf("%s%s", a, b) != EOF)
    solve();
  return 0;
}