Problem p02745 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, x, y) for (int i = (x); i < (y); ++i)
#define REP(i, x, y) for (int i = (x); i <= (y); ++i)
#define MP make_pair
#define PB push_back
#define PH push
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;

const int INF = 1e9 + 7;
const int maxn = 2005;

int ans = INF;
int ab[maxn][maxn], ac[maxn][maxn], bc[maxn][maxn];
string a, b, c;

inline bool match(int mat[][maxn], int xl, int xr, int yl, int yr) {
  int zl = max(xl, yl), zr = min(xr, yr);
  return mat[zl - xl][zl - yl] >= (zr - zl + 1);
}

int main() {
  cin >> a >> b >> c;
  for (int i = a.size() - 1; i >= 0; --i)
    for (int j = b.size() - 1; j >= 0; --j)
      ab[i][j] =
          (a[i] != b[j] && a[i] != '?' && b[j] != '?' ? 0
                                                      : ab[i + 1][j + 1] + 1);
  for (int i = a.size() - 1; i >= 0; --i)
    for (int j = c.size() - 1; j >= 0; --j)
      ac[i][j] =
          (a[i] != c[j] && a[i] != '?' && c[j] != '?' ? 0
                                                      : ac[i + 1][j + 1] + 1);
  for (int i = b.size() - 1; i >= 0; --i)
    for (int j = c.size() - 1; j >= 0; --j)
      bc[i][j] =
          (b[i] != c[j] && b[i] != '?' && c[j] != '?' ? 0
                                                      : bc[i + 1][j + 1] + 1);
  int tot = a.size() + b.size() + c.size();
  FOR(i, 0, tot) FOR(j, 0, tot) {
    int al = i, ar = i + a.size() - 1, bl = j, br = j + b.size() - 1, cl = 0,
        cr = 0 + c.size() - 1;
    if (!match(ab, al, ar, bl, br))
      continue;
    if (!match(ac, al, ar, cl, cr))
      continue;
    if (!match(bc, bl, br, cl, cr))
      continue;
    ans = min(ans, max(ar, max(br, cr)) + 1);
  }
  FOR(i, 0, tot) FOR(j, 0, tot) {
    int al = i, ar = i + a.size() - 1, bl = 0, br = 0 + b.size() - 1, cl = j,
        cr = j + c.size() - 1;
    if (!match(ab, al, ar, bl, br))
      continue;
    if (!match(ac, al, ar, cl, cr))
      continue;
    if (!match(bc, bl, br, cl, cr))
      continue;
    ans = min(ans, max(ar, max(br, cr)) + 1);
  }
  FOR(i, 0, tot) FOR(j, 0, tot) {
    int al = 0, ar = 0 + a.size() - 1, bl = i, br = i + b.size() - 1, cl = j,
        cr = j + c.size() - 1;
    if (!match(ab, al, ar, bl, br))
      continue;
    if (!match(ac, al, ar, cl, cr))
      continue;
    if (!match(bc, bl, br, cl, cr))
      continue;
    ans = min(ans, max(ar, max(br, cr)) + 1);
  }
  printf("%d\n", ans);
  return 0;
}


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

using namespace std;

#define FOR(i, x, y) for (int i = (x); i < (y); ++i)
#define REP(i, x, y) for (int i = (x); i <= (y); ++i)
#define MP make_pair
#define PB push_back
#define PH push
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;

const int INF = 1e9 + 7;
const int maxn = 2005;

int ans = INF;
int ab[maxn][maxn], ac[maxn][maxn], bc[maxn][maxn];
string a, b, c;

inline bool match(int mat[][maxn], int xl, int xr, int yl, int yr) {
  int zl = max(xl, yl), zr = min(xr, yr);
  if (zl > zr)
    return true;
  return mat[zl - xl][zl - yl] >= (zr - zl + 1);
}

int main() {
  cin >> a >> b >> c;
  for (int i = a.size() - 1; i >= 0; --i)
    for (int j = b.size() - 1; j >= 0; --j)
      ab[i][j] =
          (a[i] != b[j] && a[i] != '?' && b[j] != '?' ? 0
                                                      : ab[i + 1][j + 1] + 1);
  for (int i = a.size() - 1; i >= 0; --i)
    for (int j = c.size() - 1; j >= 0; --j)
      ac[i][j] =
          (a[i] != c[j] && a[i] != '?' && c[j] != '?' ? 0
                                                      : ac[i + 1][j + 1] + 1);
  for (int i = b.size() - 1; i >= 0; --i)
    for (int j = c.size() - 1; j >= 0; --j)
      bc[i][j] =
          (b[i] != c[j] && b[i] != '?' && c[j] != '?' ? 0
                                                      : bc[i + 1][j + 1] + 1);
  int tot = a.size() + b.size() + c.size();
  FOR(i, 0, tot) FOR(j, 0, tot) {
    int al = i, ar = i + a.size() - 1, bl = j, br = j + b.size() - 1, cl = 0,
        cr = 0 + c.size() - 1;
    if (!match(ab, al, ar, bl, br))
      continue;
    if (!match(ac, al, ar, cl, cr))
      continue;
    if (!match(bc, bl, br, cl, cr))
      continue;
    ans = min(ans, max(ar, max(br, cr)) + 1);
  }
  FOR(i, 0, tot) FOR(j, 0, tot) {
    int al = i, ar = i + a.size() - 1, bl = 0, br = 0 + b.size() - 1, cl = j,
        cr = j + c.size() - 1;
    if (!match(ab, al, ar, bl, br))
      continue;
    if (!match(ac, al, ar, cl, cr))
      continue;
    if (!match(bc, bl, br, cl, cr))
      continue;
    ans = min(ans, max(ar, max(br, cr)) + 1);
  }
  FOR(i, 0, tot) FOR(j, 0, tot) {
    int al = 0, ar = 0 + a.size() - 1, bl = i, br = i + b.size() - 1, cl = j,
        cr = j + c.size() - 1;
    if (!match(ab, al, ar, bl, br))
      continue;
    if (!match(ac, al, ar, cl, cr))
      continue;
    if (!match(bc, bl, br, cl, cr))
      continue;
    ans = min(ans, max(ar, max(br, cr)) + 1);
  }
  printf("%d\n", ans);
  return 0;
}