Problem p03856 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include<boost/multiprecision/cpp_int.hpp>
#define int int64_t
#define uint uint64_t
#define REP(i, a, b) for (int64_t i = (int64_t)(a); i < (int64_t)(b); i++)
#define rep(i, a) REP(i, 0, a)
#define EACH(i, a) for (auto i : a)
#define ITR(x, a) for (auto x = a.begin(); x != a.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define HAS(a, x) (a.find(x) != a.end())
#define Min(x) *min_element(ALL(x))
#define Max(x) *max_element(ALL(x))
#define Unique(L) (L.erase(unique(ALL(L)), L.end()))
#define intmax (std::numeric_limits<int64_t>::max() / 4)
#define doublemax (std::numeric_limits<double>::max() / 4)
using namespace std;
// typedef boost::multiprecision::cpp_int bigint;
const double EPS = 1e-9;
const double PI = acos(-1.0);

vector<string> a;

bool solve(string &s, string &t, int num) {
  if (s.size() < t.size())
    return false;
  REP(i, t.size() - num, t.size()) if (s[i] != t[i]) return false;
  if (t.size() == s.size())
    return true;

  rep(i, a.size()) {
    string nt = t + a[i];
    if (solve(s, nt, a[i].size()))
      return true;
  }
  return false;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  a = {"dreamer", "dream", "eraser", "erase"};
  string S;
  cin >> S;
  string T = "";
  cout << (solve(S, T, 0) ? "YES" : "NO") << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include<boost/multiprecision/cpp_int.hpp>
#define int int64_t
#define uint uint64_t
#define REP(i, a, b) for (int64_t i = (int64_t)(a); i < (int64_t)(b); i++)
#define rep(i, a) REP(i, 0, a)
#define EACH(i, a) for (auto i : a)
#define ITR(x, a) for (auto x = a.begin(); x != a.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define HAS(a, x) (a.find(x) != a.end())
#define Min(x) *min_element(ALL(x))
#define Max(x) *max_element(ALL(x))
#define Unique(L) (L.erase(unique(ALL(L)), L.end()))
#define intmax (std::numeric_limits<int64_t>::max() / 4)
#define doublemax (std::numeric_limits<double>::max() / 4)
using namespace std;
// typedef boost::multiprecision::cpp_int bigint;
const double EPS = 1e-9;
const double PI = acos(-1.0);

vector<string> a;

bool solve(string &s, string &t, int num) {
  if (s.size() < t.size())
    return false;
  REP(i, t.size() - num, t.size()) if (s[i] != t[i]) return false;
  if (t.size() == s.size())
    return true;

  rep(i, a.size()) {
    t += a[i];
    bool res = solve(s, t, a[i].size());
    rep(j, a[i].size()) t.erase(--t.end());
    if (res)
      return true;
  }
  return false;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  a = {"dreamer", "dream", "eraser", "erase"};
  string S;
  cin >> S;
  string T = "";
  cout << (solve(S, T, 0) ? "YES" : "NO") << endl;
  return 0;
}