Problem p03565 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
typedef long long ll;

#define pi 3.14159265358979323846
#define VI vector<int>
#define VLL vector<long long>
#define MAX max_element
#define MIN min_element
#define all(v) v.begin(), v.end()
const ll MOD = 1e9 + 7;
using namespace std;
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> T finder(vector<T> vec, T number) {
  auto itr = find(vec.begin(), vec.end(), number);
  size_t index = distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}
ll frac(ll n) {
  if (n == 0) {
    return 1;
  }
  return (n * frac(n - 1)) % MOD;
}

/*char r[4];
int yaju[4];
char op[3];
int sum = 0;
scanf("%s", r);
for (int i = 0;i < 4;i++) {
        yaju[i] = r[i] - '0';
}
*/
int main() {
  string S, T;
  cin >> S >> T;
  bool ok = false;
  ll itr = 0;
  ll end = 0;
  for (int i = 0; i <= S.size() - T.size(); i++) {
    itr = i;
    for (int u = 0; u < T.size(); u++) {
      if (S[itr] == T[u] || S[itr] == '?')
        itr++;
      else
        break;
      if (u == T.size() - 1) {
        ok = true;
        end = itr;
      }
    }
  }
  if (!ok)
    cout << "UNRESTORABLE" << endl;
  else {
    ll begin = end - T.size();
    ll o = 0;
    for (int i = begin; i < end; i++) {
      S[i] = T[o];
      o++;
    }
    for (int i = 0; i < S.size(); i++) {
      if (S[i] != '?')
        cout << S[i];
      else
        cout << "a";
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
typedef long long ll;

#define pi 3.14159265358979323846
#define VI vector<int>
#define VLL vector<long long>
#define MAX max_element
#define MIN min_element
#define all(v) v.begin(), v.end()
const ll MOD = 1e9 + 7;
using namespace std;
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> T finder(vector<T> vec, T number) {
  auto itr = find(vec.begin(), vec.end(), number);
  size_t index = distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}
ll frac(ll n) {
  if (n == 0) {
    return 1;
  }
  return (n * frac(n - 1)) % MOD;
}

/*char r[4];
int yaju[4];
char op[3];
int sum = 0;
scanf("%s", r);
for (int i = 0;i < 4;i++) {
        yaju[i] = r[i] - '0';
}
*/
int main() {
  string S, T;
  cin >> S >> T;
  bool ok = false;
  ll itr = 0;
  ll end = 0;
  ll go = S.size() - T.size();
  if (go >= 0) {
    for (int i = 0; i <= S.size() - T.size(); i++) {
      itr = i;
      for (int u = 0; u < T.size(); u++) {
        if (S[itr] == T[u] || S[itr] == '?')
          itr++;
        else
          break;
        if (u == T.size() - 1) {
          ok = true;
          end = itr;
        }
      }
    }
  }
  if (!ok)
    cout << "UNRESTORABLE" << endl;
  else {
    ll begin = end - T.size();
    ll o = 0;
    for (int i = begin; i < end; i++) {
      S[i] = T[o];
      o++;
    }
    for (int i = 0; i < S.size(); i++) {
      if (S[i] != '?')
        cout << S[i];
      else
        cout << "a";
    }
  }
}