Problem p03854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
using namespace std;
int main() {
  string s;
  cin >> s;
  int n = s.size() - 1;
  string ss;
  while (n >= 0) {
    if (s[n - 2] == 'e' && s[n - 1] == 'a' && s[n] == 'm') {
      if (n < 4) {
        cout << "NO" << endl;
        return 0;
      } else {
        for (int i = n - 4; i <= n; i++) {
          ss.push_back(s[i]);
        }
        if (ss == "dream") {
          n -= 5;
        } else {
          cout << "NO" << endl;
          return 0;
        }
      }
    } else if (s[n - 2] == 'm' && s[n - 1] == 'e' && s[n] == 'r') {
      if (n < 6) {
        cout << "NO" << endl;
        return 0;
      } else {
        for (int i = n - 6; i <= n; i++) {
          ss.push_back(s[i]);
        }
        if (ss == "dreamer") {
          n -= 7;
        } else {
          cout << "NO" << endl;
          return 0;
        }
      }
    } else if (s[n - 2] == 'a' && s[n - 1] == 's' && s[n] == 'e') {
      if (n < 4) {
        cout << "NO" << endl;
        return 0;
      } else {
        for (int i = n - 4; i <= n; i++) {
          ss.push_back(s[i]);
        }
        if (ss == "erase") {
          n -= 5;
        } else {
          cout << "NO" << endl;
          return 0;
        }
      }
    } else if (s[n - 2] == 's' && s[n - 1] == 'e' && s[n] == 'r') {
      if (n < 5) {
        cout << "NO" << endl;
        return 0;
      } else {
        for (int i = n - 5; i <= n; i++) {
          ss.push_back(s[i]);
        }
        if (ss == "eraser") {
          n -= 6;
        } else {
          cout << "NO" << endl;
          return 0;
        }
      }
    }
    ss = "";
  }
  cout << "YES" << endl;
}


Fixed submission:
#include <iostream>
#include <string>
using namespace std;
int main() {
  string s;
  cin >> s;
  int n = s.size() - 1;
  string ss;
  while (n >= 0) {
    if (s[n - 2] == 'e' && s[n - 1] == 'a' && s[n] == 'm') {
      if (n < 4) {
        cout << "NO" << endl;
        return 0;
      } else {
        for (int i = n - 4; i <= n; i++) {
          ss.push_back(s[i]);
        }
        if (ss == "dream") {
          n -= 5;
        } else {
          cout << "NO" << endl;
          return 0;
        }
      }
    } else if (s[n - 2] == 'm' && s[n - 1] == 'e' && s[n] == 'r') {
      if (n < 6) {
        cout << "NO" << endl;
        return 0;
      } else {
        for (int i = n - 6; i <= n; i++) {
          ss.push_back(s[i]);
        }
        if (ss == "dreamer") {
          n -= 7;
        } else {
          cout << "NO" << endl;
          return 0;
        }
      }
    } else if (s[n - 2] == 'a' && s[n - 1] == 's' && s[n] == 'e') {
      if (n < 4) {
        cout << "NO" << endl;
        return 0;
      } else {
        for (int i = n - 4; i <= n; i++) {
          ss.push_back(s[i]);
        }
        if (ss == "erase") {
          n -= 5;
        } else {
          cout << "NO" << endl;
          return 0;
        }
      }
    } else if (s[n - 2] == 's' && s[n - 1] == 'e' && s[n] == 'r') {
      if (n < 5) {
        cout << "NO" << endl;
        return 0;
      } else {
        for (int i = n - 5; i <= n; i++) {
          ss.push_back(s[i]);
        }
        if (ss == "eraser") {
          n -= 6;
        } else {
          cout << "NO" << endl;
          return 0;
        }
      }
    } else {
      cout << "NO" << endl;
      return 0;
    }
    ss = "";
  }
  cout << "YES" << endl;
}