Problem p03854 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, j, n) for (i = j; i < (n); i++)
#define rrep(i, j, n) for (i = j; i > (n); i--)
typedef long long int lli;
#define N 1000000007
#define M 998244353

using namespace std;

void dp(string t, lli size, string *ans, string *a, string *b, string *c) {
  *a = t.substr(0, 5);
  *b = t.substr(0, 6);
  *c = t.substr(0, 7);
  if (*a == "esare") {
    size -= 5;
    if (size > 0) {
      t.erase(t.begin(), t.begin() + 5);
      dp(t, size, ans, a, b, c);
    } else {
      if (size == 0) {
        *ans = "YES";
      }
    }
  } else {
    if (*a == "maerd") {
      size -= 5;
      if (size > 0) {
        t.erase(t.begin(), t.begin() + 5);
        dp(t, size, ans, a, b, c);
      } else {
        if (size == 0) {
          *ans = "YES";
        }
      }
    } else {
      if (*b == "resare") {
        size -= 6;
        if (size > 0) {
          t.erase(t.begin(), t.begin() + 6);
          dp(t, size, ans, a, b, c);
        } else {
          if (size == 0) {
            *ans = "YES";
          }
        }
      } else {
        if (*c == "remaerd") {
          size -= 7;
          if (size > 0) {
            t.erase(t.begin(), t.begin() + 7);
            dp(t, size, ans, a, b, c);
          } else {
            if (size == 0) {
              *ans = "YES";
            }
          }
        }
      }
    }
  }
}

int main() {
  string s, ans = "NO", a, b, c;
  lli i, j;
  cin >> s;
  reverse(s.begin(), s.end());
  i = s.size();
  dp(s, i, &ans, &a, &b, &c);
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, j, n) for (i = j; i < (n); i++)
#define rrep(i, j, n) for (i = j; i > (n); i--)
typedef long long int lli;
#define N 1000000007
#define M 998244353

using namespace std;

void dp(string t, lli size, string *ans, string *a, string *b, string *c) {
  *a = t.substr(0, 5);
  *b = t.substr(0, 6);
  *c = t.substr(0, 7);
  if (*a == "esare") {
    size -= 5;
    if (size > 0) {
      t.erase(t.begin(), t.begin() + 5);
      dp(t, size, ans, a, b, c);
    } else {
      if (size == 0) {
        *ans = "YES";
      }
    }
  } else {
    if (*a == "maerd") {
      size -= 5;
      if (size > 0) {
        t.erase(t.begin(), t.begin() + 5);
        dp(t, size, ans, a, b, c);
      } else {
        if (size == 0) {
          *ans = "YES";
        }
      }
    } else {
      if (*b == "resare") {
        size -= 6;
        if (size > 0) {
          t.erase(t.begin(), t.begin() + 6);
          dp(t, size, ans, a, b, c);
        } else {
          if (size == 0) {
            *ans = "YES";
          }
        }
      } else {
        if (*c == "remaerd") {
          size -= 7;
          if (size > 0) {
            t.erase(t.begin(), t.begin() + 7);
            dp(t, size, ans, a, b, c);
          } else {
            if (size == 0) {
              *ans = "YES";
            }
          }
        }
      }
    }
  }
}

int main() {
  string s, ans = "NO", a, b, c;
  lli i, j;
  cin >> s;
  reverse(s.begin(), s.end());
  i = s.size();
  while (1) {
    //        cout<<i<<endl;
    a = s.substr(0, 5);
    b = s.substr(0, 6);
    c = s.substr(0, 7);
    if (a == "esare") {
      i -= 5;
      if (i > 0) {
        s.erase(s.begin(), s.begin() + 5);
      } else {
        if (i == 0) {
          ans = "YES";
        }
        break;
      }
    } else {
      if (a == "maerd") {
        i -= 5;
        if (i > 0) {
          s.erase(s.begin(), s.begin() + 5);
        } else {
          if (i == 0) {
            ans = "YES";
          }
          break;
        }
      } else {
        if (b == "resare") {
          i -= 6;
          if (i > 0) {
            s.erase(s.begin(), s.begin() + 6);
          } else {
            if (i == 0) {
              ans = "YES";
            }
            break;
          }
        } else {
          if (c == "remaerd") {
            i -= 7;
            if (i > 0) {
              s.erase(s.begin(), s.begin() + 7);
            } else {
              if (i == 0) {
                ans = "YES";
              }
              break;
            }
          } else {
            break;
          }
        }
      }
    }
  }
  cout << ans << endl;
}