Problem p02745 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define ff first
#define ss second
#define pb push_back

using namespace std;
using ll = long long;
using ii = pair<int, int>;

const int N = 2005;

int dp[3][3][N];

int ok(string t, string p, int ini, int a, int b) {
  if (ini < 0) {
    swap(t, p);
    swap(a, b);
    ini = abs(ini);
  }
  if (ini >= t.size())
    return 1;
  if (dp[a][b][ini] != -1)
    return dp[a][b][ini];
  int id = 0;
  for (int i = ini; i < (int)t.size(); i++) {
    if (id == p.size())
      break;
    if (p[id] != t[i] && t[i] != '?' && p[id] != '?') {
      return dp[a][b][ini] = 0;
    }
    id++;
  }
  return dp[a][b][ini] = 1;
}

int main() {
  memset(dp, -1, sizeof dp);
  char a[3][N];
  scanf("%s", a[0]);
  scanf("%s", a[1]);
  scanf("%s", a[2]);
  string sa[3];
  sa[0] = a[0];
  sa[1] = a[1];
  sa[2] = a[2];
  int p[3] = {0, 1, 2};
  int ans = 3 * N;
  do {
    for (int i = 0; i <= (int)sa[p[0]].size(); i++) {
      if (ok(sa[p[0]], sa[p[1]], i, p[0], p[1])) {
        int sz = max(sa[p[0]].size(), i + sa[p[1]].size());
        string aux0 = sa[p[0]];
        string aux1 = sa[p[1]];
        int id = 0;
        for (int j = i; j < (int)sa[p[0]].size(); j++) {
          if (id == aux1.size())
            break;
          if (aux0[j] == '?' && aux1[id] != '?') {
            aux0[j] = aux1[id];
          } else if (aux0[j] != '?' && aux1[id] == '?') {
            aux1[id] = aux0[j];
          }
          id++;
        }
        memset(dp, -1, sizeof dp);
        for (int j = 0; j <= sz; j++) {
          if (ok(sa[p[0]], sa[p[2]], j, p[0], p[2]) &&
              ok(sa[p[1]], sa[p[2]], j - i, p[1], p[2])) {
            ans = min(ans, max(sz, (int)j + (int)sa[p[2]].size()));
          }
        }
      }
    }
  } while (next_permutation(p, p + 3));
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define ff first
#define ss second
#define pb push_back

using namespace std;
using ll = long long;
using ii = pair<int, int>;

const int N = 2005;

int dp[3][3][N];

int ok(string t, string p, int ini, int a, int b) {
  if (ini < 0) {
    swap(t, p);
    swap(a, b);
    ini = abs(ini);
  }
  if (ini >= t.size())
    return 1;
  if (dp[a][b][ini] != -1)
    return dp[a][b][ini];
  int id = 0;
  for (int i = ini; i < (int)t.size(); i++) {
    if (id == p.size())
      break;
    if (p[id] != t[i] && t[i] != '?' && p[id] != '?') {
      return dp[a][b][ini] = 0;
    }
    id++;
  }
  return dp[a][b][ini] = 1;
}

int main() {
  memset(dp, -1, sizeof dp);
  char a[3][N];
  scanf("%s", a[0]);
  scanf("%s", a[1]);
  scanf("%s", a[2]);
  string sa[3];
  sa[0] = a[0];
  sa[1] = a[1];
  sa[2] = a[2];
  int p[3] = {0, 1, 2};
  int ans = 3 * N;
  do {
    for (int i = 0; i <= (int)sa[p[0]].size(); i++) {
      if (ok(sa[p[0]], sa[p[1]], i, p[0], p[1])) {
        int sz = max(sa[p[0]].size(), i + sa[p[1]].size());
        string aux0 = sa[p[0]];
        string aux1 = sa[p[1]];
        int id = 0;
        for (int j = 0; j <= sz; j++) {
          if (ok(sa[p[0]], sa[p[2]], j, p[0], p[2]) &&
              ok(sa[p[1]], sa[p[2]], j - i, p[1], p[2])) {
            ans = min(ans, max(sz, (int)j + (int)sa[p[2]].size()));
          }
        }
      }
    }
  } while (next_permutation(p, p + 3));
  printf("%d\n", ans);
  return 0;
}