Problem p03565 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define _USE_MATH_DEFINES
#include <math.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define NIL = -1;
#define all(x) x.begin(), x.end()
const ll INF = 1e9;
const ll mod = 1e9 + 7;

int digit(ll x) {
  if (x / 10 == 0)
    return 1;
  return digit(x / 10) + 1;
}

ll gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;

  return gcd(b, a % b);
}

bool is_prime(long long N) {
  if (N == 1)
    return false;
  for (long long i = 2; i * i <= N; i++) {
    if (N % i == 0)
      return false;
  }
  return true;
}

ll lcm(ll a, ll b) { return ((a * b == 0)) ? 0 : (a / gcd(a, b) * b); }

double DegreeToRadian(double degree) { return degree * M_PI / 180.0; }

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

///////////////////////////////////////////////////////////////////////////////////

bool ok(string S, string T) {
  for (int i = 0; i < S.size(); i++) {
    if (S[i] != '?' && S[i] != T[i]) {
      return false;
    }
  }

  return true;
}

int main() {
  string s, t, out = "~";
  cin >> s >> t;
  for (int i = 0; i <= s.size() - t.size(); i++) {
    string tmp = s.substr(i, t.size());
    if (ok(tmp, t)) {
      string rep = s;
      for (int j = 0; j < i; j++) {
        if (rep[j] == '?') {
          rep[j] = 'a';
        }
      }
      for (int j = i; j < i + t.size(); j++) {
        rep[j] = t[j - i];
      }
      for (int j = i + t.size(); j < rep.size(); j++) {
        if (rep[j] == '?') {
          rep[j] = 'a';
        }
      }
      out = min(out, rep);
    }
  }

  if (out == "~") {
    cout << "UNRESTORABLE" << endl;
  } else {
    cout << out << endl;
  }
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define _USE_MATH_DEFINES
#include <math.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define NIL = -1;
#define all(x) x.begin(), x.end()
const ll INF = 1e9;
const ll mod = 1e9 + 7;

int digit(ll x) {
  if (x / 10 == 0)
    return 1;
  return digit(x / 10) + 1;
}

ll gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;

  return gcd(b, a % b);
}

bool is_prime(long long N) {
  if (N == 1)
    return false;
  for (long long i = 2; i * i <= N; i++) {
    if (N % i == 0)
      return false;
  }
  return true;
}

ll lcm(ll a, ll b) { return ((a * b == 0)) ? 0 : (a / gcd(a, b) * b); }

double DegreeToRadian(double degree) { return degree * M_PI / 180.0; }

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

///////////////////////////////////////////////////////////////////////////////////

bool ok(string S, string T) {
  for (int i = 0; i < S.size(); i++) {
    if (S[i] != '?' && S[i] != T[i]) {
      return false;
    }
  }

  return true;
}

int main() {
  string s, t, out = "~";
  cin >> s >> t;
  if (s.size() < t.size()) {
    cout << "UNRESTORABLE" << endl;
    return 0;
  }
  for (int i = 0; i <= s.size() - t.size(); i++) {
    string tmp = s.substr(i, t.size());
    if (ok(tmp, t)) {
      string rep = s;
      for (int j = 0; j < i; j++) {
        if (rep[j] == '?') {
          rep[j] = 'a';
        }
      }
      for (int j = i; j < i + t.size(); j++) {
        rep[j] = t[j - i];
      }
      for (int j = i + t.size(); j < rep.size(); j++) {
        if (rep[j] == '?') {
          rep[j] = 'a';
        }
      }
      out = min(out, rep);
    }
  }

  if (out == "~") {
    cout << "UNRESTORABLE" << endl;
  } else {
    cout << out << endl;
  }
}