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

#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
using namespace std;

const int maxn = 4010;

char a[maxn], b[maxn];

struct cnt {
  int num[26];
  void init() { memset(num, 0, sizeof(num)); }
  bool operator<(const cnt &b) const {
    for (int i = 0; i < 26; i++)
      if (num[i] != b.num[i])
        return num[i] < b.num[i];
    return 0;
  }
  friend ostream &operator<<(ostream &out, cnt &a) {
    for (int i = 0; i < 26; i++)
      out << a.num[i] << " ";
    return out;
  }
};

bool can(int x) {
  set<cnt> cs;
  cnt cmp;
  cmp.init();
  for (int i = 0; a[i]; i++) {
    cmp.num[a[i] - 'a']++;
    if (i >= x)
      cmp.num[a[i - x] - 'a']--;
    if (i >= x - 1)
      cs.insert(cmp);
  }
  cmp.init();
  for (int i = 0; b[i]; i++) {
    cmp.num[b[i] - 'a']++;
    if (i >= x)
      cmp.num[b[i - x] - 'a']--;
    if (i >= x - 1)
      if (cs.find(cmp) != cs.end())
        return 1;
  }
  return 0;
}

int main() {
  scanf("%s%s", a, b);
  bool flag = false;
  for (int i = strlen(a); i > 0; i--)
    if (can(i)) {
      printf("%d\\n", i);
      flag = true;
      break;
    }
  if (!flag)
    printf("0\\n");
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int, int> PII;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int K = 1e6 + 7;
const int mod = 1e9 + 7;

char sa[K], sb[K];
char tmp[200];
int cnt[200];

#define MAXNUM 13
// 定?字典???体
typedef struct Trie {
  bool flag; // 从根到此是否?一个??
  Trie *next[MAXNUM];
} Trie;
// 声明一个根
Trie *root;
// 初始化?根
void init() {
  root = (Trie *)malloc(sizeof(Trie));
  root->flag = false;
  for (int i = 0; i < MAXNUM; i++)
    root->next[i] = NULL;
}
//??字典?的插入??操作
void insert(char *word) {
  Trie *tem = root;
  while (*word != '\\0') {
    if (tem->next[*word - 'a'] == NULL) {
      Trie *cur = (Trie *)malloc(sizeof(Trie));
      for (int i = 0; i < MAXNUM; i++)
        cur->next[i] = NULL;
      cur->flag = false;
      tem->next[*word - 'a'] = cur;
    }
    tem = tem->next[*word - 'a'];
    word++;
  }
  tem->flag = true;
}
//??一个??的操作
bool search(char *word) {
  Trie *tem = root;
  for (int i = 0; word[i] != '\\0'; i++) {
    if (tem == NULL || tem->next[word[i] - 'a'] == NULL)
      return false;
    tem = tem->next[word[i] - 'a'];
  }
  return tem->flag;
}
void del(Trie *cur) {
  for (int i = 0; i < MAXNUM; i++) {
    if (cur->next[i] != NULL)
      del(cur->next[i]);
  }
  free(cur), cur = NULL;
}
void work(int x, char ch) {
  int st = (ch - 'a') * 4;
  int a = x / 10000, b = (x / 100) % 10, c = (x / 10) % 10, d = x % 10;
  tmp[st] = a + 'a', tmp[st + 1] = b + 'a', tmp[st + 2] = c + 'a',
  tmp[st + 3] = d + 'a';
}
int la, lb;
bool sc(int x) {
  for (int i = 0; i < 26 * 4; i++)
    tmp[i] = 'a';
  for (int i = 0; i < 30; i++)
    cnt[i] = 0;
  for (int i = 1; i <= x; i++)
    work(++cnt[sa[i] - 'a'], sa[i]);
  insert(tmp);
  for (int i = x + 1; i <= la; i++) {
    work(--cnt[sa[i - x] - 'a'], sa[i - x]);
    work(++cnt[sa[i] - 'a'], sa[i]);
    insert(tmp);
    // printf("%s\\n",tmp);
  }
  for (int i = 0; i < 26 * 4; i++)
    tmp[i] = 'a';
  for (int i = 0; i < 30; i++)
    cnt[i] = 0;
  for (int i = 1; i <= x; i++)
    work(++cnt[sb[i] - 'a'], sb[i]);
  if (search(tmp))
    return 1;
  for (int i = x + 1; i <= lb; i++) {
    work(--cnt[sb[i - x] - 'a'], sb[i - x]);
    work(++cnt[sb[i] - 'a'], sb[i]);
    // printf("%s\\n",tmp);
    if (search(tmp))
      return 1;
  }
  return 0;
}
int main(void) {
  scanf("%s%s", sa + 1, sb + 1);
  la = strlen(sa + 1), lb = strlen(sb + 1);
  init();
  tmp[26 * 4] = '\\0';
  int ans = 0;
  for (int i = 1, mx = min(la, lb); i <= mx; i++) {
    if (i % 400 == 0) {
      del(root);
      init();
    }
    if (sc(i))
      ans = i;
  }
  printf("%d\\n", ans);
  return 0;
}