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

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  rep(i, v.size()) { os << v[i] << (i == v.size() - 1 ? "" : " "); }
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &x : v) {
    is >> x;
  }
  return is;
}

int main() {
  string s, t;
  cin >> s >> t;

  for (int len = min(s.size(), t.size()); len >= 0; len--) {
    set<vector<int>> st;

    vector<int> cnt(26, 0);
    int i = 0;
    while (i < len) {
      cnt[s[i++] - 'a']++;
    }
    st.emplace(cnt);
    while (i < s.size()) {
      cnt[s[i] - 'a']++;
      cnt[s[i - len] - 'a']--;
      st.emplace(cnt);
      i++;
    }

    cnt = vector<int>(26, 0);
    i = 0;
    while (i < len) {
      cnt[t[i++] - 'a']++;
    }
    if (st.count(cnt)) {
      cout << len << endl;
      return 0;
    }

    while (i < t.size()) {
      cnt[t[i] - 'a']++;
      cnt[t[i - len] - 'a']--;
      if (st.count(cnt)) {
        cout << len << endl;
        return 0;
      }
      i++;
    }
  }
  cout << 0 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  rep(i, v.size()) { os << v[i] << (i == v.size() - 1 ? "" : " "); }
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &x : v) {
    is >> x;
  }
  return is;
}

const int MAX_N = 4000;

class BIT {
private:
  vector<vector<int>> bit; //[1, n]
public:
  BIT() { bit = vector<vector<int>>(MAX_N + 1, vector<int>(26, 0)); }
  vector<int> sum(int l, int r) {
    vector<int> right = sum(r);
    vector<int> left = sum(l - 1);
    rep(i, 26) { right[i] -= left[i]; }
    return right;
  }
  vector<int> sum(int i) {
    if (i == 0)
      return vector<int>(26, 0);
    assert(1 <= i and i <= MAX_N and "invalid argument");
    vector<int> s(26, 0);
    while (i > 0) {
      rep(j, 26) { s[j] += bit[i][j]; }
      i -= i & -i;
    }
    return s;
  }
  void add(int i, char c) {
    assert(1 <= i and i <= MAX_N and "invalid argument");
    while (i <= MAX_N) {
      bit[i][c - 'a']++;
      i += i & -i;
    }
  }
};

int comp(vector<int> a, vector<int> b) {
  int diff = 0;
  rep(i, 26) { diff += abs(a[i] - b[i]); }
  return diff;
}

int main() {
  string s, t;
  cin >> s >> t;

  BIT bs, bt;
  rep(i, s.size()) { bs.add(i + 1, s[i]); }
  rep(i, t.size()) { bt.add(i + 1, t[i]); }

  for (int len = min(s.size(), t.size()); len >= 0; len--) {
    rep(i, s.size() - len + 1) {
      rep(j, t.size() - len + 1) {
        int diff = comp(bs.sum(i + 1, i + len), bt.sum(j + 1, j + len));
        if (diff == 0) {
          // cout << len << ' ' << i << ' ' << j << endl;
          cout << len << endl;
          return 0;
        }
        j += max(diff - 2, 0) / 2;
      }
    }
  }
}