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

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
// #define mt make_tuple
// typedef tuple<double,int,double> tp;
// typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
const double PI = acos(-1);
const double EPS = 1e-7;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
#define MAX 27
int ash[2][MAX] = {0};
int MOD[2] = {1145141919, 1145148109};
int x[2] = {381038107, 524524524};
int power[2][MAX];
int main() {
  string s, t;
  cin >> s >> t;
  if (s.size() > t.size())
    swap(s, t);
  rep(i, 2) {
    power[i][0] = 1;
    rep(j, MAX) power[i][j + 1] = (ll)power[i][j] * x[i] % MOD[i];
  }
  for (int n = s.size(); n > 0; n--) {
    map<pii, int> ma;
    vi co(26);
    rep(i, n) co[s[i] - 'a']++;
    rep(p, s.size() - n + 1) {
      pii has;
      rep(i, 2) {
        int ha = 0;
        rep(j, 26)(ha += (ll)power[i][j] * co[j] % MOD[i]) %= MOD[i];
        if (i)
          has.second = ha;
        else
          has.first = ha;
      }
      ma[has] = true;
      if (p == s.size() - n)
        break;
      co[s[n + p] - 'a']++;
      co[s[p] - 'a']--;
    }

    co = vi(26);
    rep(i, n) co[t[i] - 'a']++;
    bool h = false;
    rep(p, t.size() - n + 1) {
      pii has;
      rep(i, 2) {
        int ha = 0;
        rep(j, 26)(ha += (ll)power[i][j] * co[j] % MOD[i]) %= MOD[i];
        if (i)
          has.second = ha;
        else
          has.first = ha;
      }
      //      cout<<has.first<<" "<<has.second<<endl;
      if (ma.count(has)) {
        h = true;
        break;
      }
      if (p == t.size() - n)
        break;
      co[t[n + p] - 'a']++;
      co[t[p] - 'a']--;
    }
    if (h) {
      cout << n << endl;
      return 0;
    }
  }
  cout << 0 << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
// #define mt make_tuple
// typedef tuple<double,int,double> tp;
// typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
const double PI = acos(-1);
const double EPS = 1e-7;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
#define MAX 27
int ash[2][MAX] = {0};
int MOD[2] = {1000000007, 1000000009};
int x[2] = {524524524, 36103610};
int power[2][MAX];
map<int, bool> ma;
int main() {
  string s, t;
  cin >> s >> t;
  rep(i, 2) {
    power[i][0] = 1;
    rep(j, MAX) power[i][j + 1] = (ll)power[i][j] * x[i] % MOD[i];
  }

  rep(q, s.size()) {
    vi co(26);
    loop(p, q, s.size()) {
      co[s[p] - 'a']++;
      int has;
      rep(i, 1) {
        int ha = 0;
        rep(j, 26)(ha += (ll)power[i][j] * co[j] % MOD[i]) %= MOD[i];
        has = ha;
      }
      ma[has] = true;
      // cout<<q<<" "<<p<<" "<<has.first<<" "<<has.second<<endl;
    }
  }
  int out = 0;
  rep(q, t.size()) {
    vi co(26);
    loop(p, q, t.size()) {
      //    cout<<q<<" "<<p<<endl;
      co[t[p] - 'a']++;
      int has;
      rep(i, 1) {
        int ha = 0;
        rep(j, 26)(ha += (ll)power[i][j] * co[j] % MOD[i]) %= MOD[i];
        has = ha;
      }
      if (ma.count(has))
        out = max(out, p - q + 1);
    }
  }
  cout << out << endl;
}