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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)

using i64 = int64_t;
using vi = vector<int>;

string s1, s2;
int n, m;

int a1[4048][26], a2[4048][26];

bool f(int len) {
  set<vector<int>> st;

  for (int i = 0; i < n; i++) {
    // see s[i,i+len-1]
    if (i + len - 1 >= n)
      break;
    vector<int> tmp(26);
    rep(j, 26) tmp[j] = a1[i + len][j] - a1[i][j];
    st.insert(tmp);
  }

  for (int i = 0; i < m; i++) {
    // see s2[i,i+len-1]
    if (i + len - 1 >= m)
      break;
    vector<int> tmp(26);
    rep(j, 26) tmp[j] = a2[i + len][j] - a2[i][j];
    if (st.count(tmp))
      return true;
  }
  return false;
}

int main() {
  cin >> s1 >> s2;
  n = s1.size();
  m = s2.size();

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 26; j++) {
      if (s1[i] - 'a' == j)
        a1[i + 1][j] = a1[i][j] + 1;
      else
        a1[i + 1][j] = a1[i][j];
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < 26; j++) {
      if (s2[i] - 'a' == j)
        a2[i + 1][j] = a2[i][j] + 1;
      else
        a2[i + 1][j] = a2[i][j];
    }
  }

  for (int len = min(n, m); len >= 0; len--) {
    if (f(len)) {
      cout << len << endl;
      return 0;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)

using i64 = int64_t;
using vi = vector<int>;

string s1, s2;
int n, m;

int a1[4048][26], a2[4048][26];

bool f(int len) {
  rep(i, n) { // see s1[i,i+len-1]
    if (i + len - 1 >= n)
      break;
    rep(j, m) { // see s2[j,j+len-1]
      if (j + len - 1 >= m)
        break;
      bool ok = true;
      rep(k, 26) if (a1[i + len][k] - a1[i][k] != a2[j + len][k] - a2[j][k]) {
        ok = false;
        break;
      }
      if (ok)
        return true;
    }
  }
  return false;
}

int main() {
  cin >> s1 >> s2;
  n = s1.size();
  m = s2.size();

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 26; j++) {
      if (s1[i] - 'a' == j)
        a1[i + 1][j] = a1[i][j] + 1;
      else
        a1[i + 1][j] = a1[i][j];
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < 26; j++) {
      if (s2[i] - 'a' == j)
        a2[i + 1][j] = a2[i][j] + 1;
      else
        a2[i + 1][j] = a2[i][j];
    }
  }

  for (int len = min(n, m); len >= 0; len--) {
    if (f(len)) {
      cout << len << endl;
      return 0;
    }
  }
  return 0;
}