prithivMLmods/Coder-Stat huggingface dataset:
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  string s;
  cin >> s;

  vector<string> w{"dream", "dreamer", "erase", "eraser"};

  while (true) {
    bool f = false;
    FOR(i, w) {
      if ((int)s.rfind(*i) == s.size() - (*i).size()) {
        s.erase(s.end() - (*i).size(), s.end());
        f = true;
        break;
      }
    }
    if (!f)
      break;
  }

  if (s.empty())
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}


Fixed submission:
#define _GLIBCXX_DEBUG
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  string s;
  cin >> s;

  vector<string> w{"dream", "dreamer", "erase", "eraser"};

  while (true) {
    bool f = false;
    FOR(i, w) {
      int index = (int)s.rfind(*i);
      if (index != -1 && index == s.size() - (*i).size()) {
        s.erase(s.end() - (*i).size(), s.end());
        f = true;
        break;
      }
    }
    if (!f)
      break;
  }

  if (s.empty())
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string S;
  cin >> S;
  deque<char> dq;
  for (int i = 0; i < S.size(); i++) {
    dq.push_back(S.at(i));
  }
  bool b = 1;
  while (!dq.empty()) {
    if (dq.size() >= 5 && dq.at(0) == 'd' && dq.at(1) == 'r' &&
        dq.at(2) == 'e' && dq.at(3) == 'a' && dq.at(4) == 'm') {
      for (int i = 0; i < 5; i++) {
        dq.pop_front();
      }
      if (dq.at(0) == 'e' && dq.at(1) == 'r' &&
          (dq.size() == 2 || (dq.size() > 2 && dq.at(2) != 'a'))) {
        for (int i = 0; i < 2; i++) {
          dq.pop_front();
        }
      }
    } else if (dq.size() >= 5 && dq.at(0) == 'e' && dq.at(1) == 'r' &&
               dq.at(2) == 'a' && dq.at(3) == 's' && dq.at(4) == 'e') {
      for (int i = 0; i < 5; i++) {
        dq.pop_front();
      }
      if (!dq.empty() && dq.at(0) == 'r') {
        dq.pop_front();
      }
    } else {
      b = 0;
      break;
    }
  }
  if (b == 0) {
    cout << "NO" << endl;
  }
  if (b == 1) {
    cout << "YES" << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  string S;
  cin >> S;
  deque<char> dq;
  for (int i = 0; i < S.size(); i++) {
    dq.push_back(S.at(i));
  }
  bool b = 1;
  while (!dq.empty()) {
    if (dq.size() >= 5 && dq.at(0) == 'd' && dq.at(1) == 'r' &&
        dq.at(2) == 'e' && dq.at(3) == 'a' && dq.at(4) == 'm') {
      for (int i = 0; i < 5; i++) {
        dq.pop_front();
      }
      if (!dq.empty() && dq.at(0) == 'e' && dq.at(1) == 'r' &&
          (dq.size() == 2 || (dq.size() > 2 && dq.at(2) != 'a'))) {
        for (int i = 0; i < 2; i++) {
          dq.pop_front();
        }
      }
    } else if (dq.size() >= 5 && dq.at(0) == 'e' && dq.at(1) == 'r' &&
               dq.at(2) == 'a' && dq.at(3) == 's' && dq.at(4) == 'e') {
      for (int i = 0; i < 5; i++) {
        dq.pop_front();
      }
      if (!dq.empty() && dq.at(0) == 'r') {
        dq.pop_front();
      }
    } else {
      b = 0;
      break;
    }
  }
  if (b == 0) {
    cout << "NO" << endl;
  }
  if (b == 1) {
    cout << "YES" << endl;
  }
}
Problem p03854 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

bool answer(string s) {
  if (s == "")
    return true;

  int l = s.size();
  if (s.at(l - 1) == 'm' && s.at(l - 2) == 'a' && s.at(l - 3) == 'e' &&
      s.at(l - 4) == 'r' && s.at(l - 5) == 'd')
    return answer(s.erase(l - 5));
  if (s.at(l - 1) == 'e' && s.at(l - 2) == 's' && s.at(l - 3) == 'a' &&
      s.at(l - 4) == 'r' && s.at(l - 5) == 'e')
    return answer(s.erase(l - 5));
  if (s.at(l - 1) == 'r' && s.at(l - 2) == 'e' && s.at(l - 3) == 's' &&
      s.at(l - 4) == 'a' && s.at(l - 5) == 'r' && s.at(l - 6) == 'e')
    return answer(s.erase(l - 6));
  if (s.at(l - 1) == 'r' && s.at(l - 2) == 'e' && s.at(l - 3) == 'm' &&
      s.at(l - 4) == 'a' && s.at(l - 5) == 'e' && s.at(l - 6) == 'r' &&
      s.at(l - 7) == 'd')
    return answer(s.erase(l - 7));
  return false;
}

