Problem p03565 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 1000000000
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
typedef long long int ll;

#define ARRAY_MAX 100005
#define MOD 1000000007

int main() {

  string s, t;
  cin >> s >> t;
  int maxi_index = 0;
  bool flag = false;
  for (int i = 0; i <= s.length() - t.length(); i++) {
    // cout << i << endl;
    bool exist = true;
    if (s[i] == t[0] || s[i] == '?') {
      // 先頭文字と一致したら
      // cout << i << " a " << endl;
      for (int j = i; j < i + t.length(); j++) {
        // cout << j << " b " << endl;
        if (s[j] != t[j - i]) {
          // 一致しない
          if (s[j] != '?') {
            exist = false;
            break;
          }
        }
      }
      if (exist) {
        maxi_index = max(maxi_index, i);
        flag = true;
      }
    }
  }

  if (flag) {
    for (int i = maxi_index; i < maxi_index + t.length(); i++) {
      s[i] = t[i - maxi_index];
    }
    REP(i, s.length()) {
      if (s[i] == '?') {
        s[i] = 'a';
      }
    }
    cout << s << endl;
  } else {
    cout << "UNRESTORABLE" << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 1000000000
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
typedef long long int ll;

#define ARRAY_MAX 100005
#define MOD 1000000007

int main() {

  string s, t;
  cin >> s >> t;
  int maxi_index = 0;
  bool flag = false;
  if (s.length() < t.length()) {
    cout << "UNRESTORABLE" << endl;
    return 0;
  }
  for (int i = 0; i <= s.length() - t.length(); i++) {
    // cout << i << endl;
    bool exist = true;
    if (s[i] == t[0] || s[i] == '?') {
      // 先頭文字と一致したら
      // cout << i << " a " << endl;
      for (int j = i; j < i + t.length(); j++) {
        // cout << j << " b " << endl;
        if (s[j] != t[j - i]) {
          // 一致しない
          if (s[j] != '?') {
            exist = false;
            break;
          }
        }
      }
      if (exist) {
        maxi_index = max(maxi_index, i);
        flag = true;
      }
    }
  }

  if (flag) {
    for (int i = maxi_index; i < maxi_index + t.length(); i++) {
      s[i] = t[i - maxi_index];
    }
    REP(i, s.length()) {
      if (s[i] == '?') {
        s[i] = 'a';
      }
    }
    cout << s << endl;
  } else {
    cout << "UNRESTORABLE" << endl;
  }

  return 0;
}