Problem p03565 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORR(i, a, b) for (ll i = (a); i <= (b); i++)
#define repR(i, n) for (ll i = n; i >= 0; i--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define F first
#define S second
#define pb push_back
#define pu push
#define COUT(x) cout << (x) << endl
#define PQ priority_queue<ll>
#define PQR priority_queue<ll, vector<ll>, greater<ll>>
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define mp make_pair
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
using vll = vector<ll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pll = pair<ll, ll>;
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 main() {
  string s, t;
  cin >> s >> t;
  ll sn = sz(s);
  ll st = sz(t);
  vstr ans(0);
  rep(i, sn - st + 1) {
    string a = s;
    ll cu = 1;
    for (int j = 0; j < st; j++) {
      if (s[i + j] == '?')
        continue;
      if (s[i + j] != t[j]) {
        cu = 0;
        break;
      }
    }
    if (cu) {
      for (int h = i; h < i + st; h++) {
        a[h] = t[h - i];
      }
      rep(g, sn) {
        if (a[g] == '?') {
          a[g] = 'a';
        }
      }
      ans.pb(a);
    }
  }
  sort(all(ans));
  COUT(ans[0]);
}


Fixed submission:
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORR(i, a, b) for (ll i = (a); i <= (b); i++)
#define repR(i, n) for (ll i = n; i >= 0; i--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define F first
#define S second
#define pb push_back
#define pu push
#define COUT(x) cout << (x) << endl
#define PQ priority_queue<ll>
#define PQR priority_queue<ll, vector<ll>, greater<ll>>
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define mp make_pair
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
using vll = vector<ll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pll = pair<ll, ll>;
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 main() {
  string s, t;
  cin >> s >> t;
  ll sn = sz(s);
  ll st = sz(t);
  vstr ans(0);
  rep(i, sn - st + 1) {
    string a = s;
    ll cu = 1;
    for (int j = 0; j < st; j++) {
      if (s[i + j] == '?')
        continue;
      if (s[i + j] != t[j]) {
        cu = 0;
        break;
      }
    }
    if (cu) {
      for (int h = i; h < i + st; h++) {
        a[h] = t[h - i];
      }
      rep(g, sn) {
        if (a[g] == '?') {
          a[g] = 'a';
        }
      }
      ans.pb(a);
    }
  }

  if (sz(ans) == 0)
    COUT("UNRESTORABLE");
  else {
    sort(all(ans));
    COUT(ans[0]);
  }
}