Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
typedef long long ll;

using namespace std;

template <typename T> void output(T, int);
int gcd(int a, int b);

int main() {
  // source
  string s;
  cin >> s;
  vector<string> t = {"dream", "dreamer", "erase", "eraser"};
  queue<string> q;
  q.push(s);
  bool ans = false;
  while (!q.empty() || ans) {
    bool judge = false;
    string temp = q.front();
    if (temp.size() == 0) {
      break;
    }
    q.pop();
    rep(i, 4) {
      if (t[i].size() > temp.size()) {
        continue;
      }
      bool b = true;
      rep(l, t[i].size()) {
        if (t[i][l] != temp[l]) {
          b = false;
        }
      }

      if (b) {
        if (temp.size() == t[i].size()) {
          ans = true;
        } else {
          q.push(temp.substr(t[i].size(), temp.size() - t[i].size()));
        }
      }
    }
  }
  if (ans) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}

template <typename T> void output(T a, int precision) {
  if (precision > 0) {
    cout << setprecision(precision) << a << "\n";
  } else {
    cout << a << "\n";
  }
}

template <typename T> T min(T a, T b) {

  if (a < b) {
    return a;
  } else {
    return b;
  }
}

int gcd(int a, int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
typedef long long ll;

using namespace std;

template <typename T> void output(T, int);
int gcd(int a, int b);

int main() {
  // source
  string s;
  cin >> s;
  vector<string> t = {"dream", "dreamer", "erase", "eraser"};
  queue<string> q;
  q.push(s);
  bool ans = false;
  while (!q.empty() || ans) {
    bool judge = false;
    string temp = q.front();
    if (temp.size() == 0) {
      break;
    }
    q.pop();
    rep(i, 4) {
      if (t[i].size() > temp.size()) {
        continue;
      }
      bool b = true;
      rep(l, t[i].size()) {
        if (t[i][l] != temp[l]) {
          b = false;
        }
      }

      if (b) {
        if (temp.size() == t[i].size()) {
          ans = true;
          cout << "YES" << endl;
          return 0;
        } else {
          q.push(temp.substr(t[i].size(), temp.size() - t[i].size()));
        }
      }
    }
  }
  if (ans) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}

template <typename T> void output(T a, int precision) {
  if (precision > 0) {
    cout << setprecision(precision) << a << "\n";
  } else {
    cout << a << "\n";
  }
}

template <typename T> T min(T a, T b) {

  if (a < b) {
    return a;
  } else {
    return b;
  }
}

int gcd(int a, int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}