Problem p02745 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

std::string str_join(const std::string &s1, const std::string &s2) {
  std::string r{s1};
  auto n = s1.size();
  for (auto i = 0ul; i < n; ++i) {
    if (s2[i] == '?') {
      continue;
    }

    if (r[i] == '?') {
      r[i] = s2[i];
      continue;
    }

    if (s1[i] != s2[i]) {
      return std::string{};
    }
  }
  return r;
}

auto slide_join(const std::string &s1, const std::string &s2,
                const std::string &s3, std::size_t min) {
  auto s1_s = s1.size();
  auto s2_s = s2.size();
  auto s3_s = s3.size();
  for (auto i = 0; i < s2_s; ++i) {
    if (i + s1_s > min) {
      break;
    }
    auto n = std::min(s1_s, s2_s - i);
    auto t = str_join(s1.substr(0, n), s2.substr(i, n));
    if (t.empty()) {
      continue;
    }

    std::string r{};
    if (i) {
      r += s2.substr(0, i);
    }
    r += t;

    if (n < s1_s) {
      r += s1.substr(n);
    } else if (i + n < s2_s) {
      r += s2.substr(i + n);
    }

    auto r_s = r.size();

    for (auto j = 0; j < r_s; ++j) {
      if (j + s3_s > min) {
        break;
      }

      auto nn = std::min(s3_s, r_s - j);
      auto tt = str_join(s3.substr(0, nn), r.substr(j, nn));
      if (tt.empty()) {
        continue;
      }

      std::string rr{};
      if (j) {
        rr += r.substr(0, j);
      }
      rr += tt;

      if (nn < s3_s) {
        rr += s3.substr(nn);
      } else if (j + nn < r_s) {
        rr += r.substr(j + nn);
      }

      if (rr.size() < min) {
        min = rr.size();
        break;
      }
    } // for j

  } // for i

  return min;
}

int main() {
  std::string a, b, c;
  std::cin >> a >> b >> c;

  auto min = a.size() + b.size() + c.size();

  min = slide_join(a, b, c, min);
  min = slide_join(b, a, c, min);

  min = slide_join(b, c, a, min);
  min = slide_join(c, b, a, min);

  min = slide_join(c, a, b, min);
  min = slide_join(a, c, b, min);

  std::cout << min << std::endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

std::string str_join(const std::string &s1, const std::string &s2) {
  std::string r{s1};
  auto n = s1.size();
  for (auto i = 0ul; i < n; ++i) {
    if (s2[i] == '?') {
      continue;
    }

    if (r[i] == '?') {
      r[i] = s2[i];
      continue;
    }

    if (s1[i] != s2[i]) {
      return std::string{};
    }
  }
  return r;
}

auto slide_join(const std::string &s1, const std::string &s2,
                const std::string &s3, std::size_t min) {
  auto s1_s = s1.size();
  auto s2_s = s2.size();
  auto s3_s = s3.size();
  for (auto i = 0; i < s2_s; ++i) {
    if (i + s1_s > min) {
      break;
    }
    auto n = std::min(s1_s, s2_s - i);
    auto t = str_join(s1.substr(0, n), s2.substr(i, n));
    if (t.empty()) {
      continue;
    }

    std::string r{};
    if (i) {
      r += s2.substr(0, i);
    }
    r += t;

    if (n < s1_s) {
      r += s1.substr(n);
    } else if (i + n < s2_s) {
      r += s2.substr(i + n);
    }

    auto r_s = r.size();

    for (auto j = i; j < r_s; ++j) {
      if (j + s3_s > min) {
        break;
      }

      auto nn = std::min(s3_s, r_s - j);
      auto tt = str_join(s3.substr(0, nn), r.substr(j, nn));
      if (tt.empty()) {
        continue;
      }

      std::string rr{};
      if (j) {
        rr += r.substr(0, j);
      }
      rr += tt;

      if (nn < s3_s) {
        rr += s3.substr(nn);
      } else if (j + nn < r_s) {
        rr += r.substr(j + nn);
      }

      if (rr.size() < min) {
        min = rr.size();
        break;
      }
    } // for j

  } // for i

  return min;
}

int main() {
  std::string a, b, c;
  std::cin >> a >> b >> c;

  auto min = a.size() + b.size() + c.size();

  min = slide_join(a, b, c, min);
  min = slide_join(b, a, c, min);

  min = slide_join(b, c, a, min);
  min = slide_join(c, b, a, min);

  min = slide_join(c, a, b, min);
  min = slide_join(a, c, b, min);

  std::cout << min << std::endl;

  return 0;
}