Problem p02745 (C++)

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

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-15;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

template <typename T> void printv(const vector<T> &v) {
  int sz = v.size();
  for (int i = 0; i < sz; i++) {
    cout << v[i] << " \n"[i == sz - 1];
  }
}

bool check(const string &s, const string &t, int sindex, int tindex, int len) {
  for (int i = 0; i < len; i++) {
    if (s[sindex + i] == t[tindex + i])
      continue;
    if (s[sindex + i] == '?' || t[tindex + i] == '?')
      continue;
    return false;
  }
  return true;
}

int solve(string a, string b, string c) {
  int alen = a.size(), blen = b.size(), clen = c.size();
  // aの相対位置で何番目ならok
  unordered_map<int, int> ab, ac, bc;
  ab[-blen] = 1;
  ab[alen] = 1;
  for (int i = -blen + 1; i <= alen - 1; i++) {
    int aindex = max(0, i);
    int bindex = max(-i, 0);
    int len = min(alen - aindex, blen - bindex);
    if (check(a, b, aindex, bindex, len))
      ab[i] = 1;
  }
  ac[-clen] = 1;
  ac[alen] = 1;
  for (int i = -clen + 1; i <= alen - 1; i++) {
    int aindex = max(0, i);
    int cindex = max(-i, 0);
    int len = min(alen - aindex, clen - cindex);
    if (check(a, c, aindex, cindex, len))
      ac[i] = 1;
  }
  bc[-clen] = 1;
  bc[blen] = 1;
  for (int i = -clen + 1; i <= blen - 1; i++) {
    int bindex = max(0, i);
    int cindex = max(-i, 0);
    int len = min(blen - bindex, clen - cindex);
    if (check(b, c, bindex, cindex, len))
      bc[i] = 1;
  }
  int ret = INF;
  for (int i = -blen; i <= alen; i++) {
    for (int j = -clen; j <= alen; j++) {
      int diff = j - i;
      int ma = max({alen - 1, i + blen - 1, j + clen - 1});
      int mi = min({0, i, j});
      if (ab[i] && ac[j]) {
        if (diff < -clen || blen < diff || bc[diff]) {
          ret = min(ret, ma - mi + 1);
        }
      }
    }
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string a, b, c;
  cin >> a >> b >> c;
  int ret = min({solve(a, b, c), solve(a, c, b), solve(b, a, c), solve(b, c, a),
                 solve(c, a, b), solve(c, b, a)});
  cout << ret << endl;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-15;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

template <typename T> void printv(const vector<T> &v) {
  int sz = v.size();
  for (int i = 0; i < sz; i++) {
    cout << v[i] << " \n"[i == sz - 1];
  }
}

bool check(const string &s, const string &t, int sindex, int tindex, int len) {
  for (int i = 0; i < len; i++) {
    if (s[sindex + i] == t[tindex + i])
      continue;
    if (s[sindex + i] == '?' || t[tindex + i] == '?')
      continue;
    return false;
  }
  return true;
}

int solve(string a, string b, string c) {
  int alen = a.size(), blen = b.size(), clen = c.size();
  // aの相対位置で何番目ならok
  unordered_map<int, int> ab, ac, bc;
  ab[-blen] = 1;
  ab[alen] = 1;
  for (int i = -blen + 1; i <= alen - 1; i++) {
    int aindex = max(0, i);
    int bindex = max(-i, 0);
    int len = min(alen - aindex, blen - bindex);
    if (check(a, b, aindex, bindex, len))
      ab[i] = 1;
  }
  ac[-clen] = 1;
  ac[alen] = 1;
  for (int i = -clen + 1; i <= alen - 1; i++) {
    int aindex = max(0, i);
    int cindex = max(-i, 0);
    int len = min(alen - aindex, clen - cindex);
    if (check(a, c, aindex, cindex, len))
      ac[i] = 1;
  }
  bc[-clen] = 1;
  bc[blen] = 1;
  for (int i = -clen + 1; i <= blen - 1; i++) {
    int bindex = max(0, i);
    int cindex = max(-i, 0);
    int len = min(blen - bindex, clen - cindex);
    if (check(b, c, bindex, cindex, len))
      bc[i] = 1;
  }
  int ret = INF;
  for (int i = -blen; i <= alen; i++) {
    for (int j = -clen; j <= alen; j++) {
      int diff = j - i;
      int ma = max({alen - 1, i + blen - 1, j + clen - 1});
      int mi = min({0, i, j});
      if (ab[i] && ac[j]) {
        if (diff < -clen || blen < diff || bc[diff]) {
          ret = min(ret, ma - mi + 1);
        }
      }
    }
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string a, b, c;
  cin >> a >> b >> c;
  int ret = min({solve(a, b, c), solve(b, a, c), solve(c, a, b)});
  cout << ret << endl;
}