Problem p03565 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, s) for (int i = 0; i < s; ++i)
#define all(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define VEC(a, n) vector<int> a(n)
#define PQ(a) priority_queue<int> a
#define PQmin(a) priority_queue<int, : vector<int>, greater<int>> a
#define PAIR pair<int, int>
/*
sort(sp.begin(), sp.end(), [](PAIR l, PAIR r){
        return l.first<r.first || (l.first==r.first && l.second > r.second);
        });
*/

typedef long long ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

int main() {
  string s, t;
  cin >> s >> t;
  string x = t;

  int cnt = 0;
  int ans_i;
  bool flag = false;
  for (int i = s.size() - t.size(); i >= 0; i--) {
    for (int j = 0; j < t.size(); j++) {
      // cout<<"AAAAA"<<endl;
      if ((s.at(i + j) != '?') && (s.at(i + j) != t.at(j))) {
        if (i == 0) {
          flag = true;
        }
        cnt = 0;
        break;
      }
      cnt++;
      // cout<<i << " "<< j << " "<< cnt<<endl;
    }
    if (cnt == t.size()) {
      // cout<<"AAAAA"<<endl;
      ans_i = i;
      break;
    }
    x = t;
  }
  if (flag) {
    cout << "UNRESTORABLE" << endl;
    return 0;
  }
  for (int k = 0; k < t.size(); k++) {
    s.at(ans_i + k) = t.at(k);
  }
  // cout<<"AAAAA"<<endl;
  for (int i = 0; i < s.size(); i++) {
    if (s.at(i) == '?')
      s.at(i) = 'a';
  }
  cout << s << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, s) for (int i = 0; i < s; ++i)
#define all(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define VEC(a, n) vector<int> a(n)
#define PQ(a) priority_queue<int> a
#define PQmin(a) priority_queue<int, : vector<int>, greater<int>> a
#define PAIR pair<int, int>
/*
sort(sp.begin(), sp.end(), [](PAIR l, PAIR r){
        return l.first<r.first || (l.first==r.first && l.second > r.second);
        });
*/

typedef long long ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

int main() {
  string s, t;
  cin >> s >> t;
  string x = t;
  if (s.size() < t.size()) {
    cout << "UNRESTORABLE" << endl;
    return 0;
  }

  int cnt = 0;
  int ans_i;
  bool flag = false;
  for (int i = s.size() - t.size(); i >= 0; i--) {
    for (int j = 0; j < t.size(); j++) {
      // cout<<"AAAAA"<<endl;
      if ((s.at(i + j) != '?') && (s.at(i + j) != t.at(j))) {
        if (i == 0) {
          flag = true;
        }
        cnt = 0;
        break;
      }
      cnt++;
      // cout<<i << " "<< j << " "<< cnt<<endl;
    }
    if (cnt == t.size()) {
      // cout<<"AAAAA"<<endl;
      ans_i = i;
      break;
    }
    x = t;
  }
  if (flag) {
    cout << "UNRESTORABLE" << endl;
    return 0;
  }
  for (int k = 0; k < t.size(); k++) {
    s.at(ans_i + k) = t.at(k);
  }
  // cout<<"AAAAA"<<endl;
  for (int i = 0; i < s.size(); i++) {
    if (s.at(i) == '?')
      s.at(i) = 'a';
  }
  cout << s << endl;

  return 0;
}