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

#include <bits/stdc++.h>
#define int long long
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef pair<int, int> pii;
static const int INF = 1ll << 60;

int dp[2][2][2][2001][2001];
int N, M;
string S, T;

int solve(int n1, int n2, int p, int s, int t) {
  // if(sum.size()>=15)cout<<sum<<" "<<p<<endl;
  if (dp[n1][n2][p][s][t] != -1)
    return dp[n1][n2][p][s][t];
  if (s > N || t > M) {
    return -INF;
  }
  if (s == N && t == M) {
    if (p == 0)
      return -INF;
    else
      return 0;
  }
  int res = 0;
  int sx, tx;
  {
    if (s == N)
      sx = p;
    if (t == M)
      tx = p;
    if (S[s] == 'I')
      sx = 1;
    if (S[s] == 'O')
      sx = 0;
    if (T[t] == 'I')
      tx = 1;
    if (T[t] == 'O')
      tx = 0;
  }
  if (n1 == 0 && n2 == 0) {
    if (p != sx && p != tx) {
      res += max(solve(0, 0, p, s + 1, t),
                 max(solve(0, 0, p, s, t + 1),
                     max(solve(1, 0, sx, s + 1, t) + 1,
                         max(solve(0, 1, tx, s, t + 1) + 1, res))));
    } else if (p != sx && p == tx) {
      res += max(solve(0, 0, p, s + 1, t),
                 max(solve(0, 0, p, s, t + 1),
                     max(solve(1, 0, sx, s + 1, t) + 1, res)));
    } else if (p == sx && p != tx) {
      res += max(solve(0, 0, p, s + 1, t),
                 max(solve(0, 0, p, s, t + 1),
                     max(solve(0, 1, tx, s, t + 1) + 1, res)));
    } else {
      res += max(solve(0, 0, p, s + 1, t), max(solve(0, 0, p, s, t + 1), res));
    }
  }

  else if (n1 == 0 && n2 != 0) {
    if (p != sx && p != tx) {
      res += max(solve(0, n2, p, s + 1, t),
                 max(solve(1, n2, sx, s + 1, t) + 1,
                     max(solve(0, n2, tx, s, t + 1) + 1, res)));
    } else if (p != sx && p == tx) {
      res += max(solve(0, n2, p, s + 1, t),
                 max(solve(1, n2, sx, s + 1, t) + 1, res));
    } else if (p == sx && p != tx) {
      res += max(solve(0, n2, p, s + 1, t),
                 max(solve(0, n2, tx, s, t + 1) + 1, res));
    } else {
      res += max(solve(0, n2, p, s + 1, t), res);
    }
  }

  else if (n1 != 0 && n2 == 0) {
    if (p != sx && p != tx) {
      res += max(solve(n1, 0, p, s, t + 1),
                 max(solve(n1, 0, sx, s + 1, t) + 1,
                     max(solve(n1, 1, tx, s, t + 1) + 1, res)));
    } else if (p != sx && p == tx) {
      res += max(solve(n1, 0, p, s, t + 1),
                 max(solve(n1, 0, sx, s + 1, t) + 1, res));
    } else if (p == sx && p != tx) {
      res += max(solve(n1, 0, p, s, t + 1),
                 max(solve(n1, 1, tx, s, t + 1) + 1, res));
    } else {
      res += max(solve(n1, 0, p, s, t + 1), res);
    }
  } else {
    if (p != sx && p != tx) {
      res += max(solve(n1, n2, sx, s + 1, t) + 1,
                 max(solve(n1, n2, tx, s, t + 1) + 1, res));
    } else if (p != sx && p == tx) {
      res += max(solve(n1, n2, sx, s + 1, t) + 1, res);
    } else if (p == sx && p != tx) {
      res += max(solve(n1, n2, tx, s, t + 1) + 1, res);
    } else if (p == 0)
      return -INF;
  }
  return dp[n1][n2][p][s][t] = res;
}

