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

#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

int dp[2001][2001];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int A, B;
  cin >> A >> B;
  string s1, s2;
  cin >> s1 >> s2;

  dp[A - 1][B] = (s1.back() == 'I');
  dp[A][B - 1] = (s2.back() == 'I');

  for (int i = A; i >= 0; --i) {
    for (int j = B; j >= 0; --j) {
      int &tmp = dp[i][j];
      if (i < A && s1[i] == 'I')
        maxi(tmp, 1);
      if (j < B && s2[j] == 'I')
        maxi(tmp, 1);

      if (i < A && j < B) {
        if (s1[i] == s2[j] && s1[i] == 'I') {
          maxi(tmp, 1);
        } else if (s1[i] != s2[j]) {
          if (dp[i + 1][j + 1])
            maxi(tmp, dp[i + 1][j + 1] + 2);
        }
      }

      if (i + 2 <= A && s1[i] == 'I' && s1[i + 1] == 'O')
        if (dp[i + 2][j])
          maxi(tmp, dp[i + 2][j] + 2);
      if (j + 2 <= B && s2[j] == 'I' && s2[j + 1] == 'O')
        if (dp[i][j + 2])
          maxi(tmp, dp[i][j + 2] + 2);
    }
  }

  int ans = 0;
  REP(i, A + 1) REP(j, B + 1) maxi(ans, dp[i][j]);
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

int dp[2][2001][2001];

int naive(string &s1, string &s2) {
  int n1 = SZ(s1), n2 = SZ(s2);
  int res = 0;
  REP(i, n1 + 1) FOR(ii, i, n1 + 1) REP(j, n2 + 1) FOR(jj, j, n2 + 1) {
    bool ok = false;
    int len = (ii - i) + (jj - j);
    for (int b = 0; b < (1 << len); ++b) {
      string s;
      int i1 = i, j1 = j;
      for (int k = 0; k < len; ++k)
        if (b >> k & 1) {
          if (i1 >= ii)
            break;
          s += s1[i1++];
        } else {
          if (j1 >= jj)
            break;
          s += s2[j1++];
        }

      if (SZ(s) != len)
        continue;
      ok = s[0] == 'I';
      for (int k = 0; k < len - 1; ++k)
        if (s[k] == s[k + 1]) {
          ok = false;
          break;
        }
      if (ok)
        break;
    }
    if (ok) {
      maxi(res, len);
    }
  }

  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int A, B;
  cin >> A >> B;
  string s1, s2;
  cin >> s1 >> s2;
  cout << naive(s1, s2) << endl;
  return 0;

  for (int i = A - 1; i >= 0; --i)
    dp[s1[i] == 'O'][i][B] = dp[s1[i] != 'O'][i + 1][B] + 1;
  for (int j = B - 1; j >= 0; --j)
    dp[s2[j] == 'O'][A][j] = dp[s2[j] != 'O'][A][j + 1] + 1;

  for (int i = A - 1; i >= 0; --i) {
    for (int j = B - 1; j >= 0; --j) {
      if (s1[i] == s2[j] && s1[i] == 'I') {
        dp[1][i][j] = 0;
        maxi(dp[0][i][j], max(dp[1][i + 1][j] + 1, dp[1][i][j + 1] + 1));
      } else if (s1[i] != s2[j]) {
        if (s1[i] == 'I') {
          maxi(dp[0][i][j], dp[1][i + 1][j] + 1);
          maxi(dp[1][i][j], dp[0][i][j + 1] + 1);
        } else {
          maxi(dp[1][i][j], dp[0][i + 1][j] + 1);
          maxi(dp[0][i][j], dp[1][i][j + 1] + 1);
        }
      } else {
        dp[0][i][j] = 0;
        maxi(dp[1][i][j], max(dp[0][i + 1][j] + 1, dp[0][i][j + 1] + 1));
      }
    }
  }
  /*
  REP(i,A+1){
        REP(j,B+1) cout << dp[0][i][j]<<"/"<<dp[1][i][j] << " ";
        cout << endl;
  }
  */
  int ans = 0;
  REP(i, A + 1) REP(j, B + 1) maxi(ans, dp[0][i][j]);
  int na = naive(s1, s2);
  if (ans != na) {
    cout << "HALT" << endl << na << endl;
    return 1;
  }
  cout << ans << endl;

  return 0;
}