Problem p02745 (C++)

Buggy submission (status: Runtime Error):
/*                         _
                        _ooOoo_
                       o8888888o
                       88" . "88
                       (| -_- |)
                       O\  =  /O
                    ____/`---'\____
                  .'  \\|     |//  `.
                 /  \\|||  :  |||//  \
                /  _||||| -:- |||||_  \
                |   | \\\  -  /'| |   |
                | \_|  `\`---'//  |_/ |
                \  .-\__ `-. -'__/-.  /
              ___`. .'  /--.--\  `. .'___
           ."" '<  `.___\_<|>_/___.' _> \"".
          | | :  `- \`. ;`. _/; .'/ /  .' ; |
          \  \ `-.   \_\_`. _.'_/_/  -' _.' /
===========`-.`___`-.__\ \___  /__.-'_.'_.-'================

                  Please give me AC.
*/

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;
using uint64 = unsigned long long;
using vi = vector<int>;
using vl = vector<int64>;
using pii = pair<int, int>;
using pll = pair<int64, int64>;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) (v).begin(), (v).end()
#define print(x) cout << (x) << '\n'
#define print2(x, y) cout << (x) << ' ' << (y) << '\n'
#define print3(x, y, z) cout << (x) << ' ' << (y) << ' ' << (z) << '\n'
#define printn(v)                                                              \
  rep(i, (v).size() - 1) cout << (v)[i] << ' ';                                \
  cout << (v)[n - 1] << '\n';
#define debug(x) cerr << #x << ": " << (x) << '\n'
#define debug2(x, y)                                                           \
  cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << '\n'
#define debug3(x, y, z)                                                        \
  cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << ", " << #z << ": " \
       << (z) << '\n'
#define dbg(v)                                                                 \
  for (size_t _ = 0; _ < v.size(); ++_) {                                      \
    cerr << #v << "[" << _ << "] : " << v[_] << '\n';                          \
  }
// constant
const int INF = (1 << 30) - 1;
const int64 INF64 = (1LL << 62) - 1;

template <typename T> T gcd(T a, T b) {
  if (a < b)
    return gcd(b, a);
  T r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

template <typename T> T lcm(const T a, const T b) { return a / gcd(a, b) * b; }

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b)
    return a = b, true;
  else
    return false;
}

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b)
    return a = b, true;
  else
    return false;
}

// End of template.

bool _ab[8080], _ac[8080], _bc[8080];
bool *ab = _ab + 4040, *ac = _ac + 4040, *bc = _bc + 4040;

string _;

bool match(const string &a, const string &b) {
  // assert(a.size() == b.size())
  rep(i, a.size()) {
    if (a[i] != '?' && b[i] != '?' && a[i] != b[i])
      return false;
  }
  return true;
}

void calc(bool *res, const string &a, const string &b) {
  const string s = _ + a + _;
  for (int i = -(int)b.size(); i <= (int)a.size(); ++i) {
    res[i] = match(s.substr(2000 + i, b.size()), b);
  }
}

int main() {
  cout << fixed << setprecision(15);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // cin.read(buf, sizeof buf); // 注意: ./a.out < in か pbp | ./a.out
  // で入力すること

  _ = "??????????????????????????????????????????????????";
  _ = _ + _ + _ + _ + _ + _ + _ + _ + _ + _;
  _ = _ + _ + _ + _; // "?" * 2000
  string a, b, c;
  cin >> a >> b >> c;
  int as = a.size(), bs = b.size(), cs = c.size();
  memset(_ab, 1, sizeof(_ab));
  memset(_ac, 1, sizeof(_ac));
  memset(_bc, 1, sizeof(_bc));
  calc(ab, a, b);
  calc(ac, a, c);
  calc(bc, b, c);

  int ans = INF;
  for (int B = -4000; B <= 4000; ++B)
    if (ab[B]) {
      for (int C = -B - cs; C <= min(4000, max(as, bs + B)); ++C) {
        if (ac[C] && bc[C - B]) {
          int m = min({0, B, C});
          int M = max({as, B + bs, C + cs});
          chmin(ans, M - m);
        }
      }
    }

  print(ans);

  return 0;
}


