Problem p02745 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

#define rep_(i, a_, b_, a, b, ...)                                             \
  for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...)                                          \
  for (int i = (b - 1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef long double ld;

bool match(char a, char b) { return (a == '?' || b == '?' || a == b); }
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string a, b, c;
  cin >> a >> b >> c;
  int A = a.length(), B = b.length(), C = c.length();
  int m = 2000, M = 10000;

  vector<bool> match_ab(6000), match_bc(6000), match_ca(6000);
  rep(i, A) {
    rep(j, B) {
      if (!(match(a[i], b[j])))
        match_ab[i - j + M] = true;
    }
  }
  rep(i, B) {
    rep(j, C) {
      if (!(match(b[i], c[j])))
        match_bc[i - j + M] = true;
    }
  }
  rep(i, C) {
    rep(j, A) {
      if (!(match(c[i], a[j])))
        match_ca[i - j + M] = true;
    }
  }

  int ans = 3 * m;
  rep(i, -2 * m, 2 * m + 1) {
    rep(j, -2 * m, 2 * m + 1) {
      if (!match_ab[i + M] && !match_bc[-i + j + M] && !match_ca[-j + M]) {
        if (chmin(ans, max({A, i + B, j + C}) - min({0, i, j})))
          eprintf("%d %d %d\n", ans, i, j);
      }
    }
  }
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

#define rep_(i, a_, b_, a, b, ...)                                             \
  for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...)                                          \
  for (int i = (b - 1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef long double ld;

bool match(char a, char b) { return (a == '?' || b == '?' || a == b); }
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string a, b, c;
  cin >> a >> b >> c;
  int A = a.length(), B = b.length(), C = c.length();
  int m = 2000, M = 10000;

  vector<bool> match_ab(2 * M + 1), match_bc(2 * M + 1), match_ca(2 * M + 1);
  rep(i, A) {
    rep(j, B) {
      if (!(match(a[i], b[j])))
        match_ab[i - j + M] = true;
    }
  }
  rep(i, B) {
    rep(j, C) {
      if (!(match(b[i], c[j])))
        match_bc[i - j + M] = true;
    }
  }
  rep(i, C) {
    rep(j, A) {
      if (!(match(c[i], a[j])))
        match_ca[i - j + M] = true;
    }
  }

  int ans = 3 * m;
  rep(i, -2 * m, 2 * m + 1) {
    rep(j, -2 * m, 2 * m + 1) {
      if (!match_ab[i + M] && !match_bc[-i + j + M] && !match_ca[-j + M]) {
        if (chmin(ans, max({A, i + B, j + C}) - min({0, i, j})))
          eprintf("%d %d %d\n", ans, i, j);
      }
    }
  }
  cout << ans << "\n";
  return 0;
}