Problem p03565 (C++)

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

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
// #define int long long
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000              // 2e9
#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)
#define fi first
#define sec second
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define dmp(x) cerr << #x << ": " << x << endl;

template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

template <class T> using MaxHeap = priority_queue<T>;
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T> vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << p.fi << ',' << p.sec;
  return os;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.fi >> p.sec;
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i];
    if (i + 1 < vec.size())
      os << ' ';
  }
  return os;
}
template <class T> istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++)
    is >> vec[i];
  return is;
}
void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

#define endl "\n"

void solve() {
  string s, t;
  cin >> s >> t;
  vector<string> cand;
  for (int i = 0; i <= s.size() - t.size(); i++) {
    bool judge = true;
    for (int j = 0; j < t.size(); j++) {
      if (s[i + j] == '?')
        continue;
      if (s[i + j] != t[j]) {
        judge = false;
        break;
      }
    }
    if (!judge)
      continue;
    string tmp = s;
    for (int j = 0; j < t.size(); j++) {
      tmp[i + j] = t[j];
    }
    for (int j = 0; j < tmp.size(); j++) {
      if (tmp[j] == '?')
        tmp[j] = 'a';
    }
    cand.push_back(tmp);
  }
  sort(all(cand));
  if (cand.size() == 0)
    cout << "UNRESTORABLE" << endl;
  else
    cout << cand.front() << endl;
  return;
}

signed main() {
  fastio();
  solve();
  // int t; cin >> t; while(t--)solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
// #define int long long
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000              // 2e9
#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)
#define fi first
#define sec second
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define dmp(x) cerr << #x << ": " << x << endl;

template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

template <class T> using MaxHeap = priority_queue<T>;
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T> vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << p.fi << ',' << p.sec;
  return os;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.fi >> p.sec;
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i];
    if (i + 1 < vec.size())
      os << ' ';
  }
  return os;
}
template <class T> istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++)
    is >> vec[i];
  return is;
}
void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

#define endl "\n"

void solve() {
  string s, t;
  cin >> s >> t;
  vector<string> cand;
  for (int i = 0; i <= (int)s.size() - (int)t.size(); i++) {
    bool judge = true;
    for (int j = 0; j < t.size(); j++) {
      if (s[i + j] == '?')
        continue;
      if (s[i + j] != t[j]) {
        judge = false;
        break;
      }
    }
    if (!judge)
      continue;
    string tmp = s;
    for (int j = 0; j < t.size(); j++) {
      tmp[i + j] = t[j];
    }
    for (int j = 0; j < tmp.size(); j++) {
      if (tmp[j] == '?')
        tmp[j] = 'a';
    }
    cand.push_back(tmp);
  }
  sort(all(cand));
  if (cand.size() == 0)
    cout << "UNRESTORABLE" << endl;
  else
    cout << cand.front() << endl;
  return;
}

signed main() {
  fastio();
  solve();
  // int t; cin >> t; while(t--)solve();
  return 0;
}