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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define inf (1 << 30) - 1
#define INF (1LL << 62) - 1
#define MOD 1000000007LL
#define MP make_pair
#define PB push_back
#define ALL(x) x.begin(), x.end()
#define PI acos(-1)
#define MEM(x, y) memset(x, y, sizeof(x))
#define debug cout << "A" << '\\n'
#define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define PER(i, a, b) for (int i = (a); i >= (b); i--)
#define REPL(i, a, b) for (LL i = (a); i <= (b); i++)
#define PERL(i, a, b) for (LL i = (a); i >= (b); i--)
#define print(x) cout << x << '\\n'
#define itrALL(c, itr)                                                         \\
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
#define UNIQUE(X) X.erase(unique(X.begin(), X.end()), X.end())

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<LL> VL;

// Order Statistic Tree

/* Special functions:

        find_by_order(k) --> returns iterator to the kth largest element
   counting from 0 order_of_key(val) --> returns the number of items in a set
   that are strictly smaller than our item
*/

typedef tree<PII, null_type, less<PII>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

template <class T> inline T bigmod(T p, T e, T M) {
  T ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1)
      ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}

template <class T> inline T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); }
template <class T, class X> inline bool getbit(T a, X i) {
  T t = 1;
  return ((a & (t << i)) > 0);
}
template <class T, class X> inline T setbit(T a, X i) {
  T t = 1;
  return (a | (t << i));
}
template <class T, class X> inline T resetbit(T a, X i) {
  T t = 1;
  return (a & (~(t << i)));
}

/*end of header*/

string s1, s2;

struct data {
  int cnt[26];

  bool operator<(const data &b) const {
    REP(i, 0, 25) {
      if (this->cnt[i] < b.cnt[i])
        return 1;
      else if (this->cnt[i] > b.cnt[i])
        return 0;
    }
    return 0;
  }
};

bool ispossible(int len) {
  set<data> s;
  data a;
  MEM(a.cnt, 0);
  REP(i, 0, len - 1) a.cnt[s1[i] - 'a']++;
  s.insert(a);
  REP(i, len, s1.size() - 1) {
    a.cnt[s1[i - len] - 'a']--;
    a.cnt[s1[i] - 'a']++;
    s.insert(a);
  }
  MEM(a.cnt, 0);
  REP(i, 0, len - 1) a.cnt[s2[i] - 'a']++;
  if (s.find(a) != s.end())
    return 1;
  REP(i, len, s2.size() - 1) {
    a.cnt[s2[i - len] - 'a']--;
    a.cnt[s2[i] - 'a']++;
    if (s.find(a) != s.end())
      return 1;
  }
  return 0;
}

int main() {
  // freopen("input_file_name.in","r",stdin);
  // freopen("output_file_name.out","w",stdout);
  //    ios_base::sync_with_stdio(0);
  //    cin.tie(0);

  cin >> s1 >> s2;
  int ans = 0;
  REP(i, 1, min(s1.size(), s2.size())) if (ispossible(i)) ans = i;
  print(ans);
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_map>
// #include <boost/functional/hash.hpp>
using namespace std;

#define ll long long
#define pl pair<ll, ll>
#define p1 1009
#define p2 1013
#define MOD1 1000000007
#define MOD2 1000000009

unordered_map<ll, int> m;
vector<int> v;
char s1[4004], s2[4004];
string s;

int ar[205];
ll Hash() {
  pl ret;
  for (ll i = 0; i < 26; i++) {
    ret.first = (ret.first * p1 + ar[i]) % MOD1;
    ret.second = (ret.second * p2 + ar[i]) % MOD2;
  }
  return ret.first * 1000000000ll + ret.second;
}
string strng(int x) {
  string q = "";
  if (x == 0)
    q += '0';
  else {
    while (x) {
      q += (x % 10) + '0';
      x /= 10;
    }
  }
  return q;
}

int main() {
  scanf("%s %s", s1, s2);
  {
    int mx = 0;
    int n1 = strlen(s1);
    int n2 = strlen(s2);

    for (int i = 0; i < n1; i++) {
      s = "";
      for (int j = 0; j < 26; j++) {
        ar[j] = 0;
      }
      int len = 0;
      for (int j = i; j < n1; j++) {
        len++;
        ar[s1[j] - 'a']++;

        ll p = Hash();
        m[p] = 1;
      }
    }
    for (int i = 0; i < n2; i++) {
      s = "";
      for (int j = 0; j < 26; j++) {
        ar[j] = 0;
      }
      int len = 0;
      for (int j = i; j < n2; j++) {
        len++;
        ar[s2[j] - 'a']++;

        ll p = Hash();
        if (m[p] == 1) {
          mx = max(mx, len);
        }
      }
    }
    printf("%d\\n", mx);
    m.clear();
  }
}