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

#include <bits/stdc++.h>
#define ll long long
#define INF (1ll << 15) - 1
using namespace std;
char s1[4010], s2[4010];
short sum1[4010][26], sum2[4010][26];
struct data {
  int s1, s2;
  short l;
  data() {}
  data(int _s1, int _s2, int _l) : s1(_s1), s2(_s2), l(_l) {}
  bool operator<(const data &b) const {
    if (s1 == b.s1 && s2 == b.s2)
      return l < b.l;
    else
      return s1 == b.s1 ? s2 < b.s2 : s1 < b.s1;
  }
  bool operator==(const data &b) const {
    return (s1 == b.s1 && s2 == b.s2 && l == b.l);
  }
} A[8000010], tmp;
int t = 0;
int main() {
  scanf("%s", s1 + 1);
  scanf("%s", s2 + 1);
  short N = strlen(s1 + 1), M = strlen(s2 + 1);
  for (short i = 1; i <= N; ++i)
    for (short j = 0; j < 26; ++j)
      sum1[i][j] = sum1[i - 1][j] + (s1[i] == j + 'a');
  for (short i = 1; i <= M; ++i)
    for (short j = 0; j < 26; ++j)
      sum2[i][j] = sum2[i - 1][j] + (s2[i] == j + 'a');
  for (short i = 1; i <= N; ++i)
    for (short j = 1; j <= min((short)(N - i + 1), (short)M); ++j) {
      tmp.s1 = 0;
      tmp.s2 = 0;
      for (short k = 0; k < 26; ++k)
        tmp.s1 =
            ((ll)tmp.s1 * 2333333333 + sum1[i + j - 1][k] - sum1[i - 1][k]) %
            1000000009,
        tmp.s2 = ((ll)tmp.s2 * 23333333 + sum1[i + j - 1][k] - sum1[i - 1][k] +
                  100007) %
                 1000000007;
      tmp.l = j;
      A[++t] = tmp;
    }
  sort(A + 1, A + 1 + t);
  short ans = 0;
  for (short i = 1; i <= M; ++i)
    for (short j = max((short)1, ans); j <= min((short)(M - i + 1), (short)N);
         ++j) {
      tmp.s1 = 0;
      tmp.s2 = 0;
      tmp.l = j;
      for (short k = 0; k < 26; ++k)
        tmp.s1 =
            ((ll)tmp.s1 * 2333333333 + sum2[i + j - 1][k] - sum2[i - 1][k]) %
            1000000009,
        tmp.s2 = ((ll)tmp.s2 * 23333333 + sum2[i + j - 1][k] - sum2[i - 1][k] +
                  100007) %
                 1000000007;
      int r = lower_bound(A + 1, A + 1 + t, tmp) - A;
      if (A[r] == tmp)
        ans = max(ans, A[r].l);
    }
  printf("%d\\n", ans);
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <unordered_set>
using namespace std;
const int ha = 817;
const int P = 1000000007;
unordered_set<int> hx;
char s[4005], t[4005];
int a[4005][28], b[4005][30];
int ans;
int ls, lt, tot;
inline bool pd(int x) {
  int i, j;
  for (i = 1; i + x - 1 <= lt; i++) {
    int key = ha;
    for (j = 0; j < 26; j++)
      key += (key << 4) + b[i + x - 1][j] - b[i - 1][j], key %= P;
    if (hx.count(key))
      return 1;
  }
  return 0;
}
int main() {
  register int i, j, k;
  scanf("%s %s", s + 1, t + 1);
  ls = strlen(s + 1), lt = strlen(t + 1);
  if (ls < lt)
    ans = ls;
  else
    ans = lt;
  for (i = 1; i <= ls; i++) {
    if (i != 1)
      for (j = 0; j < 26; j++)
        a[i][j] = a[i - 1][j];
    a[i][s[i] - 'a']++;
  }
  for (i = 1; i <= lt; i++) {
    if (i != 1)
      for (j = 0; j < 26; j++)
        b[i][j] = b[i - 1][j];
    b[i][t[i] - 'a']++;
  }
  for (i = 1; i <= ls; i++) {
    for (j = i; j <= ls; j++) {
      int key = ha;
      for (k = 0; k < 26; k++)
        key += (key << 4) + a[j][k] - a[i - 1][k], key %= P;
      hx.insert(key);
    }
  }
  while (ans) {
    if (pd(ans))
      break;
    ans--;
  }
  printf("%d\\n", ans);
}