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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define PI 3.1415926535897932
#define _GLIBCXX_DEBUG
using namespace std;
int main() {

  int H, W;
  cin >> H >> W;

  vector<string> vec(W);
  for (int i = 0; i < H; i++) {
    cin >> vec[i];
  }

  for (int i = 0; i < H; i++) {
    cout << vec[i] << endl;
    cout << vec[i] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define PI 3.1415926535897932
#define _GLIBCXX_DEBUG
using namespace std;
int main() {

  int H, W;
  cin >> H >> W;

  vector<string> vec(H);
  for (int i = 0; i < H; i++) {
    cin >> vec[i];
  }

  for (int i = 0; i < H; i++) {
    cout << vec[i] << endl;
    cout << vec[i] << endl;
  }
}
Problem p03853 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define rrep(i, n) for (int(i) = (n)-1; (i) >= 0; --(i))    // Reversi rep
#define nfor(i, a, b) for (int(i) = (a); (i) < (b); ++(i))  // Natural for
#define rfor(i, a, b) for (int(i) = (b); (i) >= (a); --(i)) // Reversi for
#define ll long long
#define mod (1000000007)
#define YES cout << "YES" << endl;
#define Yes cout << "Yes" << endl;
#define NO cout << "NO" << endl;
#define No cout << "No" << endl;

int asc(const void *a, const void *b) { return *(int *)a - *(int *)b; }

int desc(const void *a, const void *b) { return *(int *)b - *(int *)a; }

int main() {
  int h, w;
  cin >> h >> w;
  string c[h];
  rep(i, h) cin >> c[i];
  rep(i, h + h + 1) cout << c[i / 2] << endl;
  ;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define rrep(i, n) for (int(i) = (n)-1; (i) >= 0; --(i))    // Reversi rep
#define nfor(i, a, b) for (int(i) = (a); (i) < (b); ++(i))  // Natural for
#define rfor(i, a, b) for (int(i) = (b); (i) >= (a); --(i)) // Reversi for
#define ll long long
#define mod (1000000007)
#define YES cout << "YES" << endl;
#define Yes cout << "Yes" << endl;
#define NO cout << "NO" << endl;
#define No cout << "No" << endl;

int asc(const void *a, const void *b) { return *(int *)a - *(int *)b; }

int desc(const void *a, const void *b) { return *(int *)b - *(int *)a; }

int main() {
  int h, w;
  cin >> h >> w;
  string c[h];
  rep(i, h) {
    cin >> c[i];
    cout << c[i] << endl;
    cout << c[i] << endl;
  }
  return 0;
}
Problem p03853 (C++)

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

int main() {
  int h, w;
  string r[100], r2[100];
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    cin >> r[i];
  }
  for (int i = 0; i < h; i++) {
    r2[2 * i] = r[i];
    r2[2 * i + 1] = r[i];
  }
  for (int i = 0; i < 2 * h; i++) {
    cout << r2[i] << endl;
  }
  return 0;
}

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

int main() {
  int h, w;
  string r[100], r2[200];
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    cin >> r[i];
  }
  for (int i = 0; i < h; i++) {
    r2[2 * i] = r[i];
    r2[2 * i + 1] = r[i];
  }
  for (int i = 0; i < 2 * h; i++) {
    cout << r2[i] << endl;
  }
  return 0;
}
Problem p03853 (Python)

Buggy submission (status: Runtime Error):
C = open(0)
for i in C[1:]:
    print(i)
    print(i)


Fixed submission:
import sys

H, W = sys.stdin.readline().split()
C = map(str, sys.stdin)
for i in C:
    print(i)
    print(i)
Problem p03854 (C++)

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

using namespace std;

int main(void) {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int flag = 1;
  while (!s.empty()) {
    if (s.size() < 5) {
      flag = 0;
      break;
    }
    if (s.rfind("maerd") == s.size() - 5) {
      if (s.rfind("re") == s.size() - 7) {
        if (s.size() >= 11) {
          if (s.rfind("resare") == s.size() - 11) {
            for (int i = 0; i < 11; i++) {
              s.pop_back();
            }
          }
        } else if (s.size() >= 10) {
          if (s.rfind("esare") == s.size() - 10) {
            for (int i = 0; i < 10; i++) {
              s.pop_back();
            }
          }
        } else {
          for (int i = 0; i < 7; i++) {
            s.pop_back();
          }
        }
      } else {
        for (int i = 0; i < 5; i++) {
          s.pop_back();
        }
      }
    } else if (s.rfind("resare") == s.size() - 6) {
      for (int i = 0; i < 6; i++) {
        s.pop_back();
      }
    } else if (s.rfind("esare") == s.size() - 5) {
      for (int i = 0; i < 5; i++) {
        s.pop_back();
      }
    } else {
      flag = 0;
      break;
    }
  }
  if (flag) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}

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

using namespace std;

int main(void) {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int flag = 1;
  while (!s.empty()) {
    if (s.find("remaerd") == 0) {
      s.erase(0, 7);
    } else if (s.find("resare") == 0) {
      s.erase(0, 6);
    } else if (s.find("esare") == 0 || s.find("maerd") == 0) {
      s.erase(0, 5);
    } else {
      flag = 0;
      break;
    }
  }
  if (flag) {
    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;
  cin >> s;
  string words[4] = {"dream", "dreamer", "erase", "eraser"};
  reverse(s.begin(), s.end());
  for (int i = 0; i < 4; i++)
    reverse(words[i].begin(), words[i].end());

  bool devided = false;
  for (int i = 0; i < s.size();) {
    for (int j = 0; j < 4; j++) {
      string d = words[j];
      if (s.substr(i, d.size()) == d) {
        i += d.size();
        devided = true;
        break;
      }
    }
    if (!devided) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
}

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

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

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

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

int main() {
  string S, T;
  int i = 0;
  T = "";
  cin >> S;
  while (i < S.size()) {
    if (S.at(i) == 'd' && S.at(i + 1) == 'r' && S.at(i + 2) == 'e' &&
        S.at(i + 3) == 'a' && S.at(i + 4) == 'm') {
      T += "dream";
      if (S.size() == T.size())
        break;
      if (S.at(i + 5) == 'e' && S.at(i + 6) == 'r') {
        T += "er";
        if (S.size() == T.size())
          break;
        if (S.at(i + 7) == 'a' && S.at(i + 8) == 's' && S.at(i + 9) == 'e') {
          T += "ase";
          if (S.size() == T.size())
            break;
          if (S.at(i + 10) == 'r') {
            T += "r";
            if (S.size() == T.size())
              break;
          }
        }
      }
    } else if (S.at(i) == 'e' && S.at(i + 1) == 'r' && S.at(i + 2) == 'a' &&
               S.at(i + 3) == 's' && S.at(i + 4) == 'e') {
      T += "erase";
      if (S.size() == T.size())
        break;
      if (S.at(i + 5) == 'r') {
        T += "r";
        if (S.size() == T.size())
          break;
      }
    } else {
      cout << "NO" << endl;
      break;
    }
    i = T.size();
  }
  if (S == T) {
    cout << "YES" << endl;
  }
}

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

int main() {
  string S, T;
  int i = 0;
  T = "";
  cin >> S;
  while (true) {
    if (S.size() - T.size() < 5) {
      cout << "NO" << endl;
      break;
    }
    if (S.at(i) == 'd' && S.at(i + 1) == 'r' && S.at(i + 2) == 'e' &&
        S.at(i + 3) == 'a' && S.at(i + 4) == 'm') {
      T += "dream";
      if (S.size() == T.size())
        break;
      if (S.at(i + 5) == 'e' && S.at(i + 6) == 'r') {
        T += "er";
        if (S.size() == T.size())
          break;
        if (S.at(i + 7) == 'a' && S.at(i + 8) == 's' && S.at(i + 9) == 'e') {
          T += "ase";
          if (S.size() == T.size())
            break;
          if (S.at(i + 10) == 'r') {
            T += "r";
            if (S.size() == T.size())
              break;
          }
        }
      }
    } else if (S.at(i) == 'e' && S.at(i + 1) == 'r' && S.at(i + 2) == 'a' &&
               S.at(i + 3) == 's' && S.at(i + 4) == 'e') {
      T += "erase";
      if (S.size() == T.size())
        break;
      if (S.at(i + 5) == 'r') {
        T += "r";
        if (S.size() == T.size())
          break;
      }
    } else {
      cout << "NO" << endl;
      break;
    }
    i = T.size();
  }
  if (S == T) {
    cout << "YES" << endl;
  }
}
Problem p03854 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <string>
using namespace std;
int check(string s, int point) {
  if (point >= s.size())
    return 0;
  if (point >= s.size() - 4)
    return 1;
  if (s[point] == 'd' && s[point + 1] == 'r' && s[point + 2] == 'e' &&
      s[point + 3] == 'a' && s[point + 4] == 'm') {
    if (check(s, point + 5) == 0)
      return 0;
    if (point >= s.size() - 6)
      return 1;
    if (s[point + 5] == 'e' && s[point + 6] == 'r') {
      if (check(s, point + 7) == 0)
        return 0;
    }
    return 1;
  }
  if (s[point] == 'e' && s[point + 1] == 'r' && s[point + 2] == 'a' &&
      s[point + 3] == 's' && s[point + 4] == 'e') {
    if (check(s, point + 5) == 0)
      return 0;
    if (point >= s.size() - 5)
      return 1;
    if (s[point + 5] == 'r') {
      if (check(s, point + 6) == 0)
        return 0;
    }
    return 1;
  }
  return 1;
}
int main() {
  string s;
  cin >> s;
  if (check(s, 0) == 0) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;
int check(string &s, int point) {
  if (point >= s.size())
    return 0;
  if (point >= s.size() - 4)
    return 1;
  if (s[point] == 'd' && s[point + 1] == 'r' && s[point + 2] == 'e' &&
      s[point + 3] == 'a' && s[point + 4] == 'm') {
    if (check(s, point + 5) == 0)
      return 0;
    if (point >= s.size() - 6)
      return 1;
    if (s[point + 5] == 'e' && s[point + 6] == 'r') {
      if (check(s, point + 7) == 0)
        return 0;
    }
    return 1;
  }
  if (s[point] == 'e' && s[point + 1] == 'r' && s[point + 2] == 'a' &&
      s[point + 3] == 's' && s[point + 4] == 'e') {
    if (check(s, point + 5) == 0)
      return 0;
    if (point >= s.size() - 5)
      return 1;
    if (s[point + 5] == 'r') {
      if (check(s, point + 6) == 0)
        return 0;
    }
    return 1;
  }
  return 1;
}
int main() {
  string s;
  cin >> s;
  if (check(s, 0) == 0) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <math.h>
#include <stdio.h>
#include <string.h>
int main(void) {
  int s, d = 0, f, g, h, j, i;
  char a[10001];
  scanf("%s", a);
  s = strlen(a);
  f = -1;
  while (1) {
    // printf("%d %c\n",f+1,a[f+1]);
    if (a[f + 1] == 'e' && a[f + 2] == 'r' && a[f + 3] == 'a' &&
        a[f + 4] == 's' && a[f + 5] == 'e' && a[f + 6] == 'r') {
      f += 6;
      if (a[f + 1] != 'e' && a[f + 1] != 'd' && f < s - 1)
        f -= 2;
    } else if (a[f + 1] == 'e' && a[f + 2] == 'r' && a[f + 3] == 'a' &&
               a[f + 4] == 's' && a[f + 5] == 'e')
      f += 5;
    else if (a[f + 1] == 'd' && a[f + 2] == 'r' && a[f + 3] == 'e' &&
             a[f + 4] == 'a' && a[f + 5] == 'm' && a[f + 6] == 'e' &&
             a[f + 7] == 'r') {
      f += 7;
      if (a[f + 1] != 'e' && a[f + 1] != 'd' && f < s - 1)
        f -= 2;
    } else if (a[f + 1] == 'd' && a[f + 2] == 'r' && a[f + 3] == 'e' &&
               a[f + 4] == 'a' && a[f + 5] == 'm')
      f += 5;
    else
      break;
    if (f >= s - 1)
      break;
  }
  // printf("%d %d\n",f,s-1);
  if (f >= s - 1)
    printf("YES\n");
  else
    printf("NO\n");
  return 0;
}

Fixed submission:
#include <math.h>
#include <stdio.h>
#include <string.h>
int main(void) {
  int s, d = 0, f, g, h, j, i;
  char a[100001];
  scanf("%s", a);
  s = strlen(a);
  f = -1;
  while (1) {
    // printf("%d %c\n",f+1,a[f+1]);
    if (a[f + 1] == 'e' && a[f + 2] == 'r' && a[f + 3] == 'a' &&
        a[f + 4] == 's' && a[f + 5] == 'e' && a[f + 6] == 'r') {
      f += 6;
      if (a[f + 1] != 'e' && a[f + 1] != 'd' && f < s - 1)
        f -= 2;
    } else if (a[f + 1] == 'e' && a[f + 2] == 'r' && a[f + 3] == 'a' &&
               a[f + 4] == 's' && a[f + 5] == 'e')
      f += 5;
    else if (a[f + 1] == 'd' && a[f + 2] == 'r' && a[f + 3] == 'e' &&
             a[f + 4] == 'a' && a[f + 5] == 'm' && a[f + 6] == 'e' &&
             a[f + 7] == 'r') {
      f += 7;
      if (a[f + 1] != 'e' && a[f + 1] != 'd' && f < s - 1)
        f -= 2;
    } else if (a[f + 1] == 'd' && a[f + 2] == 'r' && a[f + 3] == 'e' &&
               a[f + 4] == 'a' && a[f + 5] == 'm')
      f += 5;
    else
      break;
    if (f >= s - 1)
      break;
  }
  // printf("%d %d\n",f,s-1);
  if (f >= s - 1)
    printf("YES\n");
  else
    printf("NO\n");
  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vout(x) rep(i, x.size()) cout << x[i] << " "
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

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

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vout(x) rep(i, x.size()) cout << x[i] << " "
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

int main() {
  string s;
  cin >> s;
  int id = 0;
  while (id < s.size()) {
    if (s[id] == 'e') {
      string tmp = s.substr(id, 6);
      if (tmp == "eraser")
        id += 6;
      else {
        tmp = s.substr(id, 5);
        if (tmp == "erase")
          id += 5;
        else {
          cout << "NO" << endl;
          return 0;
        }
      }
    } else if (s[id] == 'd') {
      string tmp = s.substr(id, 7);
      if (tmp == "dreamer") {
        if (s[id + 7] == 'a')
          id += 5;
        else
          id += 7;
      } else {
        tmp = s.substr(id, 5);
        if (tmp == "dream")
          id += 5;
        else {
          cout << "NO" << endl;
          return 0;
        }
      }
    } else {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
}
Problem p03854 (C++)

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

int main() {
  string S;
  cin >> S;
  vector<string> t = {"dream", "dreamer", "erase", "eraser"};
  reverse(S.begin(), S.end());
  rep(i, 0, 4) reverse(t[i].begin(), t[i].end());
  int c = 0;
  bool flag = false;
  while (c != S.size()) {
    rep(i, 0, 4) {
      if (S.substr(c, t[i].size()) == t[i]) {
        c += t[i].size();
        flag = true;
        break;
      }
    }
    if (!flag) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
  return 0;
}

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

int main() {
  string S;
  cin >> S;
  vector<string> t = {"dream", "dreamer", "erase", "eraser"};
  reverse(S.begin(), S.end());
  rep(i, 0, 4) reverse(t[i].begin(), t[i].end());
  int c = 0;
  bool flag = false;
  while (c != S.size()) {
    flag = false;
    rep(i, 0, 4) {
      if (S.substr(c, t[i].size()) == t[i]) {
        c += t[i].size();
        flag = true;
        break;
      }
    }
    if (!flag) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
string divide[4] = {"dream", "dreamer", "erase", "eraser"};
int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  for (int i = 0; i < 4; ++i)
    reverse(divide[i].begin(), divide[i].end());

  for (int i = 0; i < s.size();) {
    bool can = false;
    for (int k = 0; k < 4; i++) {
      string d = divide[k];
      if (s.substr(i, d.size()) == d) {
        can = true;
        i += d.size();
      }
    }
    if (!can) {
      cout << "NO";
      return 0;
    }
  }
  cout << "YES";
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
string divide[4] = {"dream", "dreamer", "erase", "eraser"};
int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  for (int i = 0; i < 4; ++i)
    reverse(divide[i].begin(), divide[i].end());

  for (int i = 0; i < s.size();) {
    bool can = false;
    for (int k = 0; k < 4; k++) {
      string d = divide[k];
      if (s.substr(i, d.size()) == d) {
        can = true;
        i += d.size();
      }
    }
    if (!can) {
      cout << "NO";
      return 0;
    }
  }
  cout << "YES";
}
Problem p03854 (C++)

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

int main(void) {
  string st;
  int count = 0, type = -1;
  bool flag = true;
  cin >> st;
  size_t len = st.length();
  size_t i = 0;
  while (i < len) {
    string sub = st.substr(i, 5);
    // cout << "*" << sub << i << endl;
    if (sub == "dream") {
      // cout << "!" << "dream" << endl;
      i += 5;
      // cout << "pos:" << i << endl;
      if (st.substr(i, 2) == "er") {
        if (st.substr(i, 3) != "era") {
          i += 2;
        }
        // cout << "pos:" << i << endl;
      }
    } else if (sub == "erase") {
      // cout << "!" << "erase" << endl;
      i += 5;
      // cout << "pos:" << i << endl;
      if (st.at(i) == 'r') {
        i++;
        // cout << "pos:" << i << endl;
      }
    } else {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
  return 0;
}

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

int main(void) {
  string st;
  int count = 0, type = -1;
  bool flag = true;
  cin >> st;
  size_t len = st.length();
  size_t i = 0;
  while (i < len) {
    string sub = st.substr(i, 5);
    // cout << "*" << sub << i << endl;
    if (sub == "dream") {
      // cout << "!" << "dream" << endl;
      i += 5;
      // cout << "pos:" << i << endl;
      if (st.substr(i, 2) == "er") {
        if (st.substr(i, 3) != "era") {
          i += 2;
        }
        // cout << "pos:" << i << endl;
      }
    } else if (sub == "erase") {
      // cout << "!" << "erase" << endl;
      i += 5;
      // cout << "pos:" << i << endl;
      if (i < len && st.at(i) == 'r') {
        i++;
        // cout << "pos:" << i << endl;
      }
    } else {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
  return 0;
}
Problem p03854 (C++)

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

using namespace std;

int main(void) {
  string str;
  cin >> str;
  map<string, int> m;
  string a = "maerd";
  m[a] = true;
  a = "remaerd";
  m[a] = true;
  a = "esare";
  m[a] = true;
  a = "resare";
  m[a] = true;
  a.clear();
  int counter = 0;
  for (int i = str.size() - 1; i >= 0; i--) {
    a += str[i];
    string pp = a;
    if (m[a] == true) {
      counter += a.size();
      a.clear();
    }
  }
  if (counter == str.size())
    printf("YES\n");
  else
    printf("NO\n");

  return 0;
}


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

using namespace std;

int main(void) {
  string str;
  cin >> str;
  map<string, int> m;
  string a = "maerd";
  m[a] = true;
  a = "remaerd";
  m[a] = true;
  a = "esare";
  m[a] = true;
  a = "resare";
  m[a] = true;
  a.clear();
  int counter = 0;
  for (int i = str.size() - 1; i >= 0; i--) {
    a += str[i];
    if (a.size() > 7)
      break;
    if (m[a] == true) {
      counter += a.size();
      a.clear();
    }
  }
  if (counter == str.size())
    printf("YES\n");
  else
    printf("NO\n");

  return 0;
}
Problem p03854 (C++)

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

int main() {
  string s;
  cin >> s;
  string a = "dream", b = "dreamer", c = "erase", d = "eraser"; // 判定

  int key = s.size();
  for (int i = s.size() - 1; i >= 0; i--) {
    if (s.substr(i, key) == a || s.substr(i, key) == b ||
        s.substr(i, key) == c || s.substr(i, key) == d) {
      key = i;
    }
  }

  if (key == 0) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }

  return 0;
}

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

int main() {
  string s;
  cin >> s;
  string a = "dream", b = "dreamer", c = "erase", d = "eraser"; // 判定

  int key, i = s.size() - 1;
  while (i >= 0) {
    int num = i;
    if (i - 4 >= 0) {
      if (s[i] == 'm' && s[i - 1] == 'a' && s[i - 2] == 'e' &&
          s[i - 3] == 'r' && s[i - 4] == 'd') {
        key = i - 4;
        if (key == 0) {
          break;
        }
        i = key - 1;
      }
      if (s[i] == 'e' && s[i - 1] == 's' && s[i - 2] == 'a' &&
          s[i - 3] == 'r' && s[i - 4] == 'e') {
        key = i - 4;
        if (key == 0) {
          break;
        }
        i = key - 1;
      }
    }
    if (i - 5 >= 0) {
      if (s[i] == 'r' && s[i - 1] == 'e' && s[i - 2] == 's' &&
          s[i - 3] == 'a' && s[i - 4] == 'r' && s[i - 5] == 'e') {
        key = i - 5;
        if (key == 0) {
          break;
        }
        i = key - 1;
      }
    }
    if (i - 6 >= 0) {
      if (s[i] == 'r' && s[i - 1] == 'e' && s[i - 2] == 'm' &&
          s[i - 3] == 'a' && s[i - 4] == 'e' && s[i - 5] == 'r' &&
          s[i - 6] == 'd') {
        key = i - 6;
        if (key == 0) {
          break;
        }
        i = key - 1;
      }
    }

    if (i == num) {
      key = 1;
      break;
    }
  }

  if (key == 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;
  cin >> s;
  reverse(s.begin(), s.end());
  vector<string> t = {"dream", "dreamer", "erase", "eraser"};
  for (int i = 0; i < 4; i++) {
    reverse(t[i].begin(), t[i].end());
  }

  bool can = true;
  for (int i = 0; i < s.size();) {
    bool can2 = false;
    for (int j = 0; j < 4; j++) {
      string A = t[j];
      if (s.substr(i, A.size()) == A) {
        i += A.size();
        can2 = true;
      }
    }
    if (can2 == false) {
      can = false;
    }
  }
  if (can) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}

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

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

  bool can = true;
  for (int i = 0; i < s.size();) {
    bool can2 = false;
    for (int j = 0; j < 4; j++) {
      string A = t[j];
      if (s.substr(i, A.size()) == A) {
        i += A.size();
        can2 = true;
      }
    }
    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;

int main() {
  string S;
  cin >> S;
  cout << (regex_match(S, regex("(dream?|dreamer?|eraser?|erase?)*")) ? "YES"
                                                                      : "NO")
       << endl;
  getchar();
}

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

int main() {
  string S;
  cin >> S;
  regex R("(dream|dreamer|erase|eraser)*");
  cout << ((regex_match(S, R)) ? "YES" : "NO") << endl;
  getchar();
}
Problem p03854 (C++)

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

int main() {
  string s;
  cin >> s;
  reverse(rng(s));
  while (s != "") {
    if (s.substr(0, 5) == "maerd")
      s.erase(0, 5);
    else if (s.substr(0, 5) == "esare")
      s.erase(0, 5);
    else if (s.substr(0, 7) == "remaerd")
      s.erase(0, 7);
    else if (s.substr(0, 6) == "resare")
      s.substr(0, 6);
    else {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
  return 0;
}

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

int main() {
  string s;
  cin >> s;
  reverse(rng(s));
  for (int i = 0; i < s.size(); i++) {
    if (s.substr(i, 5) == "maerd")
      i += 4;
    else if (s.substr(i, 5) == "esare")
      i += 4;
    else if (s.substr(i, 6) == "resare")
      i += 5;
    else if (s.substr(i, 7) == "remaerd")
      i += 6;
    else {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
  return 0;
}
Problem p03854 (C++)

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

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  vector<string> choices{"dream", "dreamer", "erase", "eraser"};
  string s;
  cin >> s;

  int p = 0;
  int n = s.size();
  if (n < 5)
    cout << "NO" << endl;
  else {
    while (p + 5 <= n) {
      string sub = s.substr(p, 5);
      p += 5;
      if (sub == "dream") {
        if (p + 2 <= n && sub.substr(p, 2) == "er" &&
            (p + 5 > n || sub.substr(p, 5) != "erase")) {
          p += 2;
        }
      } else if (sub == "erase") {
        if (p + 1 <= n && s.substr(p, 1) == "r") {
          p++;
        }
      } else {
        cout << "NO" << endl;
        return 0;
      }
    }
    if (p == n)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }

  return 0;
}


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

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  vector<string> choices{"dream", "dreamer", "erase", "eraser"};
  string s;
  cin >> s;

  int p = 0;
  int n = s.size();
  if (n < 5)
    cout << "NO" << endl;
  else {
    while (p + 5 <= n) {
      string sub = s.substr(p, 5);
      p += 5;
      if (sub == "dream") {
        if (p + 2 <= n && s.substr(p, 2) == "er") {
          if (p + 5 > n || (p + 5 <= n && s.substr(p, 5) != "erase")) {
            p += 2;
          }
        }
      } else if (sub == "erase") {
        if (p + 1 <= n && s.substr(p, 1) == "r") {
          p++;
        }
      } else {
        cout << "NO" << endl;
        return 0;
      }
    }
    if (p == n)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }

  return 0;
}
Problem p03854 (Python)

Buggy submission (status: Runtime Error):
s = input()[::-1]
words = [w[::-1] for w in ["dream", "dreamer", "erase", "eraser"]]

can = "YES"
while s:
    print(s)
    match_any = False
    for w in words:
        if s.startswith(w):
            s = s.replace(w, "", 1)
            match_any = True
            break
    if not match_any:
        can = "NO"
        break
print(can)


Fixed submission:
s = input()[::-1]
words = [w[::-1] for w in ["dream", "dreamer", "erase", "eraser"]]

can = "YES"
while s:
    match_any = False
    for w in words:
        if s.startswith(w):
            s = s.replace(w, "", 1)
            match_any = True
            break
    if not match_any:
        can = "NO"
        break
print(can)
Problem p03854 (Python)

Buggy submission (status: Time Limit Exceeded):
S = input()
S = S[::-1]
C = ["maerd", "remaerd", "esare", "resare"]
ans = False
for j in range(len(S)):
    for i in range(len(S)):
        if S[0:i] in C:
            S = S[i : len(S)]
            ans = True
            break
if ans:
    print("YES")
else:
    print("NO")


Fixed submission:
S = input()
S = S[::-1]
C = ["maerd", "remaerd", "esare", "resare"]
ans = False
s = 0
for i in range(len(S) + 1):
    if S[s:i] in C:
        s = i
        ans = True
    else:
        ans = False
if ans:
    print("YES")
else:
    print("NO")
Problem p03854 (Python)

Buggy submission (status: Runtime Error):
import sys

S = input()

while S:
    prefix = S[0:5]
    if prefix == "dream":
        if S[5:7] == "er":
            if len(S) >= 8 and S[7] == "a":
                S = S[5:]
            else:
                S = S[7:]
        else:
            S = S[5:]
    elif prefix == "erase":
        if len(S) >= 6 and S[5] == "r":
            S = S[6:]
        else:
            S = S[5:]
    else:
        print("NO")
        sys.exit(0)
    print(S)
print("YES")


Fixed submission:
import sys

S = input()

while S:
    prefix = S[0:5]
    if prefix == "dream":
        if S[5:7] == "er":
            if len(S) >= 8 and S[7] == "a":
                S = S[5:]
            else:
                S = S[7:]
        else:
            S = S[5:]
    elif prefix == "erase":
        if len(S) >= 6 and S[5] == "r":
            S = S[6:]
        else:
            S = S[5:]
    else:
        print("NO")
        sys.exit(0)
    # print(S)
print("YES")
Problem p03854 (Python)

Buggy submission (status: Memory Limit Exceeded):
def we_can(s):
    if len(s) == 0:
        return True
    else:
        if s[-5:] == "dream" or s[-5:] == "erase":
            if we_can(s[:-5]):
                return True
        elif s[-6:] == "eraser":
            if we_can(s[:-6]):
                return True
        elif s[-7:] == "dreamer":
            if we_can(s[:-7]):
                return True
    return False


s = input()
if we_can(s):
    print("YES")
else:
    print("NO")


Fixed submission:
def we_can(s):
    ptr = 0
    while True:
        if len(s) == abs(ptr):
            return True
        else:
            if ptr == 0:
                if s[ptr - 5 :] == "dream" or s[ptr - 5 :] == "erase":
                    ptr -= 5
                    continue
                elif s[ptr - 6 :] == "eraser":
                    ptr -= 6
                    continue
                elif s[ptr - 7 :] == "dreamer":
                    ptr -= 7
                    continue
            if s[ptr - 5 : ptr] == "dream" or s[ptr - 5 : ptr] == "erase":
                ptr -= 5
                continue
            elif s[ptr - 6 : ptr] == "eraser":
                ptr -= 6
                continue
            elif s[ptr - 7 : ptr] == "dreamer":
                ptr -= 7
                continue
            return False


s = input()
if we_can(s):
    print("YES")
else:
    print("NO")
Problem p03854 (Python)

Buggy submission (status: Runtime Error):
# -*- coding: utf-8 -*-


def check(string):
    if string[-5:] == "dream":
        new = string[:-5]
        return new

    if string[-7:] == "dreamer":
        new = string[:-7]
        return new

    if string[-5:] == "erase":
        new = string[:-5]
        return new

    if string[-6:] == "eraser":
        new = string[:-6]
        return new

    print("NO")


if __name__ == "__main__":
    s = str(input())

    while s:
        print(s)
        s = check(s)

    if s == "":
        print("YES")


Fixed submission:
# -*- coding: utf-8 -*-


def check(string):
    if string[-5:] == "dream":
        new = string[:-5]
        return new

    if string[-7:] == "dreamer":
        new = string[:-7]
        return new

    if string[-5:] == "erase":
        new = string[:-5]
        return new

    if string[-6:] == "eraser":
        new = string[:-6]
        return new

    print("NO")


if __name__ == "__main__":
    s = str(input())

    while s:
        s = check(s)

    if s == "":
        print("YES")
Problem p03854 (Python)

Buggy submission (status: Runtime Error):
def solve():
    S = input()
    # print(S, len(S))

    S = S[::-1]
    words = ("dream"[::-1], "dreamer"[::-1], "erase"[::-1], "eraser"[::-1])

    def recur(i):
        if i >= len(S):
            return True

        for word in words:
            if S[i : i + len(word)] == word:
                if recur(i + len(word)):
                    return True
        return False

    ans = recur(0)

    return ans


if __name__ == "__main__":
    res = solve()
    if res:
        print("YES")
    else:
        print("NO")


Fixed submission:
import sys

sys.setrecursionlimit(10**6)


def solve():
    S = input()
    # print(S, len(S))

    S = S[::-1]
    words = ("dream"[::-1], "dreamer"[::-1], "erase"[::-1], "eraser"[::-1])

    def recur(i):
        if i >= len(S):
            return True

        for word in words:
            if S[i : i + len(word)] == word:
                if recur(i + len(word)):
                    return True
        return False

    ans = recur(0)

    return ans


if __name__ == "__main__":
    res = solve()
    if res:
        print("YES")
    else:
        print("NO")
Problem p03854 (Python)

Buggy submission (status: Runtime Error):
words = ("dream", "erase", "eraser", "dreamer")

s = input()

while len(s) > 0:
    match = False
    for w in words:
        if s.endwith(w):
            s = s[: -len(w)]
            match = True
            break
    if match is False:
        print("NO")
        exit()
print("YES")


Fixed submission:
words = ("dream", "erase", "eraser", "dreamer")

s = input()

while len(s) > 0:
    match = False
    for w in words:
        if s.endswith(w):
            s = s[: -len(w)]
            match = True
            break
    if match is False:
        print("NO")
        exit()
print("YES")
Problem p03854 (C++)

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

int main() {
  std::string S, result = "YES";
  std::string str_pattern[4] = {"dream", "dreamer", "erase", "eraser"};
  std::cin >> S;

  int end = S.length();
  while (end > 0) {
    bool match = false;
    for (int i = 0; i < 4; i++) {
      if (S.compare(end - str_pattern[i].length(), str_pattern[i].length(),
                    str_pattern[i]) == 0) {
        end -= str_pattern[i].length();
        match = true;
        break;
      }
    }

    if (match == false) {
      result = "NO";
      break;
    }
  }

  std::cout << result;
  return 0;
}

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

int main() {
  std::string S, result = "YES";
  std::string str_pattern[4] = {"dream", "dreamer", "erase", "eraser"};
  std::cin >> S;

  int end = S.length();
  while (end > 0) {
    bool match = false;
    for (int i = 0; i < 4; i++) {
      int start = end - str_pattern[i].length();
      if (start < 0) {
        continue;
      }

      if (S.compare(start, str_pattern[i].length(), str_pattern[i]) == 0) {
        end -= str_pattern[i].length();
        match = true;
        break;
      }
    }

    if (match == false) {
      result = "NO";
      break;
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a, b) for (int i = a; i >= b; --i)
using namespace std;

int N, A;
int x[111];
ll coudp[111][111][5555];
string divide[4] = {"dream", "dreamer", "erase", "eraser"};
int main() {
  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 j = 0; j < S.size();) {
    bool can2 = false;
    for (int i = 0; i < 4; i++) {
      string d = divide[i];
      if (S.substr(j, 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 <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a, b) for (int i = a; i >= b; --i)
using namespace std;

int N, A;
int x[111];
ll coudp[111][111][5555];
string divide[4] = {"dream", "dreamer", "erase", "eraser"};
int main() {
  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 j = 0; j < S.size();) {
    bool can2 = false;
    for (int i = 0; i < 4; i++) {
      string d = divide[i];
      if (S.substr(j, d.size()) == d) {
        can2 = true;
        j += 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 <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

string f(string x, string y) {
  while ((int)x.find(y) >= 0) {
    x.replace(x.find(y), y.length(), "0");
  }
  return x;
}

int main() {
  string s;
  string ans = "YES";
  cin >> s;
  s = f(s, "eraser");
  s = f(s, "erase");
  s = f(s, "dreamer");
  s = f(s, "dream");

  for (int i = 0; i < s.length(); i++) {
    if (s[i] != '0') {
      ans = "NO";
      break;
    }
  }
  cout << ans << endl;
  return 0;
}


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

string f(string x, string y) {
  int pos = 0;
  while (pos = x.find(y, pos), pos != string::npos) {
    x.replace(pos, y.length(), "0");
    pos = x.find(y, pos + 1);
  }
  return x;
}

int main() {
  string s;
  string ans = "YES";
  cin >> s;
  s = f(s, "eraser");
  s = f(s, "erase");
  s = f(s, "dreamer");
  s = f(s, "dream");

  for (int i = 0; i < s.length(); i++) {
    if (s[i] != '0') {
      ans = "NO";
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
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 < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;

int main() {
  // input
  string s;
  cin >> s;
  // solve
  reverse(ALL(s));
  int n = s.size();
  vector<string> t = {"dream", "dreamer", "erase", "eraser"};
  rep(i, 4) { reverse(ALL(t[i])); }
  bool flag = true;
  int i = 0;
  while (i < n and flag) {
    if (s[i] == 'm') {
      rep(j, 5) {
        if (i + j >= n or s[i + j] != t[0][j]) {
          flag = false;
          break;
        }
      }
      i += 5;
    } else if (s[i] == 'e') {
      rep(j, 5) {
        if (i + j >= n or s[i + j] != t[2][j]) {
          flag = false;
          break;
        }
      }
      i += 5;
    } else if (i + 2 < n and s[i] == 'r' and s[i + 1] == 'e') {
      if (s[i + 2] == 's') {
        rep(j, 6) {
          if (i + j >= n or s[i + j] != t[3][j]) {
            flag = false;
            break;
          }
        }
        i += 6;
      } else if (s[i + 2] == 'm') {
        rep(j, 7) {
          if (i + j >= n or s[i + j] != t[1][j]) {
            flag = false;
            break;
          }
        }
        i += 7;
      }
    } else {
      flag = false;
      break;
    }
  }
  // output
  if (flag)
    printf("YES\n");
  else
    printf("NO\n");
}

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

int main() {
  // input
  string s;
  cin >> s;
  // solve
  reverse(ALL(s));
  int n = s.size();
  vector<string> t = {"dream", "dreamer", "erase", "eraser"};
  rep(i, 4) { reverse(ALL(t[i])); }
  bool flag = true;
  int i = 0;
  while (i < n and flag) {
    if (s[i] == 'm') {
      rep(j, 5) {
        if (i + j >= n or s[i + j] != t[0][j]) {
          flag = false;
          break;
        }
      }
      i += 5;
    } else if (s[i] == 'e') {
      rep(j, 5) {
        if (i + j >= n or s[i + j] != t[2][j]) {
          flag = false;
          break;
        }
      }
      i += 5;
    } else if (i + 2 < n and s[i] == 'r' and s[i + 1] == 'e') {
      if (s[i + 2] == 's') {
        rep(j, 6) {
          if (i + j >= n or s[i + j] != t[3][j]) {
            flag = false;
            break;
          }
        }
        i += 6;
      } else if (s[i + 2] == 'm') {
        rep(j, 7) {
          if (i + j >= n or s[i + j] != t[1][j]) {
            flag = false;
            break;
          }
        }
        i += 7;
      } else {
        flag = false;
        break;
      }
    } else {
      flag = false;
      break;
    }
  }
  // output
  if (flag)
    printf("YES\n");
  else
    printf("NO\n");
}
Problem p03854 (C++)

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

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

int main() {
  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 (!can) {
      can = false;
      break;
    }
  }

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


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

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

int main() {
  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;
}
Problem p03854 (C++)

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

using namespace std;

int main() {
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  // Sを逆順にソートした
  // 文字列分割がわからない
  string divide[4] = {"maerd", "remaerd", "esare", "resare"};
  bool big_flag = true;

  for (int i = 0; i < S.size();) {
    bool flag = false;
    for (int j = 0; j < 4; j++) {
      if (S.substr(i, divide[j].size()) == divide[j]) {
        flag = true;
        i += divide[j].size();
      }
    }
    if (!flag) {
      big_flag = false;
    }
  }

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

  return 0;
}


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

using namespace std;

int main() {
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  // Sを逆順にソートした
  // 文字列分割がわからない
  string divide[4] = {"maerd", "remaerd", "esare", "resare"};
  bool big_flag = true;

  for (int i = 0; i < S.size();) {
    bool flag = false;
    for (int j = 0; j < 4; j++) {
      if (S.substr(i, divide[j].size()) == divide[j]) {
        flag = true;
        i += divide[j].size();
      }
    }
    if (!flag) {
      big_flag = false;
      break;
    }
  }

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

  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char s[111111], e[11], d[11];
int n, pos;
int main() {
  e[0] = 'e';
  e[1] = 'r';
  e[2] = 'a';
  e[3] = 's';
  e[4] = 'e';
  d[0] = 'd';
  d[1] = 'r';
  d[2] = 'e';
  d[3] = 'a';
  d[4] = 'm';
  scanf("%s", s);
  n = strlen(s);
  pos = 0;
  while (pos < n) {
    if (s[pos] == 'e') {
      for (int i = 0; i < 5; i++) {
        if (s[pos + i] != e[i]) {
          cout << "NO" << endl;
          return 0;
        }
      }
      pos += 5;
      if (s[pos] == 'r')
        pos++;
    } else if (s[pos] == 'd') {
      for (int i = 0; i < 5; i++) {
        if (s[pos + i] != d[i]) {
          cout << "NO" << endl;
          return 0;
        }
      }
      pos += 5;
      if (s[pos] == 'e') {
        if (s[pos + 1] != 'r') {
          cout << "NO" << endl;
          return 0;
        }
        if (s[pos + 2] != 'a')
          pos += 2;
      }
    }
  }
  cout << "YES" << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char s[111111], e[11], d[11];
int n, pos;
int main() {
  e[0] = 'e';
  e[1] = 'r';
  e[2] = 'a';
  e[3] = 's';
  e[4] = 'e';
  d[0] = 'd';
  d[1] = 'r';
  d[2] = 'e';
  d[3] = 'a';
  d[4] = 'm';
  scanf("%s", s);
  n = strlen(s);
  pos = 0;
  while (pos < n) {
    if (s[pos] == 'e') {
      for (int i = 0; i < 5; i++) {
        if (s[pos + i] != e[i]) {
          cout << "NO" << endl;
          return 0;
        }
      }
      pos += 5;
      if (s[pos] == 'r')
        pos++;
    } else if (s[pos] == 'd') {
      for (int i = 0; i < 5; i++) {
        if (s[pos + i] != d[i]) {
          cout << "NO" << endl;
          return 0;
        }
      }
      pos += 5;
      if (s[pos] == 'e') {
        if (s[pos + 1] != 'r') {
          cout << "NO" << endl;
          return 0;
        }
        if (s[pos + 2] != 'a')
          pos += 2;
      }
    } else {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
  return 0;
}