Problem p02745 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#define lson rt << 1
#define rson rt << 1 | 1
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int maxn = 43 + 10;
char a[maxn], b[maxn], c[maxn];
bool xab[maxn], xba[maxn], xbc[maxn], xac[maxn], xcb[maxn];
int main() {
  cin >> a >> b >> c;
  int lena = strlen(a), lenb = strlen(b), lenc = strlen(c);
  int len = max(max(strlen(a), strlen(b)), strlen(c));
  for (int i = -len; i <= len; i++) {
    int flag = 0;
    for (int j = 0; j < lena; j++) {
      if (0 <= j + i && j + i < lenb) {
        if (a[j] != '?' && a[j] != b[i + j] && b[i + j] != '?') {
          flag = 1;
          break;
        }
      }
    }
    if (flag == 0) {
      xab[i + len] = 1;
    }
  }

  for (int i = -len; i <= len; i++) {
    int flag = 0;
    for (int j = 0; j < lenb; j++) {
      if (0 <= j + i && j + i < lenc) {
        if (b[j] != '?' && b[j] != c[i + j] && c[i + j] != '?') {
          flag = 1;
          break;
        }
      }
    }
    if (flag == 0) {
      xbc[i + len] = 1;
    }
  }

  for (int i = -len; i <= len; i++) {
    int flag = 0;
    for (int j = 0; j < lena; j++) {
      if (0 <= j + i && j + i < lenc) {
        if (a[j] != '?' && a[j] != c[i + j] && c[i + j] != '?') {
          flag = 1;
          break;
        }
      }
    }
    if (flag == 0)
      xac[i + len] = 1;
  }

  int ans = 1e9;
  for (int i = -len * 2; i <= len * 2; i++)
    for (int j = -len * 2; j <= len * 2; j++) {
      int l = 0, r = lena;
      l = min(min(-i, -j), l);
      r = max(r, max(-i + lenb, -j + lenc));
      if (-len <= i && i <= len && xab[i + len] == 0)
        continue;
      int p = j - i;
      if (-len <= p && p <= len && xbc[p + len] == 0)
        continue;

      if (-len <= j && j <= len && xac[j + len] == 0)
        continue;

      ans = min(r - l, ans);
    }
  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#define lson rt << 1
#define rson rt << 1 | 1
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int maxn = 4e3 + 10;
char a[maxn], b[maxn], c[maxn];
bool xab[maxn], xba[maxn], xbc[maxn], xac[maxn], xcb[maxn];
int main() {
  cin >> a >> b >> c;
  int lena = strlen(a), lenb = strlen(b), lenc = strlen(c);
  int len = max(max(strlen(a), strlen(b)), strlen(c));
  for (int i = -len; i <= len; i++) {
    int flag = 0;
    for (int j = 0; j < lena; j++) {
      if (0 <= j + i && j + i < lenb) {
        if (a[j] != '?' && a[j] != b[i + j] && b[i + j] != '?') {
          flag = 1;
          break;
        }
      }
    }
    if (flag == 0) {
      xab[i + len] = 1;
    }
  }

  for (int i = -len; i <= len; i++) {
    int flag = 0;
    for (int j = 0; j < lenb; j++) {
      if (0 <= j + i && j + i < lenc) {
        if (b[j] != '?' && b[j] != c[i + j] && c[i + j] != '?') {
          flag = 1;
          break;
        }
      }
    }
    if (flag == 0) {
      xbc[i + len] = 1;
    }
  }

  for (int i = -len; i <= len; i++) {
    int flag = 0;
    for (int j = 0; j < lena; j++) {
      if (0 <= j + i && j + i < lenc) {
        if (a[j] != '?' && a[j] != c[i + j] && c[i + j] != '?') {
          flag = 1;
          break;
        }
      }
    }
    if (flag == 0)
      xac[i + len] = 1;
  }

  int ans = 1e9;
  for (int i = -len * 2; i <= len * 2; i++)
    for (int j = -len * 2; j <= len * 2; j++) {
      int l = 0, r = lena;
      l = min(min(-i, -j), l);
      r = max(r, max(-i + lenb, -j + lenc));
      if (-len <= i && i <= len && xab[i + len] == 0)
        continue;
      int p = j - i;
      if (-len <= p && p <= len && xbc[p + len] == 0)
        continue;

      if (-len <= j && j <= len && xac[j + len] == 0)
        continue;

      ans = min(r - l, ans);
    }
  cout << ans;
}