int main() {
  string s;
  cin >> s;
  if (answer(s))
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

bool answer(string &s) {
  if (s == "")
    return true;

  int l = s.size();
  if (s.at(l - 1) == 'm' && s.at(l - 2) == 'a' && s.at(l - 3) == 'e' &&
      s.at(l - 4) == 'r' && s.at(l - 5) == 'd')
    return answer(s.erase(l - 5));
  if (s.at(l - 1) == 'e' && s.at(l - 2) == 's' && s.at(l - 3) == 'a' &&
      s.at(l - 4) == 'r' && s.at(l - 5) == 'e')
    return answer(s.erase(l - 5));
  if (s.at(l - 1) == 'r' && s.at(l - 2) == 'e' && s.at(l - 3) == 's' &&
      s.at(l - 4) == 'a' && s.at(l - 5) == 'r' && s.at(l - 6) == 'e')
    return answer(s.erase(l - 6));
  if (s.at(l - 1) == 'r' && s.at(l - 2) == 'e' && s.at(l - 3) == 'm' &&
      s.at(l - 4) == 'a' && s.at(l - 5) == 'e' && s.at(l - 6) == 'r' &&
      s.at(l - 7) == 'd')
    return answer(s.erase(l - 7));
  return false;
}

int main() {
  string s;
  cin >> s;
  if (answer(s))
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
  string S, T;
  cin >> S;

  string much_ptrn[4] = {"dream", "dreamer", "erase", "eraser"};

  reverse(S.begin(), S.end());
  for (int i = 0; i < 4; i++) {
    reverse(much_ptrn[i].begin(), much_ptrn[i].end());
  }

  bool can_solve = true;
  for (int i = 0; i < S.size();) {
    bool is_much = false;
    for (int j = 0; j < 4; j++) {
      string pattern = much_ptrn[i];
      if (S.substr(i, pattern.size()) == pattern) {
        is_much = true;
        i += pattern.size();
      }
    }
    if (!is_much) {
      can_solve = false;
      break;
    }
  }
  if (can_solve)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
  string S, T;
  cin >> S;

  string much_ptrn[4] = {"dream", "dreamer", "erase", "eraser"};

  reverse(S.begin(), S.end());
  for (int i = 0; i < 4; i++) {
    reverse(much_ptrn[i].begin(), much_ptrn[i].end());
  }

  bool can_solve = true;
  for (int i = 0; i < S.size();) {
    bool is_much = false;
    for (int j = 0; j < 4; j++) {
      string pattern = much_ptrn[j];
      if (S.substr(i, pattern.size()) == pattern) {
        is_much = true;
        i += pattern.size();
      }
    }
    if (!is_much) {
      can_solve = false;
      break;
    }
  }
  if (can_solve)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

const string T[] = {"dream", "dreamer", "erase", "eraser"};

const int Maxn = 10010;
bool dp[Maxn];

int main() {
  string s;
  cin >> s;
  int n = (int)s.size();

  dp[0] = true;
  for (int i = 0; i <= n; ++i) {
    if (!dp[i])
      continue;

    string tmp = s.substr(i, 5);
    if (tmp == T[0] || tmp == T[2])
      dp[i + 5] = true;

    tmp = s.substr(i, 6);
    if (tmp == T[3])
      dp[i + 6] = true;

    tmp = s.substr(i, 7);
    if (tmp == T[1])
      dp[i + 7] = true;
  }

  cout << (dp[n] ? "YES" : "NO") << endl;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

const string T[] = {"dream", "dreamer", "erase", "eraser"};

const int Maxn = 100010;
bool dp[Maxn];

int main() {
  string s;
  cin >> s;
  int n = (int)s.size();

  dp[0] = true;
  for (int i = 0; i <= n; ++i) {
    if (!dp[i])
      continue;

    string tmp = s.substr(i, 5);
    if (tmp == T[0] || tmp == T[2])
      dp[i + 5] = true;

    tmp = s.substr(i, 6);
    if (tmp == T[3])
      dp[i + 6] = true;

    tmp = s.substr(i, 7);
    if (tmp == T[1])
      dp[i + 7] = true;
  }

  cout << (dp[n] ? "YES" : "NO") << endl;
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

// "dream", "dreamer", "erase", "eraser" → "maerd" "remaerd" "esare" "resare"
int main() {
  string S;
  cin >> S;

  int now = S.length() - 1;

  while (now > 0) {
    if (S[now] == 'm') {
      if (now < 4 || S.compare(now - 4, 5, "dream") != 0) {
        cout << "NO\n";
        return 0;
      }

      now -= 5;
    } else if (S[now] == 'e') {
      if (now < 4 || S.compare(now - 4, 5, "erase") != 0) {
        cout << "NO\n";
        return 0;
      }

      now -= 5;
    } else if (S[now] == 'r') {
      if (now < 5) {
        cout << "NO\n";
        return 0;
      }
      if (S.compare(now - 6, 7, "dreamer") == 0) {
        now -= 7;
      } else if (S.compare(now - 5, 6, "eraser") == 0) {
        now -= 6;
      } else {
        cout << "NO\n";
        return 0;
      }
    } else {
      cout << "NO\n";
      return 0;
    }
  }

  // now == -1
  if (now < 0) {
    cout << "YES\n";
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

// "dream", "dreamer", "erase", "eraser" → "maerd" "remaerd" "esare" "resare"
int main() {
  string S;
  cin >> S;

  int now = S.length() - 1;

  while (now > 0) {
    if (S[now] == 'm') {
      if (now < 4 || S.compare(now - 4, 5, "dream") != 0) {
        cout << "NO\n";
        return 0;
      }

      now -= 5;
    } else if (S[now] == 'e') {
      if (now < 4 || S.compare(now - 4, 5, "erase") != 0) {
        cout << "NO\n";
        return 0;
      }

      now -= 5;
    } else if (S[now] == 'r') {
      if (now < 5) {
        cout << "NO\n";
        return 0;
      }
      if (!(now == 5) && S.compare(now - 6, 7, "dreamer") == 0) {
        now -= 7;
      } else if (S.compare(now - 5, 6, "eraser") == 0) {
        now -= 6;
      } else {
        cout << "NO\n";
        return 0;
      }
    } else {
      cout << "NO\n";
      return 0;
    }
  }

  // now == -1
  if (now < 0) {
    cout << "YES\n";
  }
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const string YES = "YES";
const string NO = "NO";

void solve(std::string S) {
  array<string, 4> words = {"dream", "dreamer", "erase", "eraser"};
  while (!S.empty()) {
    bool found = false;
    for (auto &&word : words) {
      auto find_pos = S.rfind(word);
      if (find_pos == S.size() - word.size()) {
        found = true;
        S.resize(find_pos);
        break;
      }
    }
    if (!found) {
      cout << NO << endl;
      return;
    }
  }

  cout << YES << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
// clang-format off
int main(){
    std::string S;
    std::cin >> S;
    cin.tie(nullptr);
    cout.tie(nullptr);
	ios_base::sync_with_stdio(false);
    solve(S);
    return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const string YES = "YES";
const string NO = "NO";

void solve(std::string S) {
  array<string, 4> words = {"dream", "dreamer", "erase", "eraser"};
  while (!S.empty()) {
    bool found = false;
    for (auto &&word : words) {
      auto find_pos = S.rfind(word);
      if (find_pos != string::npos && find_pos == S.size() - word.size()) {
        found = true;
        S.resize(find_pos);
        break;
      }
    }
    if (!found) {
      cout << NO << endl;
      return;
    }
  }

  cout << YES << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
// clang-format off
int main(){
    std::string S;
    std::cin >> S;
    cin.tie(nullptr);
    cout.tie(nullptr);
	ios_base::sync_with_stdio(false);
    solve(S);
    return 0;
}
Problem p03854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long int;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll mod = 1e9 + 7;

int main() {
  string s;
  cin >> s;
  string a = "dreamer", b = "eraser";
  int g = s.size();
  int d = 0;
  bool ans = 1;
  while (d < g) {
    if (s[d] == 'e') {
      for (int i = d; i < d + 5; i++) {
        if (s[i] != b[i - d])
          ans = 0;
      }
      d += 5;
      if (s[d] == 'r')
        d++;
    } else if (s[d] == 'd') {
      for (int i = d; i < d + 5; i++) {
        if (s[i] != a[i - d])
          ans = 0;
      }
      d += 5;
      if (s[d] == 'e' && s[d + 1] == 'r' && s[d + 2] != 'a')
        d += 2;
    }

    if (ans == 0)
      break;
  }

  if (ans) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long int;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll mod = 1e9 + 7;

int main() {
  string s;
  cin >> s;
  string a = "dreamer", b = "eraser";
  int g = s.size();
  int d = 0;
  bool ans = 1;
  while (d < g) {
    if (s[d] == 'e') {
      for (int i = d; i < d + 5; i++) {
        if (s[i] != b[i - d])
          ans = 0;
      }
      d += 5;
      if (s[d] == 'r')
        d++;
    } else if (s[d] == 'd') {
      for (int i = d; i < d + 5; i++) {
        if (s[i] != a[i - d])
          ans = 0;
      }
      d += 5;
      if (s[d] == 'e' && s[d + 1] == 'r' && s[d + 2] != 'a')
        d += 2;
    } else {
      ans = 0;
    }

    if (ans == 0)
      break;
  }

  if (ans) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string S, W;
  string DE[4] = {"maerd", "esare", "resare", "remaerd"};
  int L, N, K;
  cin >> S;
  L = S.size();
  N = L;
  bool H = true;
  reverse(S.begin(), S.end());

  while (H) {
    W = "";
    bool H2 = true;
    if (N == 0) {
      break;
    }
    if (N < 5) {
      H = false;
      break;
    }

    if (S.at(L - N) == 'm' || S.at(L - N) == 'e') {
      for (int i = 0; i < 5; i++) {
        W = W + S.at(L - N + i);
      }
      if (W == DE[0] || W == DE[1]) {
        N = N - 5;
        H2 = false;
      }
    }

    else if (S.at(L - N) == 'r') {
      if (S.at(L - N + 2) == 's') {
        K = 1;
      } else if (S.at(L - N + 2) == 'm') {
        K = 2;
      }
      for (int i = 0; i < 5 + K; i++) {
        W = W + S.at(L - N + i);
      }
      if (W == DE[1 + K]) {
        N = N - 5 - K;
        H2 = false;
      }
    }
    if (H2) {
      H = false;
    }
  }

  if (H) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  string S, W;
  string DE[4] = {"maerd", "esare", "resare", "remaerd"};
  int L, N, K;
  cin >> S;
  L = S.size();
  N = L;
  bool H = true;
  reverse(S.begin(), S.end());

  while (H) {
    W = "";
    bool H2 = true;
    if (N == 0) {
      break;
    }
    if (N < 5) {
      H = false;
      break;
    }

    if (S.at(L - N) == 'm' || S.at(L - N) == 'e') {
      for (int i = 0; i < 5; i++) {
        W = W + S.at(L - N + i);
      }
      if (W == DE[0] || W == DE[1]) {
        N = N - 5;
        H2 = false;
      }
    }

    else if (S.at(L - N) == 'r') {
      if (S.at(L - N + 2) == 's') {
        K = 1;
      } else if (S.at(L - N + 2) == 'm') {
        K = 2;
      } else {
        H = false;
        break;
      }
      for (int i = 0; i < 5 + K; i++) {
        W = W + S.at(L - N + i);
      }
      if (W == DE[1 + K]) {
        N = N - 5 - K;
        H2 = false;
      }
    }
    if (H2) {
      H = false;
    }
  }

  if (H) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  string S;
  cin >> S;
  int s = 0;
  while (s < S.size()) {
    if (S.substr(s, 7) == "dreamer") {
      if (S.at(s + 7) == 'a')
        s += 5;
      else
        s += 7;
    } else if (S.substr(s, 5) == "dream") {
      s += 5;
    } else if (S.substr(s, 6) == "eraser") {
      s += 6;
    } else if (S.substr(s, 5) == "erase") {
      s += 5;
    } else {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  string S;
  cin >> S;
  int s = 0;
  while (s < S.size()) {
    if (S.substr(s, 7) == "dreamer") {
      if (s + 7 < S.size() && S.at(s + 7) == 'a')
        s += 5;
      else
        s += 7;
    } else if (S.substr(s, 5) == "dream") {
      s += 5;
    } else if (S.substr(s, 6) == "eraser") {
      s += 6;
    } else if (S.substr(s, 5) == "erase") {
      s += 5;
    } else {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
}
Problem p03854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
  string devide[4] = {"dream", "dreamer", "erase", "eraser"};
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  for (int i = 0; i < 4; ++i)
    reverse(devide[i].begin(), devide[i].end());

  bool flag = true;
  for (int i = 0; i < S.size();) {
    bool flag2 = false;
    for (int j = 0; j < 4; ++j) {
      string d = devide[j];
      if (S.substr(i, d.size()) == d) {
        flag2 = true;
        i += d.size();
      }
      if (!flag2) {
        flag = false;
        break;
      }
    }
  }
  if (flag) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
  string devide[4] = {"dream", "dreamer", "erase", "eraser"};
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  for (int i = 0; i < 4; ++i)
    reverse(devide[i].begin(), devide[i].end());

  bool flag = true;
  for (int i = 0; i < S.size();) {
    bool flag2 = false;
    for (int j = 0; j < 4; ++j) {
      string d = devide[j];
      if (S.substr(i, d.size()) == d) {
        flag2 = true;
        i += d.size();
      }
    }
    if (!flag2) {
      flag = false;
      break;
    }
  }
  if (flag) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}
Problem p03854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main() {
  string S;
  string parts[4] = {"dream", "dreamer", "erase", "eraser"};

  cin >> S;
  reverse(S.begin(), S.end());
  for (int i = 0; i < 4; i++)
    reverse(parts[i].begin(), parts[i].end());

  bool can = true;
  int head = 0;
  while (1) {
    if (S.substr(head, 5) == parts[0] || S.substr(head, 5) == parts[2]) {
      head += 5;
    } else if (S.substr(head, 6) == parts[1] || S.substr(head, 6) == parts[3]) {
      head += 6;
    }

    if (head == S.size()) {
      cout << "YES" << endl;
      return 0;
    } else if (head > S.size()) {
      cout << "NO" << endl;
      return 0;
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main() {
  string S;
  string parts[4] = {"dream", "dreamer", "erase", "eraser"};

  cin >> S;
  reverse(S.begin(), S.end());
  for (int i = 0; i < 4; i++)
    reverse(parts[i].begin(), parts[i].end());

  bool can = true;
  int head = 0;
  while (1) {
    can = false;
    for (int p = 0; p < 4; p++) {
      if (parts[p] == S.substr(head, parts[p].size())) {
        head += parts[p].size();
        can = true;
        break;
      }
    }

    if (can == false) {
      cout << "NO" << endl;
      return 0;
    }

    if (head == S.size()) {
      cout << "YES" << endl;
      return 0;
    } else if (head > S.size()) {
      cout << "NO" << endl;
      return 0;
    }
  }

  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {

  string divide[4] = {"dream", "dreamer", "erase", "eraser"};
  string s;

  cin >> s;

  reverse(s.begin(), s.end());
  for (int i = 0; i < 4; i++)
    reverse(divide[i].begin(), divide[i].end());

  bool can = true;

  for (int i = 0; i < s.size();) {
    bool can2 = false;
    for (int j = 0; j < 4; j++) {
      string d = divide[i];
      if (s.substr(i, d.size()) == d) {
        can2 = true;
        i += d.size();
      }
    }

    if (!can2) {
      can = false;
      break;
    }
  }

  if (can)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {

  string divide[4] = {"dream", "dreamer", "erase", "eraser"};
  string s;

  cin >> s;

  reverse(s.begin(), s.end());
  for (int i = 0; i < 4; i++)
    reverse(divide[i].begin(), divide[i].end());

  bool can = true;

  for (int i = 0; i < s.size();) {
    bool can2 = false;
    for (int j = 0; j < 4; j++) {
      string d = divide[j];
      if (s.substr(i, d.size()) == d) {
        can2 = true;
        i += d.size();
      }
    }

    if (!can2) {
      can = false;
      break;
    }
  }

  if (can)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

vector<string> divide = {"dream", "dreamer", "erase", "eraser"};

int main() {
  string S;
  cin >> S;

  reverse(S.begin(), S.end());
  for (int i = 0; i < 4; i++)
    reverse(divide.at(i).begin(), divide.at(i).end());

  bool can = true;
  for (int i = 0; i < S.size();) {
    bool can2 = false;
    for (int j = 0; j < 4; j++) {
      string d = divide.at(i);
      if (S.substr(i, d.size()) == d) {
        can2 = true;
        i += d.size();
      }
    }

    if (!can2) {
      can = false;
      break;
    }
  }
  if (can)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

vector<string> divide = {"dream", "dreamer", "erase", "eraser"};

int main() {
  string S;
  cin >> S;

  reverse(S.begin(), S.end());
  for (int i = 0; i < 4; i++)
    reverse(divide.at(i).begin(), divide.at(i).end());

  bool can = true;
  for (int i = 0; i < S.size();) {
    bool can2 = false;
    for (int j = 0; j < 4; j++) {
      string d = divide.at(j);
      if (S.substr(i, d.size()) == d) {
        can2 = true;
        i += d.size();
      }
    }

    if (!can2) {
      can = false;
      break;
    }
  }
  if (can)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define rep(i, a, b) for (int i = a; i < b; i++)
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  lld n, i, j, p, k, m;
  string c, s, t, a, b, x, y;
  cin >> s;
  i = 0;
  n = s.size();
  while (i < n) {
    if ((n - i) < 5)
      break;
    a = s.substr(i, 5);
    b = s.substr(i, 6);
    c = s.substr(i, 7);
    if (b == "eraser") {
      i = i + 6;
      t = t + b;
    } else if (a == "erase") {
      i = i + 5;
      t = t + a;
    } else {
      if (a == "dream" && c == "dreamer") {
        x = s.substr(i + 5, 5);
        y = s.substr(i + 5, 6);
        if (x == "erase" || y == "eraser") {
          i = i + 5;
          t = t + a;
        } else {
          i = i + 7;
          t = t + c;
        }
      } else if (a == "dream") {
        t = t + a;
        i = i + 5;
      }
    }
    // cout<<t<<" ";
  }
  if (s == t)
    cout << "YES";
  else
    cout << "NO";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define rep(i, a, b) for (int i = a; i < b; i++)
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  lld n, i, j, p, k, m;
  string c, s, t, a, b, x, y;
  cin >> s;
  i = 0;
  n = s.size();
  while (i < n) {
    if ((n - i) < 5)
      break;
    a = s.substr(i, 5);
    b = s.substr(i, 6);
    c = s.substr(i, 7);
    if (b == "eraser") {
      i = i + 6;
      t = t + b;
    } else if (a == "erase") {
      i = i + 5;
      t = t + a;
    } else {
      if (a == "dream" && c == "dreamer") {
        x = s.substr(i + 5, 5);
        y = s.substr(i + 5, 6);
        if (x == "erase" || y == "eraser") {
          i = i + 5;
          t = t + a;
        } else {
          i = i + 7;
          t = t + c;
        }
      } else if (a == "dream") {
        t = t + a;
        i = i + 5;
      } else
        break;
    }
    // cout<<t<<" ";
  }
  if (s == t)
    cout << "YES";
  else
    cout << "NO";
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
char a[10006];
int main() {
  scanf("%s", a);
  int p = strlen(a), f = 1;
  for (int i = p - 1; i >= 0;) {
    if (a[i] == 'r') {
      if (a[i - 1] == 'e' && a[i - 2] == 's' && a[i - 3] == 'a' &&
          a[i - 4] == 'r' && a[i - 5] == 'e') {
        i = i - 6;
      } else if (a[i - 1] == 'e' && a[i - 2] == 'm' && a[i - 3] == 'a' &&
                 a[i - 4] == 'e' && a[i - 5] == 'r' && a[i - 6] == 'd') {
        i = i - 7;
      } else {
        printf("NO\n");
        f = 0;
        break;
      }
    } else if (a[i] == 'e') {
      if (a[i - 1] == 's' && a[i - 2] == 'a' && a[i - 3] == 'r' &&
          a[i - 4] == 'e') {
        i = i - 5;
      } else {
        printf("NO\n");
        f = 0;
        break;
      }
    } else if (a[i] == 'm') {
      if (a[i - 1] == 'a' && a[i - 2] == 'e' && a[i - 3] == 'r' &&
          a[i - 4] == 'd') {
        i = i - 5;
      } else {
        printf("NO\n");
        f = 0;
        break;
      }
    } else {
      printf("NO\n");
      f = 0;
      break;
    }
  }
  if (f == 1)
    printf("YES\n");
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
char a[100006];
int main() {
  scanf("%s", a);
  int p = strlen(a), f = 1;
  for (int i = p - 1; i >= 0;) {
    if (a[i] == 'r') {
      if (a[i - 1] == 'e' && a[i - 2] == 's' && a[i - 3] == 'a' &&
          a[i - 4] == 'r' && a[i - 5] == 'e') {
        i = i - 6;
      } else if (a[i - 1] == 'e' && a[i - 2] == 'm' && a[i - 3] == 'a' &&
                 a[i - 4] == 'e' && a[i - 5] == 'r' && a[i - 6] == 'd') {
        i = i - 7;
      } else {
        printf("NO\n");
        f = 0;
        break;
      }
    } else if (a[i] == 'e') {
      if (a[i - 1] == 's' && a[i - 2] == 'a' && a[i - 3] == 'r' &&
          a[i - 4] == 'e') {
        i = i - 5;
      } else {
        printf("NO\n");
        f = 0;
        break;
      }
    } else if (a[i] == 'm') {
      if (a[i - 1] == 'a' && a[i - 2] == 'e' && a[i - 3] == 'r' &&
          a[i - 4] == 'd') {
        i = i - 5;
      } else {
        printf("NO\n");
        f = 0;
        break;
      }
    } else {
      printf("NO\n");
      f = 0;
      break;
    }
  }
  if (f == 1)
    printf("YES\n");
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>

using namespace std;

int main() {
  string divide[4] = {"dream", "dreamer", "erase", "eraser"};
  string S;
  cin >> S;

  reverse(S.begin(), S.end());
  for (int i = 0; i < 4; i++)
    reverse(divide[i].begin(), divide[4].end());

  bool can = true;
  for (int i = 0; i < S.size();) {
    bool can2 = false;
    for (int j = 0; j < 4; j++) {
      string d = divide[j];
      if (S.substr(i, d.size()) == d) {
        can2 = true;
        i += d.size();
      }
    }

    if (can2 == false) {
      can = false;
      break;
    }
  }
  if (can)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>

using namespace std;

int main() {
  string divide[4] = {"dream", "dreamer", "erase", "eraser"};
  string S;
  cin >> S;

  reverse(S.begin(), S.end());
  for (int i = 0; i < 4; i++)
    reverse(divide[i].begin(), divide[i].end());

  bool can = true;
  for (int i = 0; i < S.size();) {
    bool can2 = false;
    for (int j = 0; j < 4; j++) {
      string d = divide[j];
      if (S.substr(i, d.size()) == d) {
        can2 = true;
        i += d.size();
      }
    }

    if (can2 == false) {
      can = false;
      break;
    }
  }
  if (can)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
Problem p03854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  vector<string> keys = {"dream", "dreamer", "erase", "eraser"};
  string s;
  cin >> s;

  reverse(s.begin(), s.end());
  for (int i = 0; i < keys.size(); i++) {
    reverse(keys.at(i).begin(), keys.at(i).end());
  }
  bool can = true;
  for (int i = 0; i < s.size();) {
    bool can2 = false;
    for (int j = 0; j < keys.size(); j++) {
      if (s.substr(i, keys.at(j).size()) == keys.at(j)) {
        i += keys.at(j).size();
        can2 = true;
        break;
      }
      if (!can2) {
        can = false;
        break;
      }
    }
  }
  if (can)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  vector<string> keys = {"dream", "dreamer", "erase", "eraser"};
  string s;
  cin >> s;

  reverse(s.begin(), s.end());
  for (int i = 0; i < keys.size(); i++) {
    reverse(keys.at(i).begin(), keys.at(i).end());
  }
  bool can = true;
  for (int i = 0; i < s.size();) {
    bool can2 = false;
    for (int j = 0; j < keys.size(); j++) {
      if (s.substr(i, keys.at(j).size()) == keys.at(j)) {
        i += keys.at(j).size();
        can2 = true;
        break;
      }
    }
    if (!can2) {
      can = false;
      break;
    }
  }
  if (can)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
Problem p03854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define froop1(a, n) for (int i = a; i < n; i++)
#define froop2(a, n) for (int i = a; i > n; i--)
int main() {
  string N;
  vector<string> b;
  cin >> N;
  int t = 1;
  for (int i = N.length() - 1; i >= 0; i--) {
    if (N.substr(i, t) == "dream" || N.substr(i, t) == "dreamer" ||
        N.substr(i, t) == "erase" || N.substr(i, t) == "eraser") {
      b.push_back(N.substr(i, t));
      t = 0;
    }
    t++;
  }
  string check = "";
  for (int i = b.size() - 1; i >= 0; i--) {
    check += b.at(i);
  }
  if (N == check) {
    cout << "YES";
  } else {
    cout << "NO";
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define froop1(a, n) for (int i = a; i < n; i++)
#define froop2(a, n) for (int i = a; i > n; i--)
int main() {
  string N;
  vector<string> b;
  cin >> N;
  int t = 1;
  for (int i = N.length() - 1; i >= 0; i--) {
    if (N.substr(i, t) == "dream" || N.substr(i, t) == "dreamer" ||
        N.substr(i, t) == "erase" || N.substr(i, t) == "eraser") {
      b.push_back(N.substr(i, t));
      t = 0;
    }
    if (t >= 7)
      break;
    t++;
  }
  string check = "";
  for (int i = b.size() - 1; i >= 0; i--) {
    check += b.at(i);
  }
  if (N == check) {
    cout << "YES";
  } else {
    cout << "NO";
  }
  cout << endl;
  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
// #include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++) // 初期値a,i<bの範囲でループ
#define REP(i, n) FOR(i, 0, n) // 初期値0,i<nの範囲でループ
using namespace std;
typedef long long ll; // int64
typedef unsigned long long ull;

int main() {
  string s;
  cin >> s;

  for (;;) {
    if (s.size() == 0)
      break;
    string s5, s6, s7;
    s5 = s.substr(s.size() - 5, 5);
    s6 = s.substr(s.size() - 6, 6);
    s7 = s.substr(s.size() - 7, 7);

    if (s6 == "eraser") {
      s = s.substr(0, s.size() - 6);
    } else if (s5 == "erase") {
      s = s.substr(0, s.size() - 5);
    } else if (s7 == "dreamer") {
      s = s.substr(0, s.size() - 7);
    } else if (s5 == "dream") {
      s = s.substr(0, s.size() - 5);
    } else {
      cout << "NO";
      return 0;
    }
  }
  cout << "YES";

  return 0;
}

Fixed submission:
// #include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++) // 初期値a,i<bの範囲でループ
#define REP(i, n) FOR(i, 0, n) // 初期値0,i<nの範囲でループ
using namespace std;
typedef long long ll; // int64
typedef unsigned long long ull;

int main() {
  string s;
  cin >> s;

  for (;;) {
    if (s.size() == 0)
      break;
    string s5, s6, s7;
    if (s.size() >= 5)
      s5 = s.substr(s.size() - 5, 5);
    if (s.size() >= 6)
      s6 = s.substr(s.size() - 6, 6);
    if (s.size() >= 7)
      s7 = s.substr(s.size() - 7, 7);

    if (s6 == "eraser") {
      s = s.substr(0, s.size() - 6);
    } else if (s5 == "erase") {
      s = s.substr(0, s.size() - 5);
    } else if (s7 == "dreamer") {
      s = s.substr(0, s.size() - 7);
    } else if (s5 == "dream") {
      s = s.substr(0, s.size() - 5);
    } else {
      cout << "NO";
      return 0;
    }
  }
  cout << "YES";

  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int judge(string str) {
  string headStr, tailStr;
  int rtn = -1;

  headStr = str.substr(0, 5);
  tailStr = str.substr(5, 3);
  if (headStr == "dream") {
    if (tailStr == "er" || tailStr == "ere" || tailStr == "erd") {
      rtn = 7;
    } else {
      rtn = 5;
    }
  } else if (headStr == "erase") {
    if (tailStr.at(0) == 'r') {
      rtn = 6;
    } else {
      rtn = 5;
    }
  }
  return rtn;
}

int main() {
  string str;
  uint curPos = 0;
  int rtn;
  string result = "YES";
  cin >> str;

  while (curPos < str.size()) {
    if (str.size() - curPos < 5) {
      result = "NO";
      break;
    }
    rtn = judge(str.substr(curPos));
    if (rtn < 0) {
      result = "NO";
      break;
    } else {
      curPos += rtn;
    }
  }

  cout << result << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int judge(string str) {
  string headStr, tailStr;
  int rtn = -1;

  headStr = str.substr(0, 5);
  tailStr = str.substr(5, 3);
  if (headStr == "dream") {
    if (tailStr == "er" || tailStr == "ere" || tailStr == "erd") {
      rtn = 7;
    } else {
      rtn = 5;
    }
  } else if (headStr == "erase") {
    if (tailStr.size() > 0 && tailStr.at(0) == 'r') {
      rtn = 6;
    } else {
      rtn = 5;
    }
  }
  return rtn;
}

int main() {
  string str;
  uint curPos = 0;
  int rtn;
  string result = "YES";
  cin >> str;

  while (curPos < str.size()) {
    if (str.size() - curPos < 5) {
      result = "NO";
      break;
    }
    rtn = judge(str.substr(curPos));
    if (rtn < 0) {
      result = "NO";
      break;
    } else {
      curPos += rtn;
    }
  }

  cout << result << endl;
  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> P_D;
#define PI 3.1415926535897932384626433832795
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define reprev(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define ALL(a) (a).begin(), (a).end()
#define MAX 100100
#define MAX_N 200100
#define SENTINEL 2000000000
#define MAX_B 1001001
#define NIL -1
#define MOD 1000000007
#define INF 1 << 29
#define INFTY 1000000000000000000LL

int main() {
  string s;
  string add[4] = {"dream", "dreamer", "erase", "eraser"};
  cin >> s;
  rep(i, 4) reverse(ALL(add[i]));
  reverse(ALL(s));
  bool ans = false;
  int i = 0;
  while (i < s.length()) {
    rep(k, 4) {
      string p = s.substr(i, add[k].length());
      if (p == add[k]) {
        i += add[k].length();
        ans = true;
        break;
      }
    }
    if (!ans) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> P_D;
#define PI 3.1415926535897932384626433832795
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define reprev(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define ALL(a) (a).begin(), (a).end()
#define MAX 100100
#define MAX_N 200100
#define SENTINEL 2000000000
#define MAX_B 1001001
#define NIL -1
#define MOD 1000000007
#define INF 1 << 29
#define INFTY 1000000000000000000LL

int main() {
  string s;
  string add[4] = {"dream", "dreamer", "erase", "eraser"};
  cin >> s;
  rep(i, 4) reverse(ALL(add[i]));
  reverse(ALL(s));
  bool ans = false;
  int i = 0;
  while (i < s.length()) {
    ans = false;
    rep(k, 4) {
      string p = s.substr(i, add[k].length());
      if (p == add[k]) {
        i += add[k].length();
        ans = true;
        break;
      }
    }
    if (!ans) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;

int ChkD(const char *S) {
  if (S[0] != 'd')
    return -1;
  if (S[1] != 'r')
    return -1;
  if (S[2] != 'e')
    return -1;
  if (S[3] != 'a')
    return -1;
  if (S[4] != 'm')
    return -1;

  if (S[5] != 'e')
    return 5;
  if (S[6] != 'r')
    return -1;

  if (S[7] == 'a')
    return 5;

  return 7;
}

int ChkE(const char *S) {
  if (S[0] != 'e')
    return -1;
  if (S[1] != 'r')
    return -1;
  if (S[2] != 'a')
    return -1;
  if (S[3] != 's')
    return -1;
  if (S[4] != 'e')
    return -1;

  if (S[5] != 'r')
    return 5;

  return 6;
}

char S[10010];

int main() {
  int i;
  int iChk;
  char C;

  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> S;

  i = 0;
  while ((C = S[i]) != '\0') {
    if (C == 'd') {
      iChk = ChkD(S + i);
      if (iChk <= 0)
        break;

      i += iChk;
    } else if (C == 'e') {
      iChk = ChkE(S + i);
      if (iChk <= 0)
        break;

      i += iChk;
    } else
      break;
  }

  if (C == '\0')
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int ChkD(const char *S) {
  if (S[0] != 'd')
    return -1;
  if (S[1] != 'r')
    return -1;
  if (S[2] != 'e')
    return -1;
  if (S[3] != 'a')
    return -1;
  if (S[4] != 'm')
    return -1;

  if (S[5] != 'e')
    return 5;
  if (S[6] != 'r')
    return -1;

  if (S[7] == 'a')
    return 5;

  return 7;
}

int ChkE(const char *S) {
  if (S[0] != 'e')
    return -1;
  if (S[1] != 'r')
    return -1;
  if (S[2] != 'a')
    return -1;
  if (S[3] != 's')
    return -1;
  if (S[4] != 'e')
    return -1;

  if (S[5] != 'r')
    return 5;

  return 6;
}

char S[100100];

int main() {
  int i;
  int iChk;
  char C;

  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> S;

  i = 0;
  while ((C = S[i]) != '\0') {
    if (C == 'd') {
      iChk = ChkD(S + i);
      if (iChk <= 0)
        break;

      i += iChk;
    } else if (C == 'e') {
      iChk = ChkE(S + i);
      if (iChk <= 0)
        break;

      i += iChk;
    } else
      break;
  }

  if (C == '\0')
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string s, t = {}, w, ans[4] = {"dream", "dreamer", "erase", "eraser"};
  bool can = true;
  cin >> s;
  reverse(s.begin(), s.end());
  for (int i = 0; i < 4; i++)
    reverse(ans[i].begin(), ans[i].end());

  while (s.size() > t.size()) {
    for (int i = 0; i < 4; i++) {
      string w = t;
      t += ans[i];
      can = true;
      for (int j = 0; j <= t.size() - 1; j++) {
        if (s[j] != t[j]) {
          can = false;
          break;
        }
      }
      if (can == false)
        t = w;
      else
        break;
    }
    if (can == false)
      break;
  }
  if (can)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  string s, t = {}, w, ans[4] = {"dream", "dreamer", "erase", "eraser"};
  bool can = true;
  cin >> s;
  reverse(s.begin(), s.end());
  for (int i = 0; i < 4; i++)
    reverse(ans[i].begin(), ans[i].end());

  while (s.size() > t.size()) {
    for (int i = 0; i < 4; i++) {
      string w = t;
      t += ans[i];
      can = true;
      for (int j = w.size(); j <= t.size() - 1; j++) {
        if (s[j] != t[j]) {
          can = false;
          break;
        }
      }
      if (can == false)
        t = w;
      else
        break;
    }
    if (can == false)
      break;
  }
  if (can)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Time Limit Exceeded):
void solve();

int main() {
  solve();
  return 0;
}

//////////////////////////////////////////////////
//////////////////////////////////////////////////
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

void solve() {
  string S;
  cin >> S;
  while (S.size() > 0) {
    if (S.size() >= 7) {
      if (S.substr(S.size() - 7, 7) == string("dreamer")) {
        S.erase(S.size() - 7, 7);
        continue;
      }
    }
    if (S.size() >= 5) {
      if (S.substr(S.size() - 5, 5) == string("dream")) {
        S.erase(S.size() - 5, 5);
        continue;
      }
    }
    if (S.size() >= 6) {
      if (S.substr(S.size() - 6, 6) == string("eraser")) {
        S.erase(S.size() - 6, 6);
        continue;
      }
    }
    if (S.size() >= 5) {
      if (S.substr(S.size() - 5, 5) == string("erase")) {
        S.erase(S.size() - 5, 5);
        continue;
      }
    } else
      break;
  }
  if (S == string(""))
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return;
}

Fixed submission:
void solve();

int main() {
  solve();
  return 0;
}

//////////////////////////////////////////////////
//////////////////////////////////////////////////
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

void solve() {
  string S;
  cin >> S;
  while (S.size() > 0) {
    if (S.size() >= 7) {
      if (S.substr(S.size() - 7, 7) == string("dreamer")) {
        S.erase(S.size() - 7, 7);
        continue;
      }
    }
    if (S.size() >= 5) {
      if (S.substr(S.size() - 5, 5) == string("dream")) {
        S.erase(S.size() - 5, 5);
        continue;
      }
    }
    if (S.size() >= 6) {
      if (S.substr(S.size() - 6, 6) == string("eraser")) {
        S.erase(S.size() - 6, 6);
        continue;
      }
    }
    if (S.size() >= 5) {
      if (S.substr(S.size() - 5, 5) == string("erase")) {
        S.erase(S.size() - 5, 5);
        continue;
      }
    }
    break;
  }
  if (S == string(""))
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return;
}
Problem p03854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
  string str;
  bool fl = true;
  cin >> str;
  reverse(str.begin(), str.end());
  while (!(str.empty()) && fl == true) {
    if (str[0] == 'm') {
      if (str[1] == 'a') {
        if (str[2] == 'e') {
          if (str[3] == 'r') {
            if (str[4] == 'd') {
              str.erase(0, 5);
            }
          }
        }
      }
    } else if (str[0] == 'e') {
      if (str[1] == 's') {
        if (str[2] == 'a') {
          if (str[3] == 'r') {
            if (str[4] == 'e') {
              str.erase(0, 5);
            }
          }
        }
      }
    } else if (str[0] == 'r') {
      if (str[1] == 'e') {
        if (str[2] == 's') {
          if (str[3] == 'a') {
            if (str[4] == 'r') {
              if (str[5] == 'e') {
                str.erase(0, 6);
              }
            }
          }
        }
      }
    } else if (str[0] == 'r') {
      if (str[1] == 'e') {
        if (str[2] == 'm') {
          if (str[3] == 'a') {
            if (str[4] == 'e') {
              if (str[5] == 'r') {
                if (str[6] == 'd') {
                  str.erase(0, 7);
                } else
                  fl = false;
              } else
                fl = false;
            } else
              fl = false;
          } else
            fl = false;
        } else
          fl = false;
      } else
        fl = false;
    } else
      fl = false;
  }
  if (fl == true)
    cout << "YES";
  else
    cout << "NO";
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
  string str;
  bool fl = true;
  cin >> str;
  reverse(str.begin(), str.end());
  while (!(str.empty()) && fl == true) {
    if (str.substr(0, 5) == "maerd")
      str.erase(0, 5);
    else if (str.substr(0, 7) == "remaerd")
      str.erase(0, 7);
    else if (str.substr(0, 5) == "esare")
      str.erase(0, 5);
    else if (str.substr(0, 6) == "resare")
      str.erase(0, 6);
    else
      fl = false;
  }
  if (fl == true)
    cout << "YES";
  else
    cout << "NO";
}