Fixed submission:
/*                         _
                        _ooOoo_
                       o8888888o
                       88" . "88
                       (| -_- |)
                       O\  =  /O
                    ____/`---'\____
                  .'  \\|     |//  `.
                 /  \\|||  :  |||//  \
                /  _||||| -:- |||||_  \
                |   | \\\  -  /'| |   |
                | \_|  `\`---'//  |_/ |
                \  .-\__ `-. -'__/-.  /
              ___`. .'  /--.--\  `. .'___
           ."" '<  `.___\_<|>_/___.' _> \"".
          | | :  `- \`. ;`. _/; .'/ /  .' ; |
          \  \ `-.   \_\_`. _.'_/_/  -' _.' /
===========`-.`___`-.__\ \___  /__.-'_.'_.-'================

                  Please give me AC.
*/

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;
using uint64 = unsigned long long;
using vi = vector<int>;
using vl = vector<int64>;
using pii = pair<int, int>;
using pll = pair<int64, int64>;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) (v).begin(), (v).end()
#define print(x) cout << (x) << '\n'
#define print2(x, y) cout << (x) << ' ' << (y) << '\n'
#define print3(x, y, z) cout << (x) << ' ' << (y) << ' ' << (z) << '\n'
#define printn(v)                                                              \
  rep(i, (v).size() - 1) cout << (v)[i] << ' ';                                \
  cout << (v)[n - 1] << '\n';
#define debug(x) cerr << #x << ": " << (x) << '\n'
#define debug2(x, y)                                                           \
  cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << '\n'
#define debug3(x, y, z)                                                        \
  cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << ", " << #z << ": " \
       << (z) << '\n'
#define dbg(v)                                                                 \
  for (size_t _ = 0; _ < v.size(); ++_) {                                      \
    cerr << #v << "[" << _ << "] : " << v[_] << '\n';                          \
  }
// constant
const int INF = (1 << 30) - 1;
const int64 INF64 = (1LL << 62) - 1;

template <typename T> T gcd(T a, T b) {
  if (a < b)
    return gcd(b, a);
  T r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

template <typename T> T lcm(const T a, const T b) { return a / gcd(a, b) * b; }

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b)
    return a = b, true;
  else
    return false;
}

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b)
    return a = b, true;
  else
    return false;
}

// End of template.

bool _ab[8080], _ac[8080], _bc[8080];
bool *ab = _ab + 4040, *ac = _ac + 4040, *bc = _bc + 4040;

string _;

bool match(const string &a, const string &b) {
  // assert(a.size() == b.size())
  rep(i, a.size()) {
    if (a[i] != '?' && b[i] != '?' && a[i] != b[i])
      return false;
  }
  return true;
}

void calc(bool *res, const string &a, const string &b) {
  const string s = _ + a + _;
  for (int i = -(int)b.size(); i <= (int)a.size(); ++i) {
    res[i] = match(s.substr(2000 + i, b.size()), b);
  }
}

int main() {
  cout << fixed << setprecision(15);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // cin.read(buf, sizeof buf); // 注意: ./a.out < in か pbp | ./a.out
  // で入力すること

  _ = "??????????????????????????????????????????????????";
  _ = _ + _ + _ + _ + _ + _ + _ + _ + _ + _;
  _ = _ + _ + _ + _; // "?" * 2000
  string a, b, c;
  cin >> a >> b >> c;
  int as = a.size(), bs = b.size(), cs = c.size();
  memset(_ab, 1, sizeof(_ab));
  memset(_ac, 1, sizeof(_ac));
  memset(_bc, 1, sizeof(_bc));
  calc(ab, a, b);
  calc(ac, a, c);
  calc(bc, b, c);

  int ans = INF;
  for (int B = -4000; B <= 4000; ++B)
    if (ab[B]) {
      for (int C = -4000; C <= 4000; ++C) {
        if (ac[C] && -4000 <= C - B && C - B <= 4000 && bc[C - B]) {
          int m = min({0, B, C});
          int M = max({as, B + bs, C + cs});
          chmin(ans, M - m);
        }
      }
    }

  print(ans);

  return 0;
}