Problem p03565 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef pair<ll, ll> pll;

ll n = 0, m = 0, k = 0, ans = 0, sum = 0, cnt = 0, temp = 0;
string s;
bool flag;

//----------------------------------------------------------------------

#define MAX 110000

int main(void) {
  string t;
  string anss[50];
  cin >> s;
  cin >> t;

  if (s.length() < t.length()) {
    cout << "UNRESTORABLE" << endl;
    return 0;
  }

  RFOR(i, s.length() - t.length() - 1, s.length()) {
    flag = true;
    FOR(j, 0, t.length()) {
      if (!(s[i - j] == '?' || s[i - j] == t[t.length() - j - 1])) {
        flag = false;
        break;
      }
    }
    if (flag) {
      anss[cnt] = s;
      FOR(j, 0, t.length()) { anss[cnt][i - j] = t[t.length() - j - 1]; }
      cnt++;
      break;
    }
  }

  FOR(i, 0, cnt) {
    FOR(j, 0, anss[i].length()) {
      if (anss[i][j] == '?')
        anss[i][j] = 'a';
    }
  }

  if (cnt > 0)
    s = anss[0];

  FOR(i, 0, cnt) { s > anss[i] ? s = anss[i] : s = s; }

  if (cnt > 0) {
    cout << s << endl;
  } else {
    cout << "UNRESTORABLE" << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef pair<ll, ll> pll;

ll n = 0, m = 0, k = 0, ans = 0, sum = 0, cnt = 0, temp = 0;
string s;
bool flag;

//----------------------------------------------------------------------

#define MAX 110000

int main(void) {
  string t;
  string anss[50];
  cin >> s;
  cin >> t;

  if (s.length() < t.length()) {
    cout << "UNRESTORABLE" << endl;
    return 0;
  }

  RFOR(i, t.length() - 1, s.length()) {
    flag = true;
    FOR(j, 0, t.length()) {
      if (!(s[i - j] == '?' || s[i - j] == t[t.length() - j - 1])) {
        flag = false;
        break;
      }
    }
    if (flag) {
      anss[cnt] = s;
      FOR(j, 0, t.length()) { anss[cnt][i - j] = t[t.length() - j - 1]; }
      cnt++;
      break;
    }
  }

  FOR(i, 0, cnt) {
    FOR(j, 0, anss[i].length()) {
      if (anss[i][j] == '?')
        anss[i][j] = 'a';
    }
  }

  if (cnt > 0)
    s = anss[0];

  FOR(i, 0, cnt) { s > anss[i] ? s = anss[i] : s = s; }

  if (cnt > 0) {
    cout << s << endl;
  } else {
    cout << "UNRESTORABLE" << endl;
  }

  return 0;
}