Problem p03565 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
using namespace std;

// ループマクロ
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; 0 <= i; i--)
#define ROP(i, n) for (int i = 1; i <= n; i++)

// 入出力
#define SCOUT(x) cout << (x) << " "
#define VECCIN(x)                                                              \
  for (auto &ele : (x))                                                        \
  cin >> ele
#define VECOUT(x)                                                              \
  for (auto &ele : (x))                                                        \
    cout << ele << " ";                                                        \
  cout << endl;

// その他
#define ALL(obj) (obj).begin(), (obj).end()
#define P pair<int, int>
#define V vector<int>
#define VLL vector<long long>
#define M map<int, int>
#define S set<int>
#define PQ priority_queue<int>
#define PQG priority_queue<int, V, greater<int>>

int main(void) {

  ios::sync_with_stdio(false);
  cin.tie(0);
  string s, t;
  cin >> s >> t;

  int ins_point = -1;
  for (int i = 0; i <= s.size() - t.size(); i++) {

    bool flag = true;
    for (int j = 0; j < t.size(); j++) {
      if (s[i + j] != '?' && s[i + j] != t[j]) {
        flag = false;
        break;
      }
    }

    if (flag)
      ins_point = i;
  }

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

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
using namespace std;

// ループマクロ
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; 0 <= i; i--)
#define ROP(i, n) for (int i = 1; i <= n; i++)

// 入出力
#define SCOUT(x) cout << (x) << " "
#define VECCIN(x)                                                              \
  for (auto &ele : (x))                                                        \
  cin >> ele
#define VECOUT(x)                                                              \
  for (auto &ele : (x))                                                        \
    cout << ele << " ";                                                        \
  cout << endl;

// その他
#define ALL(obj) (obj).begin(), (obj).end()
#define P pair<int, int>
#define V vector<int>
#define VLL vector<long long>
#define M map<int, int>
#define S set<int>
#define PQ priority_queue<int>
#define PQG priority_queue<int, V, greater<int>>

int main(void) {

  ios::sync_with_stdio(false);
  cin.tie(0);
  string s, t;
  cin >> s >> t;

  if (s.size() < t.size()) {
    cout << "UNRESTORABLE" << endl;
    return 0;
  }
  int ins_point = -1;
  for (int i = 0; i <= s.size() - t.size(); i++) {

    bool flag = true;
    for (int j = 0; j < t.size(); j++) {
      if (s[i + j] != '?' && s[i + j] != t[j]) {
        flag = false;
        break;
      }
    }

    if (flag)
      ins_point = i;
  }

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

  return 0;
}