signed main() {
  memset(dp, -1, sizeof(dp));
  cin >> N >> M;
  cin >> S >> T;
  cout << solve(0, 0, 0, 0, 0) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef pair<int, int> pii;
static const int INF = 1ll << 60;

int dp[2][2][2][2001][2001];
int N, M;
string S, T;

int solve(int n1, int n2, int p, int s, int t, string sum) {
  // if(sum.size()>=15)cout<<sum<<" "<<p<<endl;
  if (dp[n1][n2][p][s][t] != -1)
    return dp[n1][n2][p][s][t];
  if (s > N || t > M) {
    return -INF;
  }
  if (s == N && t == M) {
    if (p == 0)
      return -INF;
    else
      return 0;
  }
  int res = 0;
  int sx, tx;
  {
    if (s == N)
      sx = p;
    if (t == M)
      tx = p;
    if (S[s] == 'I')
      sx = 1;
    if (S[s] == 'O')
      sx = 0;
    if (T[t] == 'I')
      tx = 1;
    if (T[t] == 'O')
      tx = 0;
  }
  if (n1 == 0 && n2 == 0) {
    if (p != sx && p != tx) {
      res += max(solve(0, 0, p, s + 1, t, sum),
                 max(solve(0, 0, p, s, t + 1, sum),
                     max(solve(1, 0, sx, s + 1, t, sum + S[s]) + 1,
                         max(solve(0, 1, tx, s, t + 1, sum + T[t]) + 1, res))));
    } else if (p != sx && p == tx) {
      res += max(solve(0, 0, p, s + 1, t, sum),
                 max(solve(0, 0, p, s, t + 1, sum),
                     max(solve(1, 0, sx, s + 1, t, sum + S[s]) + 1, res)));
    } else if (p == sx && p != tx) {
      res += max(solve(0, 0, p, s + 1, t, sum),
                 max(solve(0, 0, p, s, t + 1, sum),
                     max(solve(0, 1, tx, s, t + 1, sum + T[t]) + 1, res)));
    } else {
      res += max(solve(0, 0, p, s + 1, t, sum),
                 max(solve(0, 0, p, s, t + 1, sum), res));
    }
  }

  else if (n1 == 0 && n2 != 0) {
    if (p != sx && p != tx) {
      res += max(solve(0, n2, p, s + 1, t, sum),
                 max(solve(1, n2, sx, s + 1, t, sum + S[s]) + 1,
                     max(solve(0, n2, tx, s, t + 1, sum + T[t]) + 1, res)));
    } else if (p != sx && p == tx) {
      res += max(solve(0, n2, p, s + 1, t, sum),
                 max(solve(1, n2, sx, s + 1, t, sum + S[s]) + 1, res));
    } else if (p == sx && p != tx) {
      res += max(solve(0, n2, p, s + 1, t, sum),
                 max(solve(0, n2, tx, s, t + 1, sum + T[t]) + 1, res));
    } else {
      res += max(solve(0, n2, p, s + 1, t, sum), res);
    }
  }

  else if (n1 != 0 && n2 == 0) {
    if (p != sx && p != tx) {
      res += max(solve(n1, 0, p, s, t + 1, sum),
                 max(solve(n1, 0, sx, s + 1, t, sum + S[s]) + 1,
                     max(solve(n1, 1, tx, s, t + 1, sum + T[t]) + 1, res)));
    } else if (p != sx && p == tx) {
      res += max(solve(n1, 0, p, s, t + 1, sum),
                 max(solve(n1, 0, sx, s + 1, t, sum + S[s]) + 1, res));
    } else if (p == sx && p != tx) {
      res += max(solve(n1, 0, p, s, t + 1, sum),
                 max(solve(n1, 1, tx, s, t + 1, sum + T[t]) + 1, res));
    } else {
      res += max(solve(n1, 0, p, s, t + 1, sum), res);
    }
  } else {
    if (p != sx && p != tx) {
      res += max(solve(n1, n2, sx, s + 1, t, sum + S[s]) + 1,
                 max(solve(n1, n2, tx, s, t + 1, sum + T[t]) + 1, res));
    } else if (p != sx && p == tx) {
      res += max(solve(n1, n2, sx, s + 1, t, sum + S[s]) + 1, res);
    } else if (p == sx && p != tx) {
      res += max(solve(n1, n2, tx, s, t + 1, sum + T[t]) + 1, res);
    } else if (p == 0)
      return -INF;
  }
  return dp[n1][n2][p][s][t] = res;
}

signed main() {
  memset(dp, -1, sizeof(dp));
  cin >> N >> M;
  cin >> S >> T;
  cout << solve(0, 0, 0, 0, 0, "") << endl;
}