alexjercan/bugnet huggingface dataset:
Problem p01486 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stack>
#include <string>

using namespace std;

void catChecker(string str) {

  stack<char> cnt;

  for (int i = 0; i < str.size(); i++) {
    char c = str.at(i);

    if (c == 'm') {
      cnt.push(c);
    } else if (!cnt.empty()) {
      if (c == 'e' && cnt.top() == 'm') {
        cnt.pop();
        cnt.push(c);
      } else if (c == 'w' && cnt.top() == 'e') {
        cnt.pop();
      } else {
        cout << "Rabbit" << endl;
        return;
      }
    } else {
      cout << "Rabbit" << endl;
      return;
    }

    if (i < str.size() - 1 && cnt.empty()) {
      cout << "Rabbit" << endl;
      return;
    }
  }
  if (cnt.empty()) {
    cout << "Cat" << endl;
  } else {
    cout << "Rabbit" << endl;
  }
  return;
}

int main(void) {

  string str;

  while (cin >> str)
    catChecker(str);

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stack>
#include <string>

using namespace std;

void catChecker(string str) {

  stack<char> cnt;

  for (int i = 0; i < str.size(); i++) {
    char c = str.at(i);

    if (c == 'm') {
      cnt.push(c);
    } else if (c == 'e' && cnt.top() == 'm') {
      cnt.pop();
      cnt.push(c);
    } else if (c == 'w' && cnt.top() == 'e') {
      cnt.pop();
    } else {
      cout << "Rabbit" << endl;
      return;
    }

    if (i < str.size() - 1 && cnt.empty()) {
      cout << "Rabbit" << endl;
      return;
    }
  }
  if (cnt.empty()) {
    cout << "Cat" << endl;
  } else {
    cout << "Rabbit" << endl;
  }
  return;
}

int main(void) {

  string str;

  while (cin >> str)
    catChecker(str);

  return 0;
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <string>
using namespace std;
string S;
int dp[505][505];
bool is_cat(int l, int r) //[l,r)
{
  if (l == r)
    return true;
  if (S[l] != 'm' || S[r - 1] != 'w')
    return false;
  if (dp[l][r] != -1)
    return dp[l][r];
  for (int i = l + 1; i < r - 1; i++) {
    if (S[i] == 'e') {
      if (is_cat(l + 1, i) && is_cat(i + 1, r - 1))
        return dp[l][r] = true;
    }
  }
  return dp[l][r] = false;
}
int main() {
  memset(dp, -1, sizeof(dp));
  cin >> S;
  if (is_cat(0, (int)S.size()))
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
string S;
bool is_cat(int l, int r) //[l,r)
{
  if (l == r)
    return true;
  if (S[l] != 'm' || S[r - 1] != 'w')
    return false;
  for (int i = l + 1; i < r - 1; i++) {
    if (S[i] == 'e') {
      if (is_cat(l + 1, i) && is_cat(i + 1, r - 1))
        return true;
    }
  }
  return false;
}
int main() {
  cin >> S;
  if (is_cat(0, (int)S.size()))
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
  return 0;
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

string s;

int dp[512][512];

int solve(int l, int r) {
  int &res = dp[l][r];
  if (res != -1)
    return res;

  if (l == r)
    res = true;
  else if (r - l < 3)
    res = false;
  else if (r - l == 3)
    res = s.substr(l, 3) == "mew";
  else if (s[l] != 'm' || s[r - 1] != 'w')
    res = false;
  else {
    res = false;
    for (int i = l + 1; i < r; i++) {
      if (s[i] == 'e') {
        if (solve(l + 1, i) && solve(i + 1, r - 1)) {
          res = true;
          break;
        }
      }
    }
  }
  return res;
}

int main() {
  while (cin >> s) {
    memset(dp, -1, sizeof(dp));
    cout << (solve(0, s.size()) ? "Cat" : "Rabbit") << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

string s;

bool solve(int l, int r) {
  if (l == r)
    return true;
  if (r - l < 3)
    return false;
  if (r - l == 3)
    return s.substr(l, 3) == "mew";
  if (s[l] != 'm' || s[r - 1] != 'w')
    return false;
  for (int i = l + 1; i < r; i++) {
    if (s[i] == 'e') {
      if (solve(l + 1, i) && solve(i + 1, r - 1))
        return true;
    }
  }
  return false;
}

int main() {
  while (cin >> s) {
    cout << (solve(0, s.size()) ? "Cat" : "Rabbit") << endl;
  }
}
Problem p01486 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  string s;
  cin >> s;
  stack<char> st;
  bool ans = true;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'w' && st.size() > 1 && st.top() == 'e' &&
        (i == s.length() - 1 || s[i + 1] != 'm')) {
      st.pop();
      st.pop();
    } else {
      st.push(s[i]);
    }
    if (st.empty() && i < s.length() - 1) {
      ans = false;
    }
  }
  if (!st.empty())
    ans = false;
  if (ans)
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  string s;
  cin >> s;
  stack<char> st;
  bool ans = true;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'w' && st.top() == 'e' && st.size() > 1 &&
        (i == s.length() - 1 || s[i + 1] != 'm')) {
      st.pop();
      st.pop();
    } else {
      st.push(s[i]);
    }
    if (st.empty() && i < s.length() - 1) {
      ans = false;
    }
  }
  if (!st.empty())
    ans = false;
  if (ans)
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
  return 0;
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
string s;
bool iscat(int l, int r) {
  if (l > r)
    return true;
  if (s[l] != 'm' || s[r] != 'w')
    return false;
  int rec = 0;
  for (int i = l + 1; i < r; i++) {
    if (rec == 0 && s[i] == 'e')
      return iscat(l + 1, i - 1) && iscat(i + 1, r - 1);
    else if (s[i] == 'm')
      rec++;
    else if (s[i] == 'w')
      rec--;
    if (rec < 0)
      return false;
  }
}
int main() {
  cin >> s;
  cout << (iscat(0, s.size() - 1) ? "Cat" : "Rabbit") << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
string s;
bool iscat(int l, int r) {
  if (l > r)
    return true;
  if (s[l] != 'm' || s[r] != 'w')
    return false;
  for (int i = l + 1; i < r; i++) {
    if (s[i] == 'e' && iscat(l + 1, i - 1) && iscat(i + 1, r - 1)) {
      return true;
    }
  }
  return false;
}
int main() {
  cin >> s;
  cout << (iscat(0, s.size() - 1) ? "Cat" : "Rabbit") << endl;
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
string s;
int memo[500][501];
bool cat(int l, int r) {
  if (memo[l][r] != 0)
    return memo[l][r] > 0;
  int sz = r - l;
  if (sz == 0)
    return true;
  string t = s.substr(l, sz);
  if (t[0] != 'm' || t[sz - 1] != 'w') {
    memo[l][r] = -1;
    return false;
  }
  bool ok = false;
  rep(i, sz) {
    if (t[i] != 'e')
      continue;
    if (cat(l + 1, l + i) && cat(l + i + 1, r - 1))
      ok = true;
  }
  memo[l][r] = (ok ? 1 : -1);
  return ok;
}
int main() {
  cin >> s;
  cout << (cat(0, s.size()) ? "Cat\\n" : "Rabbit\\n");
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
string s;
bool cat(string t) {
  int sz = t.size();
  if (sz == 0)
    return true;
  if (t[0] != 'm' || t[sz - 1] != 'w')
    return false;
  bool ok = false;
  rep(i, sz) {
    if (t[i] != 'e')
      continue;
    if (cat(t.substr(1, i - 1)) && cat(t.substr(i + 1, sz - i - 2)))
      ok = true;
  }
  return ok;
}
int main() {
  cin >> s;
  cout << (cat(s) ? "Cat\\n" : "Rabbit\\n");
  return 0;
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <string.h>
char str[512];
int n;
int dp[500][500];
int solve(int a, int b) {
  if (a == b)
    return 1;
  if (~dp[a][b])
    return dp[a][b];
  if (str[a] != 'm' || str[b - 1] != 'w')
    return 0;
  for (int i = a + 1; i < b - 1; i++) {
    if (str[i] == 'e') {
      int v = solve(a + 1, i) & solve(i + 1, b - 1);
      if (v)
        return dp[a][b] = 1;
    }
  }
  return dp[a][b] = 0;
}
int main() {
  scanf("%s", str);
  n = strlen(str);
  for (int i = 0; i < 500; i++)
    for (int j = 0; j < 500; j++)
      dp[i][j] = -1;
  printf("%s\\n", solve(0, n) ? "Cat" : "Rabbit");
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>
char str[512];
int n;
int solve(int a, int b) {
  if (a == b)
    return 1;
  if (str[a] != 'm' || str[b - 1] != 'w')
    return 0;
  for (int i = a + 1; i < b - 1; i++) {
    if (str[i] == 'e') {
      int v = solve(a + 1, i) & solve(i + 1, b - 1);
      if (v)
        return 1;
    }
  }
  return 0;
}
int main() {
  scanf("%s", str);
  n = strlen(str);
  printf("%s\\n", solve(0, n) ? "Cat" : "Rabbit");
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;

using namespace std;

bool ans = false;
int length, e_loc[501], e_index = 0;
int memo[500][500];

char buf[501];

bool recursive(int left, int right) {
  if (ans)
    return true;
  if (buf[left] != 'm' || buf[right] != 'w')
    return false;

  bool FLG = false;
  for (int i = 0; i < e_index; i++) {
    if (left < e_loc[i] && right > e_loc[i]) { // m??¨w???????????????e???????´¢
      FLG = true;
      if (((e_loc[i] == left + 1) || memo[left + 1][e_loc[i] - 1] == 1 ||
           (memo[left + 1][e_loc[i] - 1] != 99 &&
            recursive(left + 1, e_loc[i] - 1) ==
                true)) && // m??¨e???????????????????????????????????????????????????true
          ((e_loc[i] == right - 1) || memo[e_loc[i] + 1][right - 1] == 1 ||
           (memo[e_loc[i] + 1][right - 1] != 99 &&
            recursive(e_loc[i] + 1, right - 1)) ==
               true)) { // e??¨w???????????????????????????????????????????????????true
        if (left == 0 && right == length - 1) { //?????????????????°
          ans = true;
        }
        memo[left][right] = 1;
        return true;
      }
    }
  }
  memo[left][right] = 99; // false??§???????????¨????¨????
  return false; //??£???????????°???????????§??§true????????£???????????????????????§false?????????
}

int main() {

  scanf("%s", buf);

  for (length = 0; buf[length] != '\\0'; length++) {
    if (buf[length] == 'e') {
      e_loc[e_index++] = length;
    }
  }

  for (int i = 0; i < length; i++) {
    for (int k = 0; k < length; k++)
      memo[i][k] = -1;
  }

  recursive(0, length - 1);
  if (ans) {
    printf("Cat\\n");
  } else {
    printf("Rabbit\\n");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;

using namespace std;

bool ans = false;
int length, e_loc[501], e_index = 0;
;
char buf[501];

bool recursive(int left, int right) {
  if (ans)
    return true;
  if (buf[left] != 'm' || buf[right] != 'w')
    return false;

  bool FLG = false;
  bool FLG2 = false;
  for (int i = 0; i < e_index; i++) {
    if (left + 1 <= e_loc[i] && right - 1 >= e_loc[i]) {
      FLG = true;
      if (((e_loc[i] == left + 1) ||
           (recursive(left + 1, e_loc[i] - 1) == true)) &&
          ((e_loc[i] == right - 1) ||
           (recursive(e_loc[i] + 1, right - 1)) == true)) {
        // if(left == 0 && right == length-1){
        ans = true;
        FLG2 = true;
        break;
        //}
        // FLG2 = true;
      }
    }
  }
  if (!FLG)
    return false;

  return FLG2;
}

int main() {

  scanf("%s", buf);

  for (length = 0; buf[length] != '\\0'; length++) {
    if (buf[length] == 'e') {
      e_loc[e_index++] = length;
    }
  }

  recursive(0, length - 1);
  if (ans) {
    printf("Cat\\n");
  } else {
    printf("Rabbit\\n");
  }

  return 0;
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <utility>
using namespace std;
typedef pair<int, int> P;
#define _CRT_SECURE_NO_WARNINGS
int N;
char s[502];
int dp[502][502];
bool Serch(int l, int r) {
  if (l > r) {
    return true;
  }
  if (s[l] != 'm') {
    return false;
  }
  if (s[r] != 'w') {
    return false;
  }
  if (dp[l][r] != -1) {
    if (dp[l][r] == 0) {
      return false;
    } else {
      return true;
    }
  }
  bool ans = false;

  for (int i = l + 1; i < r; i++) {
    if (s[i] == 'e') {
      if (Serch(l + 1, i - 1) && Serch(i + 1, r - 1)) {
        dp[l][r] = 1;
        ans = true;
        break;
      }
    }
  }
  if (ans == false) {
    dp[l][r] = false;
  }

  return ans;
}

int main() {
  bool ans;
  int end;

  scanf("%s", s);
  for (int i = 0; i < 500; i++) {
    if (s[i] != 'm' && s[i] != 'e' && s[i] != 'w') {
      end = i;
      for (int k = 0; k < i; k++) {
        for (int j = 0; j < i; j++) {
          dp[j][k] = -1;
        }
      }
      break;
    }
  }
  ans = Serch(0, end - 1);

  if (ans) {
    printf("Cat\\n");
  } else {
    printf("Rabbit\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <utility>
using namespace std;
typedef pair<int, int> P;
#define _CRT_SECURE_NO_WARNINGS
int N;
char s[502];
bool Serch(int l, int r) {
  if (l > r) {
    return true;
  }
  if (s[l] != 'm') {
    return false;
  }
  if (s[r] != 'w') {
    return false;
  }
  bool ans = false;

  for (int i = l + 1; i < r; i++) {
    if (s[i] == 'e') {
      if (Serch(l + 1, i - 1) && Serch(i + 1, r - 1)) {
        ans = true;
        break;
      }
    }
  }

  return ans;
}

int main() {
  bool ans;
  int end;

  scanf("%s", s);
  for (int i = 0; i < 500; i++) {
    if (s[i] != 'm' && s[i] != 'e' && s[i] != 'w') {
      end = i;
      break;
    }
  }
  ans = Serch(0, end - 1);

  if (ans) {
    printf("Cat\\n");
  } else {
    printf("Rabbit\\n");
  }
  return 0;
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool iscat(const string &S_) {
  map<string, int> vis;
  function<bool(const string &)> dfs = [&](const string &S) {
    if (S.empty())
      return true;
    if (S[0] != 'm' || S.back() != 'w')
      return false;
    if (vis.count(S))
      return vis[S] == 1;
    rep(i, S.size()) if (S[i] == 'e') {
      if (dfs(S.substr(1, i - 1)) && dfs(S.substr(i + 1, S.size() - i - 2))) {
        vis[S] = 1;
        return true;
      }
    }
    vis[S] = 0;
    return false;
  };
  return dfs(S_);
}

int main() {
  string S;
  cin >> S;
  cout << (iscat(S) ? "Cat" : "Rabbit") << endl;
}

Error: TLE

Fixed submission:

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

bool iscat(const string &S_) {
  map<string, int> vis;
  function<bool(const string &)> dfs = [&](const string &S) {
    if (S.empty())
      return true;
    if (S[0] != 'm' || S.back() != 'w')
      return false;
    if (vis.count(S))
      return vis[S] == 1;
    rep(i, S.size()) if (S[i] == 'e') {
      if (iscat(S.substr(1, i - 1)) &&
          iscat(S.substr(i + 1, S.size() - i - 2))) {
        vis[S] = 1;
        return true;
      }
    }
    vis[S] = 0;
    return false;
  };
  return dfs(S_);
}

int main() {
  string S;
  cin >> S;
  cout << (iscat(S) ? "Cat" : "Rabbit") << endl;
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int dp[510][510];

// 半開区間[0, r)が猫かどうか
bool rec(string s, int l, int r) {

  if (dp[l][r] != -1)
    return dp[l][r];

  // 空文字列ならok
  if (l == r) {
    dp[l][r] = 1;
    return true;
  }
  // cout << l << " " << r << endl;
  if (s[l] != 'm') {
    dp[l][r] = 0;
    return false;
  }

  if (s[r - 1] != 'w') {
    dp[l][r] = 0;
    return false;
  }

  l++;
  r--;
  // cout << l << r << endl;
  // eの添え字を見つける
  vector<int> idx;
  for (int i = l; i < r; i++) {
    if (s[i] == 'e')
      idx.push_back(i);
  }

  if (idx.size() == 0) {
    dp[l - 1][r + 1] = 0;
    return false;
  }

  // 各eに対して頑張る
  bool ok = false;
  for (int i = 0; i < idx.size(); i++) {
    bool flag1 = rec(s, l, idx[i]);
    bool flag2 = rec(s, idx[i] + 1, r);

    // cout << flag1 << " " << flag2 << endl;
    if (flag1 && flag2) {
      ok = true;
      break;
    }
  }

  if (ok)
    dp[l - 1][r + 1] = 1;
  else
    dp[l - 1][r + 1] = 0;

  return ok;
}

int main() {

  memset(dp, -1, sizeof(dp));

  string s;
  cin >> s;
  int n = (int)s.size();
  if (rec(s, 0, n))
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
}


Error: TLE

Fixed submission:

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

// 半開区間[0, r)が猫かどうか
bool rec(string s, int l, int r) {

  // 空文字列ならok
  if (l == r)
    return true;
  // cout << l << " " << r << endl;
  if (s[l] != 'm')
    return false;

  if (s[r - 1] != 'w')
    return false;

  l++;
  r--;
  // cout << l << r << endl;
  // eの添え字を見つける
  vector<int> idx;
  for (int i = l; i < r; i++) {
    if (s[i] == 'e')
      idx.push_back(i);
  }

  if (idx.size() == 0)
    return false;

  // 各eに対して頑張る
  bool ok = false;
  for (int i = 0; i < idx.size(); i++) {
    bool flag1 = rec(s, l, idx[i]);
    bool flag2 = rec(s, idx[i] + 1, r);

    // cout << flag1 << " " << flag2 << endl;
    if (flag1 && flag2) {
      ok = true;
      break;
    }
  }

  return ok;
}

int main() {

  string s;
  cin >> s;
  int n = (int)s.size();
  if (rec(s, 0, n))
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2369

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

string A[4] = {"mew", "mXew", "meXw", "mXeXw"};

int main() {
  string S;
  cin >> S;
  if (S == "")
    S = "X";
  for (int s = S.size(); true; s = S.size()) {
    int i;
    for (int j = 0; j < 4; j++) {
      if ((i = S.find(A[j])) != -1) {
        S.replace(i, A[j].size(), "X");
      }
    }
    if (s == S.size())
      break;
  }
  if (S == "X") {
    cout << "Cat" << endl;
  } else {
    cout << "Rabbit" << endl;
  }
}

Error: TLE

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2369

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

bool iscat(string S) {
  if (S.size() == 0)
    return true;
  if (S[0] != 'm')
    return false;
  if (S[S.size() - 1] != 'w')
    return false;
  for (unsigned int i = 1; i < S.size() - 1; i++) {
    if (S[i] == 'e' && iscat(S.substr(1, i - 1)) &&
        iscat(S.substr(i + 1, S.size() - 2 - i)))
      return true;
  }
  return false;
}

int main() {
  string S;
  cin >> S;
  if (iscat(S))
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

string s;
bool memo[500][500] = {}, visit[500][500] = {};

bool cat(int i, int j) {
  if (visit[i][j])
    return memo[i][j];
  visit[i][j] = true;
  if (i >= j)
    return memo[i][j] = true;
  if (s[i] != 'm' || s[j] != 'w')
    return memo[i][j] = false;
  bool ret = false;
  for (int k = i + 1; k < j; k++) {
    if (s[k] == 'e') {
      ret |= (cat(i + 1, k - 1) && cat(k + 1, j - 1));
    }
  }
  return memo[i][j] = ret;
}

int main() {
  cin >> s;
  cout << (cat(0, s.length() - 1) ? "Cat" : "Rabbit") << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

string s;

bool cat(int i, int j) {
  if (i >= j)
    return true;
  if (s[i] != 'm' || s[j] != 'w')
    return false;
  bool ret = false;
  for (int k = i + 1; k < j; k++) {
    if (s[k] == 'e') {
      ret |= (cat(i + 1, k - 1) && cat(k + 1, j - 1));
    }
  }
  return ret;
}

int main() {
  cin >> s;
  cout << (cat(0, s.length() - 1) ? "Cat" : "Rabbit") << endl;
  return 0;
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
string s;
bool m[500][500] = {}, v[500][500] = {};
bool f(int i, int j) {
  if (v[i][j])
    return m[i][j];
  v[i][j] = true;
  if (i >= j)
    return m[i][j] = true;
  if (s[i] != 'm' || s[j] != 'w')
    return m[i][j] = false;
  bool r = false;
  for (int k = i + 1; k < j; k++) {
    if (s[k] == 'e') {
      r |= (f(i + 1, k - 1) && f(k + 1, j - 1));
    }
  }
  return m[i][j] = r;
}
int main() {
  cin >> s;
  cout << (f(0, s.length() - 1) ? "Cat" : "Rabbit") << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

string s;

bool cat(int i, int j) {
  if (i >= j)
    return true;
  if (s[i] != 'm' || s[j] != 'w')
    return false;
  bool ret = false;
  for (int k = i + 1; k < j; k++) {
    if (s[k] == 'e') {
      ret |= (cat(i + 1, k - 1) && cat(k + 1, j - 1));
      if (ret)
        return ret;
    }
  }
  return false;
}

int main() {
  cin >> s;
  cout << (cat(0, s.length() - 1) ? "Cat" : "Rabbit") << endl;
  return 0;
}
Problem p01486 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

  vector<char> stack1;

  for (int i = 0; i < S.size(); i++) {
    if (i != 0 && stack1.empty()) {
      goto Rabbit;
    }

    stack1.push_back(S[i]);

    if (stack1.size() >= 3) {
      int end = stack1.size() - 1;

      if (stack1[end - 2] == 'm' && stack1[end - 1] == 'e' &&
          stack1[end] == 'w') {
        stack1.pop_back();
        stack1.pop_back();
        stack1.pop_back();
      }
    }
  }

  if (stack1.size() == 0) {
    goto Cat;
  }

Rabbit:;

  cout << "Rabbit" << endl;
  goto Exit;

Cat:;

  cout << "Cat" << endl;

Exit:;

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>

using namespace std;

int main() {
  string S;

  cin >> S;

  queue<string> que;

  que.push(S);

  while (!que.empty()) {
    string S1 = que.front();
    que.pop();

    if (S1 == "mew") {
      goto Cat;
    }

    if (S1[0] == 'm' && S1[S1.size() - 1] == 'w') {
      S1.erase(0, 1);
      S1.erase(S1.size() - 1, S1.size());

      for (int i = 0; i < S1.size(); i++) {
        if (S1[i] == 'e') {
          string S2 = S1;

          S2.erase(i, i + 1);

          que.push(S2);
        }
      }
    }
  }

  cout << "Rabbit" << endl;
  goto Exit;

Cat:

  cout << "Cat" << endl;

Exit:;

  return 0;
}