Problem p00949 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
const int INF = 1e9;

set<vector<int>> m1;
string s1, s2;
vector<int> memo(26);
int ans = 0;

void hashh(void) {
  /*
  for (int i = 0; i < s1.size(); ++i){
          rep(k, 26)memo[k] = 0;
          for (int j = i; j < s1.size(); ++j){
                  memo[s1[j] - 'a']++;
                  m1.insert(memo);
          }

          rep(k, 26) memo[k] = 0;
          for (int l = 0; l < s2.size(); ++l){
                  rep(k, 26) memo[k] = 0;
                  for (int m = l; m < s2.size(); ++m){
                          memo[s2[m] - 'a']++;
                          if(m1.count(memo)){
                                  ans = max(ans, m - l + 1);
                          }
                  }
          }
          m1.erase(m1.begin(), m1.end());
  }
  */

  for (int i = 1; i <= s1.size(); ++i) { //??????
    rep(k, 26) memo[k] = 0;
    for (int j = 0; j < i; ++j) {
      memo[s1[j] - 'a']++;
      m1.insert(memo);
    }
    int l = 0;
    for (int r = i; r < s1.size(); ++r) {
      // printf("l %d r %d\n", l, r);
      memo[s1[r] - 'a']++;
      memo[s1[l] - 'a']--;
      m1.insert(memo);
      l++;
    }

    rep(k, 26) memo[k] = 0;
    for (int j = 0; j < i; ++j) {
      memo[s2[j] - 'a']++;
    }
    if (m1.count(memo)) {
      ans = max(ans, i);
    }
    l = 0;
    for (int r = i; r < s2.size(); ++r) {
      memo[s2[r] - 'a']++;
      memo[s2[l] - 'a']--;
      if (m1.count(memo)) {
        ans = max(ans, i);
      }
      l++;
    }
  }
}

int main(void) {
  cin >> s1 >> s2;
  if (s1.size() > s2.size()) {
    swap(s1, s2);
  }
  hashh();
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
const int INF = 1e9;

set<vector<int>> m1;
string s1, s2;
vector<int> memo(26);
int ans = 0;

void hashh(void) {
  /*
  for (int i = 0; i < s1.size(); ++i){
          rep(k, 26)memo[k] = 0;
          for (int j = i; j < s1.size(); ++j){
                  memo[s1[j] - 'a']++;
                  m1.insert(memo);
          }

          rep(k, 26) memo[k] = 0;
          for (int l = 0; l < s2.size(); ++l){
                  rep(k, 26) memo[k] = 0;
                  for (int m = l; m < s2.size(); ++m){
                          memo[s2[m] - 'a']++;
                          if(m1.count(memo)){
                                  ans = max(ans, m - l + 1);
                          }
                  }
          }
          m1.erase(m1.begin(), m1.end());
  }
  */

  for (int i = 1; i <= s1.size(); ++i) { //??????
    rep(k, 26) memo[k] = 0;
    for (int j = 0; j < i; ++j) {
      memo[s1[j] - 'a']++;
      m1.insert(memo);
    }
    int l = 0;
    for (int r = i; r < s1.size(); ++r) {
      // printf("l %d r %d\n", l, r);
      memo[s1[r] - 'a']++;
      memo[s1[l] - 'a']--;
      m1.insert(memo);
      l++;
    }

    rep(k, 26) memo[k] = 0;
    for (int j = 0; j < i; ++j) {
      memo[s2[j] - 'a']++;
    }
    if (m1.count(memo)) {
      ans = max(ans, i);
    }
    l = 0;
    for (int r = i; r < s2.size(); ++r) {
      memo[s2[r] - 'a']++;
      memo[s2[l] - 'a']--;
      if (m1.count(memo)) {
        ans = max(ans, i);
      }
      l++;
    }
    m1.erase(m1.begin(), m1.end());
  }
}

int main(void) {
  cin >> s1 >> s2;
  if (s1.size() > s2.size()) {
    swap(s1, s2);
  }
  hashh();
  printf("%d\n", ans);
  return 0;
}