Problem p03565 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n1, n2) for (int i = n1; i < n2; i++)
#define speed_up                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef pair<int, int> Pi;
typedef long long int ll;
const int INF = (ll)(1 << 30) - 1;
const ll INFl = (ll)9223372036854775807;
const int MAX = 10000;
const ll MOD = (ll)1e9 + 7;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

string s, t;
vector<string> v;

int main() {
  cin >> s >> t;
  for (int i = 0; i <= s.size() - t.size(); i++) {
    int flag = 0;
    for (int j = 0; j < t.size(); j++) {
      if (s[i + j] != '?' && s[i + j] != t[j]) {
        flag = 1;
      }
    }
    if (flag)
      continue;

    string tt = s;
    tt.replace(i, t.size(), t);
    for (int j = 0; j < tt.size(); j++) {
      if (tt[j] == '?') {
        tt[j] = 'a';
      }
    }
    v.push_back(tt);
  }
  if (v.empty()) {
    cout << "UNRESTORABLE" << endl;
    return 0;
  }
  sort(v.begin(), v.end());
  cout << v[0] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n1, n2) for (int i = n1; i < n2; i++)
#define speed_up                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef pair<int, int> Pi;
typedef long long int ll;
const int INF = (ll)(1 << 30) - 1;
const ll INFl = (ll)9223372036854775807;
const int MAX = 10000;
const ll MOD = (ll)1e9 + 7;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

string s, t;
vector<string> v;

int main() {
  cin >> s >> t;
  if (s.size() < t.size()) {

    cout << "UNRESTORABLE" << endl;
    return 0;
  }
  for (int i = 0; i <= s.size() - t.size(); i++) {
    int flag = 0;
    for (int j = 0; j < t.size(); j++) {
      if (s[i + j] != '?' && s[i + j] != t[j]) {
        flag = 1;
      }
    }
    if (flag)
      continue;

    string tt = s;
    tt.replace(i, t.size(), t);
    for (int j = 0; j < tt.size(); j++) {
      if (tt[j] == '?') {
        tt[j] = 'a';
      }
    }
    v.push_back(tt);
  }
  if (v.empty()) {
    cout << "UNRESTORABLE" << endl;
    return 0;
  }
  sort(v.begin(), v.end());
  cout << v[0] << endl;
  return 0;
}