alexjercan/bugnet huggingface dataset:
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

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

map<string, bool> mp;
bool check(string s) {
  if (s.empty())
    return true;
  if (*s.begin() != 'm' || *s.rbegin() != 'w')
    return false;
  if (mp.find(s) != mp.end())
    return mp[s];
  for (int i = 1; i < s.size() - 1; i++) {
    if (s[i] != 'e')
      continue;
    if (check(s.substr(1, i - 1)) && check(s.substr(i + 1, s.size() - i - 2)))
      return mp[s] = true;
  }
  return mp[s] = false;
}
int main() {
  string s;
  cin >> s;
  puts(check(s) ? "Cat" : "Rabbit");
}

Error: TLE

Fixed submission:

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

bool check(string s) {
  if (s.empty())
    return true;
  if (*s.begin() != 'm' || *s.rbegin() != 'w')
    return false;
  for (int i = 1; i < s.size() - 1; i++) {
    if (s[i] != 'e')
      continue;
    if (check(s.substr(1, i - 1)) && check(s.substr(i + 1, s.size() - i - 2)))
      return true;
  }
  return false;
}
int main() {
  string s;
  cin >> s;
  puts(check(s) ? "Cat" : "Rabbit");
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

using namespace std;

string S;

char dp[501][501];

char dfs(int l, int r) {
  // cout << "dfs: " << l << "," << r << endl;
  if (dp[l][r] != -1) {
    return dp[l][r];
  }
  if (l == r) {
    return dp[l][r] = 1;
  } else if (r - l == 1 || r - l == 2) {
    return dp[l][r] = 0;
  }
  if (!(S[l] == 'm' && S[r - 1] == 'w')) {
    return dp[l][r] = 0;
  }
  for (int i = l + 1; i < r - 1; i++) {
    if (S[i] != 'e')
      continue;
    if (dfs(l + 1, i) && dfs(i + 1, r - 1)) {
      return dp[l][r] = 1;
    }
  }
  return dp[l][r] = 0;
}

int main() {
  cin >> S;
  for (int i = 0; i < 501; i++)
    for (int j = 0; j < 501; j++) {
      dp[i][j] = -1;
    }
  if (dfs(0, S.size()) == 1) {
    cout << "Cat" << endl;
  } else {
    cout << "Rabbit" << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

string S;

bool dfs(int l, int r) {
  // cout << "dfs: " << l << ", " << r << endl;
  if (l == r) {
    return true;
  } else if (r - l == 1 || r - l == 2) {
    return false;
  }
  if (!(S[l] == 'm' && S[r - 1] == 'w')) {
    return false;
  }
  bool ans = false;
  for (int i = l + 1; i < r - 1; i++) {
    if (S[i] != 'e')
      continue;
    ans = ans || (dfs(l + 1, i) && dfs(i + 1, r - 1));
  }
  return ans;
}

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

#include <bits/stdc++.h>

using namespace std;

map<string, bool> memo;

bool parse(string s) {
  if (memo.find(s) != memo.end())
    return memo[s];
  // cout << s << endl;
  if (s == "")
    return true;
  if (s == "mew")
    return true;
  int const size = s.size();

  bool ok = 0;
  for (int i = 1; i < size; i++) {
    if (s[i] == 'e') {
      string temp;
      string t = "m";
      if (i - 1 >= 0) {
        if (parse(temp = s.substr(1, i - 1)))
          t += temp;
        else
          continue;
      }
      t += "e";
      if (size - i - 2 >= 0) {
        if (parse(temp = s.substr(i + 1, size - i - 2)))
          t += temp;
        else
          continue;
      }
      t += "w";
      ok |= s == t;
      // cout << (ok ? "true: " : "false: ") << s  << " " << t<< endl;
    }
  }
  return memo[s] = ok;
}

string checker(string s) { return parse(s) ? "Cat" : "Rabbit"; }

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

  cout << checker(s) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

bool parse(string s) {
  // cout << s << endl;
  if (s == "")
    return true;
  if (s == "mew")
    return true;
  int const size = s.size();

  bool ok = 0;
  for (int i = 1; i < size; i++) {
    if (s[i] == 'e') {
      string temp;
      string t = "m";
      if (i - 1 >= 0) {
        if (parse(temp = s.substr(1, i - 1)))
          t += temp;
        else
          continue;
      }
      t += "e";
      if (size - i - 2 >= 0) {
        if (parse(temp = s.substr(i + 1, size - i - 2)))
          t += temp;
        else
          continue;
      }
      t += "w";
      ok |= s == t;
      // cout << (ok ? "true: " : "false: ") << s  << " " << t<< endl;
    }
  }
  return ok;
}

string checker(string s) { return parse(s) ? "Cat" : "Rabbit"; }

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

  cout << checker(s) << endl;

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

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

map<string, bool> memo;

bool parse(string s) {
  if (memo.find(s) != memo.end())
    return memo[s];
  if (s == "")
    return 1;
  int const size = s.size();
  if (s[0] != 'm' || s[size - 1] != 'w')
    return memo[s] = 0;

  bool ok = 0;
  for (int i = 1; i < size; i++) {
    if (s[i] == 'e') {
      if (parse(s.substr(1, i - 1)) && parse(s.substr(i + 1, size - i - 2)))
        return memo[s] = 1;
    }
  }
  return memo[s] = 0;
}

string checker(string s) { return parse(s) ? "Cat" : "Rabbit"; }

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

  cout << checker(s) << endl;

  return 0;
}
/*
以下は無駄なことしてて2秒くらいかかるコード
bool parse(string s) {
  if(memo.find(s) != memo.end()) return memo[s];
  if(s == "") return true;
  int const size = s.size();

  bool ok = 0;
  for(int i=1; i<size; i++) {
    if(s[i] == 'e') {
      string temp;
      string t = "m";
      if(i-1>=0) {
        if(parse(temp = s.substr(1, i-1))) t+=temp;
        else continue;
      }
      t += "e";
      if(size-i-2>=0) {
        if(parse(temp = s.substr(i+1, size-i-2))) t+=temp;
        else continue;
      }
      t +="w";
      ok |= s == t;
    }
  }
  return memo[s] = ok;
}
*/

Error: TLE

Fixed submission:

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

map<string, bool> memo;

bool parse(string s) {
  if (memo.find(s) != memo.end())
    return memo[s];
  if (s == "")
    return 1;
  int const size = s.size();

  bool ok = 0;
  for (int i = 1; i < size; i++) {
    if (s[i] == 'e') {
      if (parse(s.substr(1, i - 1)) && parse(s.substr(i + 1, size - i - 2)))
        return 1;
    }
  }
  return memo[s] = 0;
}

string checker(string s) { return parse(s) ? "Cat" : "Rabbit"; }

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

  cout << checker(s) << endl;

  return 0;
}
/*
以下は無駄なことしてて2秒くらいかかるコード
bool parse(string s) {
  if(memo.find(s) != memo.end()) return memo[s];
  if(s == "") return true;
  int const size = s.size();

  bool ok = 0;
  for(int i=1; i<size; i++) {
    if(s[i] == 'e') {
      string temp;
      string t = "m";
      if(i-1>=0) {
        if(parse(temp = s.substr(1, i-1))) t+=temp;
        else continue;
      }
      t += "e";
      if(size-i-2>=0) {
        if(parse(temp = s.substr(i+1, size-i-2))) t+=temp;
        else continue;
      }
      t +="w";
      ok |= s == t;
    }
  }
  return memo[s] = ok;
}
*/
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

const int SIZE = 501;
char memo[SIZE][SIZE];

bool rec(string &str, int begin, int end) {
  if (begin > end) {
    return true;
  }
  if (begin == end) {
    return false;
  }
  if (str[begin] != 'm' || str[end] != 'w') {
    return false;
  }
  if (memo[begin][end] != -1) {
    return (bool)memo[begin][end];
  }
  bool result = false;
  for (int i = begin + 1; i < end; ++i) {
    if (str[i] == 'e') {
      result |= rec(str, begin + 1, i - 1) && rec(str, i + 1, end - 1);
    }
  }
  memo[begin][end] = (char)result;
  return result;
}

int main() {
  string str;
  while (cin >> str) {
    memset(memo, -1, SIZE * SIZE);
    cout << (rec(str, 0, str.size() - 1) ? "Cat" : "Rabbit") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

bool rec(string &str, int begin, int end) {
  if (begin > end) {
    return true;
  }
  if (begin == end) {
    return false;
  }
  if (str[begin] != 'm' || str[end] != 'w') {
    return false;
  }
  bool result = false;
  for (int i = begin + 1; i < end; ++i) {
    if (str[i] == 'e') {
      result |= rec(str, begin + 1, i - 1) && rec(str, i + 1, end - 1);
    }
  }
  return result;
}

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

#include <bits/stdc++.h>
using namespace std;
map<string, bool> used;

bool rec(string str) {
  if (used.find(str) != used.end())
    return used[str];

  if (str.empty())
    return true;
  if (str[0] != 'm' || str[str.size() - 1] != 'w')
    return used[str] = false;

  // 適当に真ん中を決める
  for (int i = 1; i < str.size() - 1; i++) {
    if (str[i] == 'e') {
      if (i == 1 || i == str.size() - 2 ||
          (i >= 4 || i <= str.size() - 5)) { // 可能性がありそう(てきとー
        string right = str.substr(1, i - 1);
        string left = str.substr(i + 1, str.size() - i - 2);
        if (rec(right) && rec(left))
          return used[str] = true;
      }
    }
  }
  return used[str] = false;
}

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

  if (rec(str))
    puts("Cat");
  else
    puts("Rabbit");
}

Error: TLE

Fixed submission:

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

bool rec(string str) {

  if (str.empty())
    return true;
  if (str[0] != 'm' || str[str.size() - 1] != 'w')
    return false;

  // 適当に真ん中を決める
  for (int i = 1; i < str.size() - 1; i++) {
    if (str[i] == 'e') {
      if (i == 1 || i == str.size() - 2 ||
          (i >= 4 || i <= str.size() - 5)) { // 可能性がありそう(てきとー
        string right = str.substr(1, i - 1);
        string left = str.substr(i + 1, str.size() - i - 2);
        if (rec(right) && rec(left))
          return true;
      }
    }
  }
  return false;
}

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

  if (rec(str))
    puts("Cat");
  else
    puts("Rabbit");
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

signed main(void) {
  string s;
  cin >> s;

  while (true) {
    bool flag = false;
    FOR(i, 3, s.size() - 1) {
      if (s.substr(i - 2, 3) == "mew") {
        s.erase(i - 2, 3);
        flag = true;
      }
    }
    if (!flag)
      break;
  }

  if (s == "mew")
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;

  return 0;
}

Error: TLE

Fixed submission:

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

bool dfs(string s) {
  if (s.size() == 0)
    return true;
  if (s[0] != 'm' || s[s.size() - 1] != 'w')
    return false;

  s.erase(0, 1);
  s.erase(s.size() - 1, 1);
  bool ret = false;
  REP(i, s.size()) {
    if (s[i] == 'e') {
      if (dfs(s.substr(0, i)) && dfs(s.substr(i + 1)))
        ret = true;
    }
  }
  return ret;
}

signed main(void) {
  string s;
  cin >> s;
  if (dfs(s))
    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;

int dp[501][501];
bool dfs(string &s, int from, int to) {
  if (dp[from][to] >= 0)
    return dp[from][to];
  if (s == "")
    return true;
  else if (s.size() <= 2)
    return false;
  else if (s[0] != 'm' || s[s.size() - 1] != 'w')
    return false;
  for (int i = 1; i < s.size() - 1; i++) {
    if (s[i] != 'e')
      continue;
    string X = s.substr(1, i - 1);
    string Y = s.substr(i + 1);
    Y = Y.substr(0, Y.size() - 1);
    if ((X == "" || dfs(X, from + 1, from + i - 1)) &&
        (Y == "" || dfs(Y, from + i + 1, from + s.size() - 2)))
      return dp[from][to] = true;
  }
  return dp[from][to] = false;
}

int main() {

  string S;
  cin >> S;
  memset(dp, -1, sizeof(dp));
  if (dfs(S, 0, S.size() - 1))
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

bool dfs(string &s) {
  if (s == "")
    return true;
  else if (s.size() <= 2)
    return false;
  else if (s[0] != 'm' || s[s.size() - 1] != 'w')
    return false;
  for (int i = 1; i < s.size() - 1; i++) {
    if (s[i] != 'e')
      continue;
    else if (i != 1 && (s[i - 1] != 'w' || s[1] != 'm'))
      continue;
    else if ((i + 1 != s.size() - 1) &&
             (s[i + 1] != 'm' || s[s.size() - 2] != 'w'))
      continue;
    string X = s.substr(1, i - 1);
    string Y = s.substr(i + 1);
    Y = Y.substr(0, Y.size() - 1);
    if (dfs(X) && dfs(Y))
      return true;
  }
  return false;
}

int main() {

  string S;
  cin >> S;
  if (dfs(S))
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;

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

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

const static int tx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const static int ty[] = {-1, -1, 0, 1, 1, 1, 0, -1};

static const double EPS = 1e-8;

int gPos = 0;

void parse_CAT(const string &str) {
  if (gPos >= str.size())
    return;

  if (gPos < str.size() && str[gPos++] != 'm') {
    gPos--;
    return;
  }
  parse_CAT(str);
  if (gPos < str.size() && str[gPos++] != 'e') {
    gPos--;
    return;
  }

  parse_CAT(str);
  if (gPos < str.size() && str[gPos++] != 'w') {
    gPos--;
    return;
  }

  return;
}

int main() {
  string str;
  while (cin >> str) {
    gPos = 0;
    parse_CAT(str);
    printf("%s\\n", (gPos != str.size()) ? "Rabbit" : "Cat");
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double EPS = 1e-8;

int main() {
  int agent_num;
  while (~scanf("%d", &agent_num)) {
    if (agent_num == 0)
      break;
    vector<int> agent_info[101];
    for (int i = 1; i <= agent_num; i++) {
      int node_num;
      scanf("%d", &node_num);

      int to_node;
      for (int j = 0; j < node_num; j++) {
        scanf("%d", &to_node);
        agent_info[i].push_back(to_node);
      }
    }
    int leaked_num;
    int leak[101];
    scanf("%d", &leaked_num);
    for (int j = 0; j < leaked_num; j++) {
      scanf("%d", leak + j);
    }

    int criminal_num = 0;
    int criminal_idx = 0;
    for (int i = 1; i <= agent_num; i++) {
      int count = 0;
      for (int j = 0; j < agent_info[i].size(); j++) {
        for (int k = 0; k < leaked_num; k++) {
          if (leak[k] == agent_info[i][j]) {
            count++;
            break;
          }
        }
      }
      if (count >= leaked_num) {
        criminal_idx = i;
        criminal_num++;
      }
    }

    printf("%d\\n", criminal_num != 1 ? -1 : criminal_idx);
  }
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int dp[505][505];

int check(const string &s, int l, int r) {
  // cerr << l << " " << r << endl;
  int f;
  if (l == r)
    f = 1;
  else if (dp[l][r] != -1)
    f = dp[l][r];
  else if (s[l] != 'm' || s[r - 1] != 'w')
    f = 0;
  else {
    f = 0;
    for (int i = l; i < r; i++) {
      if (s[i] == 'e' && check(s, l + 1, i) && check(s, i + 1, r - 1))
        f = 1;
    }
  }
  return dp[l][r] = f;
}

int main(void) {
  string s;
  cin >> s;
  for (int i = 0; i < 505; i++)
    for (int j = 0; j < 505; j++)
      dp[i][j] = -1;
  bool f = check(s, 0, s.length());
  // for(int i=0;i<=s.length();i++){
  //     for(int j=0;j<=s.length();j++)cerr << dp[i][j] << " ";
  //     cerr << endl;
  // }
  if (f)
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

// int dp[505][505];

bool check(const string &s, int l, int r) {
  // cerr << l << " " << r << endl;
  bool f;
  if (l == r)
    f = true;
  // else if(s[l] != 'm' || s[r-1] != 'w')f = false;
  else {
    f = false;
    for (int i = l; i < r; i++) {
      if (s[i] == 'e' && check(s, l + 1, i) && check(s, i + 1, r - 1))
        f = true;
    }
  }
  // cerr << l << " " << r << " " << f << endl;
  return f;
}

int main(void) {
  string s;
  cin >> s;
  bool f = check(s, 0, s.length());
  if (f)
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
bool iscat(string s) {
  int l = s.length();
  if (l == 0 || s == "mew")
    return true;
  if (s[0] != 'm' || s[l - 1] != 'w' || l % 3 != 0)
    return false;
  int m = 0, e = 0, w = 0;
  for (int i = 0; i < l; i++) {
    switch (s[i]) {
    case 'm':
      m++;
      break;
    case 'e':
      e++;
      break;
    case 'w':
      w++;
      break;
    }
  }
  if (m != e || e != m)
    return false;
  int id = 1;
  int n;
  while ((n = s.find('e', id)) != string::npos) {
    string s1 = s.substr(1, n - 1);
    string s2 = s.substr(n + 1, l - n - 2);
    if (iscat(s1) && iscat(s2))
      return true;
    id = n + 1;
  }
  return false;
}
int main() {
  string s;
  cin >> s;
  if (iscat(s))
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
bool iscat(string s) {
  int l = s.length();
  if (l == 0 || s == "mew")
    return true;
  if (s[0] != 'm' || s[l - 1] != 'w' || l % 3 != 0)
    return false;
  int id = 1;
  int n;
  while ((n = s.find('e', id)) != string::npos) {
    string s1 = s.substr(1, n - 1);
    string s2 = s.substr(n + 1, l - n - 2);
    if (iscat(s1) && iscat(s2))
      return true;
    id = n + 1;
  }
  return false;
}
int main() {
  string s;
  cin >> s;
  if (iscat(s))
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
  return 0;
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

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

char str[1024];
bool f[1024][1024];

int main() {
  scanf("%s", str);
  int n = strlen(str);
  for (int i = 0; i < n; i++) {
    f[i][i] = true;
  }
  for (int t = 0; t < n / 2; t++) {
    for (int w1 = 0; w1 < n; w1 += 3) {
      for (int w2 = 0; w2 < n; w2 += 3) {
        for (int i = 0; i < n; i++) {
          int j = i + w1 + 1;
          int k = i + w1 + w2 + 2;
          if (str[i] != 'm')
            continue;
          if (str[j] != 'e')
            continue;
          if (str[k] != 'w')
            continue;
          if (!f[i + 1][j])
            continue;
          if (!f[j + 1][k])
            continue;
          f[i][k + 1] = true;
        }
      }
    }
  }
  puts(f[0][n] ? "Cat" : "Rabbit");
}

Error: TLE

Fixed submission:

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

char str[1024];
bool f[1024][1024];

int main() {
  scanf("%s", str);
  int n = strlen(str);
  for (int i = 0; i < n; i++) {
    f[i][i] = true;
  }
  for (int t = 0; t < n; t++) {
    for (int w1 = 0; w1 < n; w1 += 3) {
      for (int w2 = 0; w2 < n; w2 += 3) {
        for (int i = 0; i < n; i++) {
          int j = i + w1 + 1;
          int k = i + w1 + w2 + 2;
          if (str[i] != 'm')
            continue;
          if (str[j] != 'e')
            continue;
          if (str[k] != 'w')
            continue;
          if (!f[i + 1][j])
            continue;
          if (!f[j + 1][k])
            continue;
          f[i][k + 1] = true;
        }
      }
    }
  }
  puts(f[0][n] ? "Cat" : "Rabbit");
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "algorithm"
#include "bitset"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

const long long int MOD = 1000000007;

bool Search(string &s, vector<vector<int>> &dp, int l, int r) {
  if (dp[l][r] == 1)
    return true;
  if (dp[l][r] == -1)
    return false;
  if ((r - l + 1) % 3) {
    dp[l][r] = -1;
    return false;
  }
  if (r < l) {
    dp[l][r] = 1;
    return true;
  }
  if (r - l + 1 == 3) {
    if (s[l] == 'm' && s[l + 1] == 'e' && s[r] == 'w') {
      dp[l][r] = 1;
      return true;
    }
    dp[l][r] = -1;
    return false;
  }
  if (s[l] != 'm' || s[r] != 'w') {
    dp[l][r] = -1;
    return false;
  }
  bool ret = false;
  for (int i = l + 1; i < r; i++) {
    if (s[i] == 'e') {
      bool bag = true;
      bag &= Search(s, dp, l + 1, i - 1);
      bag &= Search(s, dp, i + 1, r - 1);
      ret |= bag;
    }
  }
  if (ret)
    dp[l][r] = 1;
  else
    dp[l][r] = -1;
  return ret;
}

long long int N, M, K, H, W, L, R, V, E;

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

  string s;
  cin >> s;
  vector<vector<int>> dp(s.size(), vector<int>(s.size()));
  if (Search(s, dp, 0, s.size() - 1))
    cout << "Cat\\n";
  else
    cout << "Rabbit\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include "algorithm"
#include "bitset"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

const long long int MOD = 1000000007;

bool Search(string &s, int l, int r) {
  if ((r - l + 1) % 3)
    return false;
  if (r < l)
    return true;
  if (r - l + 1 == 3) {
    if (s[l] == 'm' && s[l + 1] == 'e' && s[r] == 'w')
      return true;
    return false;
  }
  if (s[l] != 'm' || s[r] != 'w')
    return false;
  bool ret = false;
  for (int i = l + 1; i < r; i++) {
    if (s[i] == 'e') {
      bool bag = true;
      bag &= Search(s, l + 1, i - 1);
      bag &= Search(s, i + 1, r - 1);
      ret |= bag;
    }
  }
  return ret;
}

long long int N, M, K, H, W, L, R, V, E;

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

  string s;
  cin >> s;
  if (Search(s, 0, s.size() - 1))
    cout << "Cat\\n";
  else
    cout << "Rabbit\\n";
  return 0;
}