Problem p02745 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;

int ok[6][4002]; // 0:(a,b),1:(b,a),2:(a,c),3:(c,a),4:(b,c),5:(c,b)

void CompStr(string a, string b, int i) {
  rep(j, a.size()) for (int k = j; k < a.size(); k++) {
    if ((a.at(k) != '?') && (b.at(k - j) != '?') && (a.at(k) != b.at(k - j))) {
      ok[i][j] = 0;
      break;
    }
  }
}

int minLen(int ab, int bc, int ac, int as, int bs, int cs) {
  int min = as + bs + cs;
  rep(i, as + 1) rep(j, max(bs + 1, as - i + 1)) {
    if ((ok[ab][i] == 1) && (ok[bc][j] == 1) && (ok[ac][i + j] == 1)) {
      int len =
          max(max(as, i + bs),
              i + j + cs); // calculate the length of the corresponding string
      if (min > len)
        min = len;
    }
  }
  return min;
}

int main() {
  string a, b, c;
  int as, bs, cs;
  cin >> a >> b >> c;
  as = a.size();
  bs = b.size();
  cs = c.size();
  rep(i, 6) rep(j, 4002) ok[i][j] = 1;
  CompStr(a, b, 0);
  CompStr(b, a, 1);
  CompStr(a, c, 2);
  CompStr(c, a, 3);
  CompStr(b, c, 4);
  CompStr(c, b, 5);
  int min = as + bs + cs;
  int minl;
  // 0:(a,b),1:(b,a),2:(a,c),3:(c,a),4:(b,c),5:(c,b)
  minl = minLen(0, 4, 2, as, bs, cs);
  if (min > minl)
    min = minl; // abc
  minl = minLen(2, 5, 0, as, cs, bs);
  if (min > minl)
    min = minl; // acb
  minl = minLen(1, 2, 4, bs, as, cs);
  if (min > minl)
    min = minl; // bac
  minl = minLen(4, 3, 1, bs, cs, as);
  if (min > minl)
    min = minl; // bca
  minl = minLen(3, 0, 5, cs, as, bs);
  if (min > minl)
    min = minl; // cab
  minl = minLen(5, 1, 3, cs, bs, as);
  if (min > minl)
    min = minl; // cba
  cout << min << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;

int ok[6][4002]; // 0:(a,b),1:(b,a),2:(a,c),3:(c,a),4:(b,c),5:(c,b)

void CompStr(string a, string b, int i) {
  rep(j, a.size()) for (int k = j; k < min(a.size(), b.size() + j); k++) {
    if ((a.at(k) != '?') && (b.at(k - j) != '?') && (a.at(k) != b.at(k - j))) {
      ok[i][j] = 0;
      break;
    }
  }
}

int minLen(int ab, int bc, int ac, int as, int bs, int cs) {
  int min = as + bs + cs;
  rep(i, as + 1) rep(j, max(bs + 1, as - i + 1)) {
    if ((ok[ab][i] == 1) && (ok[bc][j] == 1) && (ok[ac][i + j] == 1)) {
      int len =
          max(max(as, i + bs),
              i + j + cs); // calculate the length of the corresponding string
      if (min > len)
        min = len;
    }
  }
  return min;
}

int main() {
  string a, b, c;
  int as, bs, cs;
  cin >> a >> b >> c;
  as = a.size();
  bs = b.size();
  cs = c.size();
  rep(i, 6) rep(j, 4002) ok[i][j] = 1;
  CompStr(a, b, 0);
  CompStr(b, a, 1);
  CompStr(a, c, 2);
  CompStr(c, a, 3);
  CompStr(b, c, 4);
  CompStr(c, b, 5);
  int min = as + bs + cs;
  int minl;
  // 0:(a,b),1:(b,a),2:(a,c),3:(c,a),4:(b,c),5:(c,b)
  minl = minLen(0, 4, 2, as, bs, cs);
  if (min > minl)
    min = minl; // abc
  minl = minLen(2, 5, 0, as, cs, bs);
  if (min > minl)
    min = minl; // acb
  minl = minLen(1, 2, 4, bs, as, cs);
  if (min > minl)
    min = minl; // bac
  minl = minLen(4, 3, 1, bs, cs, as);
  if (min > minl)
    min = minl; // bca
  minl = minLen(3, 0, 5, cs, as, bs);
  if (min > minl)
    min = minl; // cab
  minl = minLen(5, 1, 3, cs, bs, as);
  if (min > minl)
    min = minl; // cba
  cout << min << endl;
  return 0;
}