Problem p03854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define INF 1 << 30
#define endl '\n'
#define maxn 100005
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long ll;
const double PI = acos(-1.0);
#define dbg(x) cerr << #x << " = " << x << endl;
#define dbg2(x, y) cerr << #x << " = " << x << ", " << #y << " = " << y << endl;
#define dbg3(x, y, z)                                                          \
  cerr << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = "  \
       << z << endl;

const ll mod = 1e9 + 7;

map<pair<int, int>, int> mp;

int main() {
  FASTIO
  /*
 //double start_time = clock();
#ifndef ONLINE_JUDGE
 freopen("in.txt", "r", stdin);
 freopen("out.txt", "w", stdout);
 freopen("error.txt", "w", stderr);
#endif
//*/
  int T;
  // cin >> T;
  T = 1;
  for (int cs = 1; cs <= T; cs++) {
    // dream, dreamer, erase, eraser
    string s;
    cin >> s;
    int l = s.length();
    int i = l - 1;
    while (i >= 0) {
      if (i < 4) {
        cout << "NO\n";
        return 0;
      }
      if (s[i - 2] == 'e') // dream
      {
        if (s[i - 4] == 'd' && s[i - 3] == 'r' && s[i - 1] == 'a' &&
            s[i] == 'm')
          i -= 5;
        else {
          cout << "NO\n";
          return 0;
        }
      }

      else if (s[i - 2] == 'a') // erase
      {
        if (s[i - 4] == 'e' && s[i - 3] == 'r' && s[i - 1] == 's' &&
            s[i] == 'e')
          i -= 5;
        else {
          cout << "NO\n";
          return 0;
        }
      } else if (i < 5) {
        cout << "NO\n";
        return 0;
      }

      else if (s[i - 2] == 's') // eraser
      {
        if (s[i - 5] == 'e' && s[i - 4] == 'r' && s[i - 3] == 'a' &&
            s[i - 1] == 'e' && s[i] == 'r')
          i -= 6;
        else {
          cout << "NO\n";
          return 0;
        }
      }

      else if (i < 6) {
        cout << "NO\n";
        return 0;
      }

      else if (s[i - 2] == 'm') // dreamer
      {
        if (s[i - 6] == 'd' && s[i - 5] == 'r' && s[i - 4] == 'e' &&
            s[i - 3] == 'a' && s[i - 1] == 'e' && s[i] == 'r')
          i -= 7;
        else {
          cout << "NO\n";
          return 0;
        }
      }
    }
    cout << "YES\n";
  }

  // double end_time = clock();
  // printf( "Time = %lf ms\n", ( (end_time - start_time) /
  // CLOCKS_PER_SEC)*1000);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INF 1 << 30
#define endl '\n'
#define maxn 100005
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long ll;
const double PI = acos(-1.0);
#define dbg(x) cerr << #x << " = " << x << endl;
#define dbg2(x, y) cerr << #x << " = " << x << ", " << #y << " = " << y << endl;
#define dbg3(x, y, z)                                                          \
  cerr << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = "  \
       << z << endl;

const ll mod = 1e9 + 7;

map<pair<int, int>, int> mp;

int main() {
  FASTIO
  /*
 //double start_time = clock();
#ifndef ONLINE_JUDGE
 freopen("in.txt", "r", stdin);
 freopen("out.txt", "w", stdout);
 freopen("error.txt", "w", stderr);
#endif
//*/
  int T;
  // cin >> T;
  T = 1;
  for (int cs = 1; cs <= T; cs++) {
    // dream, dreamer, erase, eraser
    string s;
    cin >> s;
    int l = s.length();
    int i = l - 1;
    while (i >= 0) {
      if (i < 4) {
        cout << "NO\n";
        return 0;
      }
      if (s[i - 2] == 'e') // dream
      {
        if (s[i - 4] == 'd' && s[i - 3] == 'r' && s[i - 1] == 'a' &&
            s[i] == 'm')
          i -= 5;
        else {
          cout << "NO\n";
          return 0;
        }
      }

      else if (s[i - 2] == 'a') // erase
      {
        if (s[i - 4] == 'e' && s[i - 3] == 'r' && s[i - 1] == 's' &&
            s[i] == 'e')
          i -= 5;
        else {
          cout << "NO\n";
          return 0;
        }
      } else if (i < 5) {
        cout << "NO\n";
        return 0;
      }

      else if (s[i - 2] == 's') // eraser
      {
        if (s[i - 5] == 'e' && s[i - 4] == 'r' && s[i - 3] == 'a' &&
            s[i - 1] == 'e' && s[i] == 'r')
          i -= 6;
        else {
          cout << "NO\n";
          return 0;
        }
      }

      else if (i < 6) {
        cout << "NO\n";
        return 0;
      }

      else if (s[i - 2] == 'm') // dreamer
      {
        if (s[i - 6] == 'd' && s[i - 5] == 'r' && s[i - 4] == 'e' &&
            s[i - 3] == 'a' && s[i - 1] == 'e' && s[i] == 'r')
          i -= 7;
        else {
          cout << "NO\n";
          return 0;
        }
      } else {
        cout << "NO\n";
        return 0;
      }
    }
    cout << "YES\n";
  }

  // double end_time = clock();
  // printf( "Time = %lf ms\n", ( (end_time - start_time) /
  // CLOCKS_PER_SEC)*1000);

  return 0;
}