Problem p03856 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define int long long
#define CONTAINS(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REMOVE(v, a) v.erase(remove(v.begin(), v.end(), a), v.end())
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;

char s[10000];
int len;

bool find(int l, string word) {
  for (int i = 0; i < word.length(); i++) {
    int j = i + l;
    if (j >= len) {
      return false;
    }
    if (s[j] != word.at(i)) {
      return false;
    }
  }
  return true;
}

signed main() {
  cin >> s;
  len = strlen(s);
  int l = 0;

  bool is_dream = false;

  while (true) {
    if (l == len) {
      cout << "YES" << endl;
      return 0;
    }

    if (find(l, "dream")) {
      is_dream = true;
      l += 5;
      continue;
    }

    if (find(l, "eraser")) {
      is_dream = false;
      l += 6;
      continue;
    }

    if (find(l, "erase")) {
      is_dream = false;
      l += 5;
      continue;
    }

    if (is_dream && find(l, "er")) {
      is_dream = false;
      l += 2;
      continue;
    }

    cout << "NO" << endl;
    return 0;
  }
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define int long long
#define CONTAINS(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REMOVE(v, a) v.erase(remove(v.begin(), v.end(), a), v.end())
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;

char s[100001];
int len;

bool find(int l, string word) {
  for (int i = 0; i < word.length(); i++) {
    int j = i + l;
    if (j >= len) {
      return false;
    }
    if (s[j] != word.at(i)) {
      return false;
    }
  }
  return true;
}

signed main() {
  cin >> s;
  len = strlen(s);
  int l = 0;

  bool is_dream = false;

  while (true) {
    if (l == len) {
      cout << "YES" << endl;
      return 0;
    }

    if (find(l, "dream")) {
      is_dream = true;
      l += 5;
      continue;
    }

    if (find(l, "eraser")) {
      is_dream = false;
      l += 6;
      continue;
    }

    if (find(l, "erase")) {
      is_dream = false;
      l += 5;
      continue;
    }

    if (is_dream && find(l, "er")) {
      is_dream = false;
      l += 2;
      continue;
    }

    cout << "NO" << endl;
    return 0;
  }
}