Problem p03565 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n); i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
typedef unsigned long long ull;

const ll INF = 1LL << 60;
const int MINF = 1e9;
const int MOD = 1e9 + 7;

using vi = vector<int>;
using vii = vector<vi>;

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

template <typename T> T GCD(T a, T b) { return b ? GCD(b, a % b) : a; }
template <typename T> T LCM(T a, T b) { return a * b / GCD(a, b); }

template <typename T> // 文字列の置換/Boost:replace_all()
T replace_str(T str, string target, string str_to) {
  string::size_type pos = 0;
  while (pos = str.find(target, pos), pos != string::npos) {
    str.replace(pos, target.length(), str_to);
    pos += str_to.length();
  }
  return str;
}

int main() {
  string s, t;
  cin >> s >> t;
  if (s.size() == 1) {
    if ((s[0] == '?' && t.size() == 1) || (s == t)) {
      cout << t << endl;
    } else {
      cout << "UNRESTORABLE" << endl;
    }
    return 0;
  }

  replace(ALL(s), '?', '.');
  string ans;
  bool exist = false;
  for (int i = 0; i <= s.size() - t.size(); i++) {
    regex re(s.substr(i, t.size()), regex_constants::extended);
    if (regex_search(t, re)) {
      ans = s.substr(0, i) + t + s.substr(i + t.size());
      exist = true;
    }
  }
  if (exist) {
    replace(ALL(ans), '.', 'a');
    cout << ans << endl;
  } else {
    cout << "UNRESTORABLE" << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n); i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
typedef unsigned long long ull;

const ll INF = 1LL << 60;
const int MINF = 1e9;
const int MOD = 1e9 + 7;

using vi = vector<int>;
using vii = vector<vi>;

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

template <typename T> T GCD(T a, T b) { return b ? GCD(b, a % b) : a; }
template <typename T> T LCM(T a, T b) { return a * b / GCD(a, b); }

template <typename T> // 文字列の置換/Boost:replace_all()
T replace_str(T str, string target, string str_to) {
  string::size_type pos = 0;
  while (pos = str.find(target, pos), pos != string::npos) {
    str.replace(pos, target.length(), str_to);
    pos += str_to.length();
  }
  return str;
}

int main() {
  string s, t;
  cin >> s >> t;
  if (s.size() == 1) {
    if ((s[0] == '?' && t.size() == 1) || (s == t)) {
      cout << t << endl;
    } else {
      cout << "UNRESTORABLE" << endl;
    }
    return 0;
  }
  if (t.size() > s.size()) {
    cout << "UNRESTORABLE" << endl;
    return 0;
  }
  replace(ALL(s), '?', '.');
  string ans;
  bool exist = false;
  for (int i = 0; i <= s.size() - t.size(); i++) {
    regex re(s.substr(i, t.size()), regex_constants::extended);
    if (regex_search(t, re)) {
      ans = s.substr(0, i) + t + s.substr(i + t.size());
      exist = true;
    }
  }
  if (exist) {
    replace(ALL(ans), '.', 'a');
    cout << ans << endl;
  } else {
    cout << "UNRESTORABLE" << endl;
  }
  return 0;
}