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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int dy[] = {0, 1, -1, 0};
int dx[] = {-1, 0, 0, 1};

int main() {
  int h, w;
  cin >> w >> h;
  vector<string> s(h);
  for (int i = 0; i < h; ++i)
    cin >> s[i];

  int n;
  cin >> n;
  vector<vector<int>> open(h, vector<int>(w, -1));
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    open[y][x] = i + 1;
  }

  vector<vector<bool>> check(h, vector<bool>(w, false));
  check[0][0] = true;
  vector<queue<pair<int, int>>> q(n + 1);
  q[0].push(make_pair(0, 0));

  for (int i = 0; i <= n; ++i) {
    while (!q[i].empty()) {
      int y0 = q[i].front().first;
      int x0 = q[i].front().second;
      q[i].pop();

      if (s[y0][x0] == 't') {
        cout << i << endl;
        return 0;
      }

      for (int j = 0; j < 4; ++j) {
        int t = i;
        int y = y0 + dy[j];
        int x = x0 + dx[j];
        if (y < 0 || y >= h || x < 0 || x >= w || check[y][x])
          continue;
        if (s[y][x] == '#') {
          if (open[y][x] == -1)
            continue;
          t = max(t, open[y][x]);
        }
        check[y][x] = true;
        q[t].push(make_pair(y, x));
      }
    }
  }

  cout << -1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int dy[] = {0, 1, -1, 0};
int dx[] = {-1, 0, 0, 1};

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  for (int i = 0; i < h; ++i)
    cin >> s[i];

  int n;
  cin >> n;
  vector<vector<int>> open(h, vector<int>(w, INT_MAX / 2));
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    open[y][x] = i + 1;
  }

  vector<vector<int>> dp(h, vector<int>(w, INT_MAX));
  dp[0][0] = 0;
  multimap<int, pair<int, int>> mm;
  mm.insert(make_pair(0, make_pair(0, 0)));

  for (;;) {
    int t0 = mm.begin()->first;
    int y0 = mm.begin()->second.first;
    int x0 = mm.begin()->second.second;
    mm.erase(mm.begin());
    if (t0 > dp[y0][x0])
      continue;
    if (s[y0][x0] == 't') {
      if (t0 > n)
        cout << -1 << endl;
      else
        cout << t0 << endl;
      return 0;
    }

    for (int i = 0; i < 4; ++i) {
      int t = t0;
      int y = y0 + dy[i];
      int x = x0 + dx[i];
      if (y < 0 || y >= h || x < 0 || x >= w)
        continue;
      if (s[y][x] == '#')
        t = max(t, open[y][x]);
      if (t < dp[y][x]) {
        dp[y][x] = t;
        mm.insert(make_pair(t, make_pair(y, x)));
      }
    }
  }
}
Problem p01481 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

const int xdir[4] = {0, 1, 0, -1};
const int ydir[4] = {1, 0, -1, 0};

int w, h;
bool vis[1001][1001];
int a[1001][1001];

class St {
public:
  int x, y, s;

  St() {}
  St(int _x, int _y, int _s) : x(_x), y(_y), s(_s) {}

  bool operator>(const St &st) const { return s > st.s; }

  bool operator<(const St &st) const { return s < st.s; }
};

int bfs() {
  priority_queue<St, vector<St>, greater<St>> que;
  que.push(St(0, 0, 0));
  fill(&vis[0][0], &vis[h][0], true);
  vis[0][0] = false;
  St st;
  int dy, dx;

  while (!que.empty()) {
    st = que.top();
    que.pop();

    if (a[st.y][st.x] == -2)
      return st.s;

    for (int i = 0; i < 4; ++i) {
      dy = st.y + ydir[i];
      dx = st.x + xdir[i];
      if (0 <= dx && dx < w && 0 <= dy && dy < h && vis[dy][dx] &&
          a[dy][dx] != -1) {
        vis[dy][dx] = false;
        que.push(St(dx, dy, max(st.s, a[dy][dx])));
      }
    }
  }

  return -1;
}

int main() {
  int m, x, y;
  char ch;

  while (cin >> w >> h && h + w) {
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j) {
        cin >> ch;
        if (ch == 't')
          a[i][j] = -2;
        if (ch == '.')
          a[i][j] = 0;
        if (ch == '#')
          a[i][j] = -1;
      }

    cin >> m;
    for (int i = 1; i <= m; ++i) {
      cin >> x >> y;
      if (a[y][x] == -1)
        a[y][x] = i;
    }

    cout << bfs() << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>

#define ft first
#define sd second

using namespace std;

typedef pair<int, int> P;

const int xdir[4] = {0, 1, 0, -1};
const int ydir[4] = {1, 0, -1, 0};

int w, h;
char a[1001][1001];
bool vis[1001][1001];

bool bfs(int s, int t) {
  queue<P> que;
  que.push(P(0, 0));

  if (a[t][s] == '#')
    a[t][s] = '.';
  fill(&vis[0][0], &vis[h][0], true);
  vis[0][0] = false;

  while (!que.empty()) {
    P p = que.front();
    que.pop();

    if (a[p.sd][p.ft] == 't')
      return true;
    for (int i = 0; i < 4; ++i) {
      int dx = p.ft + xdir[i], dy = p.sd + ydir[i];
      if (0 <= dx && dx < w && 0 <= dy && dy < h && a[dy][dx] != '#' &&
          vis[dy][dx]) {
        vis[dy][dx] = false;
        que.push(P(dx, dy));
      }
    }
  }

  return false;
}

int main() {
  int n, s, t, r;

  while (cin >> w >> h) {
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j)
        cin >> a[i][j];

    cin >> n;
    r = (bfs(0, 0)) ? 0 : -1;
    for (int i = 1; i <= n; ++i) {
      cin >> s >> t;
      if (r == -1 && bfs(s, t))
        r = i;
    }

    cout << r << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <queue>
#include <tuple>
using namespace std;
int X[2000][2000];
int V[2000][2000];
int H, W, K, SX, SY, X1, Y1;
char c;
int main() {
  cin >> W >> H;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c;
      if (c == '#') {
        X[i][j] = 2000;
      }
      if (c == '.') {
        X[i][j] = 0;
      }
      if (c == 't') {
        SX = i;
        SY = j;
        X[i][j] = 0;
      }
    }
  }
  cin >> K;
  for (int i = 1; i <= K; i++) {
    cin >> Y1 >> X1;
    X[X1][Y1] = i;
  }
  queue<tuple<int, int, int>> Q;
  for (int i = 0; i < 1000; i++) {
    for (int j = 0; j < 1000; j++) {
      V[i][j] = 3000;
    }
  }
  V[0][0] = 0;
  Q.push(make_tuple(0, 0, 0));
  while (!Q.empty()) {
    tuple<int, int, int> tup = Q.front();
    Q.pop();
    int a1 = get<0>(tup);
    int a2 = get<1>(tup);
    int a3 = get<2>(tup);

    if (a1 - 1 >= 0) {
      int a4 = X[a1 - 1][a2];
      a4 = max(a4, a3);
      if (V[a1 - 1][a2] > a4) {
        V[a1 - 1][a2] = a4;
        Q.push(make_tuple(a1 - 1, a2, V[a1 - 1][a2]));
      }
    }
    if (a1 + 1 < H) {
      int a4 = X[a1 + 1][a2];
      a4 = max(a4, a3);
      if (V[a1 + 1][a2] > a4) {
        V[a1 + 1][a2] = a4;
        Q.push(make_tuple(a1 + 1, a2, V[a1 + 1][a2]));
      }
    }
    if (a2 - 1 >= 0) {
      int a4 = X[a1][a2 - 1];
      a4 = max(a4, a3);
      if (V[a1][a2 - 1] > a4) {
        V[a1][a2 - 1] = a4;
        Q.push(make_tuple(a1, a2 - 1, V[a1][a2 - 1]));
      }
    }
    if (a2 + 1 < W) {
      int a4 = X[a1][a2 + 1];
      a4 = max(a4, a3);
      if (V[a1][a2 + 1] > a4) {
        V[a1][a2 + 1] = a4;
        Q.push(make_tuple(a1, a2 + 1, V[a1][a2 + 1]));
      }
    }
  }
  if (V[SX][SY] >= 2000) {
    cout << "-1" << endl;
  } else {
    cout << V[SX][SY] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
char T[1003][1003];
int V[1003][1003];
int H, W, N, SX, SY, X, Y;
queue<pair<int, int>> Q;
int OK = -1;
inline void P(int x, int y) {
  if (x <= -1 || y <= -1)
    return;
  if (V[x][y] == 0 && T[x][y] == '.') {
    Q.push(make_pair(x, y));
    V[x][y] = 1;
  }
}
inline bool bfs() {
  while (!Q.empty())
    Q.pop();
  for (int i = 0; i < 1000; i++) {
    for (int j = 0; j < 1000; j++) {
      V[i][j] = 0;
    }
  }
  Q.push(make_pair(0, 0));
  V[0][0] = 1;
  while (!Q.empty()) {
    pair<int, int> pa = Q.front();
    if (V[SX][SY] == 1)
      return true;
    Q.pop();
    P(pa.first - 1, pa.second);
    P(pa.first + 1, pa.second);
    P(pa.first, pa.second - 1);
    P(pa.first, pa.second + 1);
  }
  return false;
}
int main() {
  cin >> W >> H;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> T[i][j];
      if (T[i][j] == 't') {
        SX = i;
        SY = j;
        T[i][j] = '.';
      }
    }
  }
  if (bfs() == true) {
    OK = 0;
  }
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> X >> Y;
    T[Y][X] = '.';
    if (bfs() == true && OK == -1) {
      OK = i;
    }
  }
  cout << OK << endl;
  return 0;
}
Problem p01481 (C++) — buggy submission, verdict: Time Limit Exceeded

// 17
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <utility>

using namespace std;

char g[1000][1001];
int w, h;
set<pair<int, int>> s;

bool ff(pair<int, int> p) {
  bool r = false;
  if (s.count(p)) {
    queue<pair<int, int>> que;
    que.push(p);
    while (!que.empty()) {
      int x = que.front().first;
      int y = que.front().second;
      que.pop();
      if (x < 0 || w <= x || y < 0 || h <= y || g[y][x] == 'p') {
        continue;
      } else if (g[y][x] == '#') {
        s.insert(make_pair(x, y));
      } else {
        r |= g[y][x] == 't';
        g[y][x] = 'p';
        for (int i = 0; i < 4; i++) {
          const static int d[] = {0, 1, 0, -1, 0};
          que.push(make_pair(x + d[i], y + d[i + 1]));
        }
      }
    }
  }
  return r;
}

int main() {
  cin >> w >> h;
  for (int i = 0; i < h; i++) {
    cin >> g[i];
  }
  s.insert(make_pair(0, 0));
  int t = -1;
  if (ff(make_pair(0, 0))) {
    t = 0;
  }
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    g[y][x] = '.';
    if (ff(make_pair(x, y))) {
      t = i;
    }
  }
  cout << t << endl;
  return 0;
}

Error: TLE

Fixed submission:

// 17
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <utility>

using namespace std;

char g[1000][1001];
int w, h;
set<pair<int, int>> s;

bool ff(pair<int, int> p) {
  bool r = false;
  if (s.count(p)) {
    queue<pair<int, int>> que;
    que.push(p);
    while (!que.empty()) {
      int x = que.front().first;
      int y = que.front().second;
      que.pop();
      if (x < 0 || w <= x || y < 0 || h <= y || g[y][x] == 'p') {
        continue;
      } else if (g[y][x] == '#') {
        s.insert(make_pair(x, y));
      } else {
        r |= g[y][x] == 't';
        g[y][x] = 'p';
        for (int i = 0; i < 4; i++) {
          const static int d[] = {0, 1, 0, -1, 0};
          que.push(make_pair(x + d[i], y + d[i + 1]));
        }
      }
    }
  }
  return r;
}

int main() {
  cin >> w >> h;
  for (int i = 0; i < h; i++) {
    cin >> g[i];
  }
  s.insert(make_pair(0, 0));
  int t = -1;
  if (ff(make_pair(0, 0))) {
    t = 0;
  }
  replace(g[0], g[h], 'p', '.');
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    g[y][x] = '.';
    if (ff(make_pair(x, y))) {
      t = i;
    }
    replace(g[0], g[h], 'p', '.');
  }
  cout << t << endl;
  return 0;
}
Problem p01481 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
string map[1000];
int w, h;
vector<pair<int, int>> stack;
void f(int x, int y) {
  if (x < 0 || x >= w || y < 0 || y >= h || map[y][x] != '.')
    return;
  map[y][x] = 'o';
  stack.push_back(make_pair(x - 1, y));
  stack.push_back(make_pair(x, y - 1));
  stack.push_back(make_pair(x + 1, y));
  stack.push_back(make_pair(x, y + 1));
  return;
}
void g(int x, int y) {
  stack.push_back(make_pair(x, y));
  while (stack.size()) {
    auto a = stack.back();
    stack.pop_back();
    f(a.first, a.second);
  }
}
bool ch(int x, int y) {
  if (x < 0 || x >= w || y < 0 || y >= h)
    return false;
  return map[y][x] == 'o';
}
bool check(int x, int y) {
  return ch(x - 1, y) || ch(x, y - 1) || ch(x + 1, y) || ch(x, y + 1);
}
int main() {
  int n, tx, ty;
  cin >> w >> h;
  for (int i = 0; i < h; i++) {
    cin >> map[i];
  }
  cin >> n;
  for (int i = 0; i < h; i++) {
    size_t pos = map[i].find('t');
    if (pos == string::npos)
      continue;
    tx = pos, ty = i;
    break;
  }
  if (map[0][0] == 't') {
    cout << 0 << endl;
    return 0;
  }
  g(0, 0);
  if (check(tx, ty)) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    map[y][x] = '.';
    if (check(x, y)) {
      g(x, y);
    }
    if (check(tx, ty)) {
      cout << i + 1 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
string map[1000];
int w, h;
void f(int x, int y) {
  if (x < 0 || x >= w || y < 0 || y >= h || map[y][x] != '.')
    return;
  map[y][x] = 'o';
  f(x - 1, y);
  f(x, y - 1);
  f(x + 1, y);
  f(x, y + 1);
  return;
}
bool ch(int x, int y) {
  if (x < 0 || x >= w || y < 0 || y >= h)
    return false;
  return map[y][x] == 'o';
}
bool check(int x, int y) {
  return ch(x - 1, y) || ch(x, y - 1) || ch(x + 1, y) || ch(x, y + 1);
}
int main() {
  int n, tx, ty;
  cin >> w >> h;
  for (int i = 0; i < h; i++) {
    cin >> map[i];
  }
  cin >> n;
  for (int i = 0; i < h; i++) {
    size_t pos = map[i].find('t');
    if (pos == string::npos)
      continue;
    tx = pos, ty = i;
    break;
  }
  if (map[0][0] == 't') {
    cout << 0 << endl;
    return 0;
  }
  f(0, 0);
  if (check(tx, ty)) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    map[y][x] = '.';
    if (check(x, y)) {
      f(x, y);
    }
    if (check(tx, ty)) {
      cout << i + 1 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
Problem p01483 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const static int INF = 1 << 30;

int main() {
  int n, m, W;
  int weight[15], ptr = 0;
  int bit[10000] = {};
  int sum[1 << 15] = {}, dp[1 << 15];
  int highest = 0;
  fill_n(dp, 1 << 15, INF);

  cin >> n >> m >> W;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    if (k > 0)
      highest = i;
    for (int j = 0; j < k; j++) {
      int w;
      cin >> w;
      if (i > 0) {
        weight[ptr] = w;
        bit[i] |= 1 << ptr;
        ++ptr;
      }
    }
  }
  if (ptr == 0) {
    puts("0");
  } else {
    for (int i = 1; i < 1 << ptr; i++) {
      for (int j = 0; j < ptr; j++) {
        if ((i >> j) & 1)
          sum[i] += weight[j];
      }
    }

    dp[0] = 0;
    for (int i = 1; i < 1 << ptr; i++) {
      for (int j = i; j > 0; j = j - 1 & i) {
        if (sum[j] <= W)
          dp[i] = min(dp[i], dp[i - j] + 1);
      }
    }
    int ret = 0;
    for (int i = highest, bits = 0; i > 0; i--) {
      ret += dp[bits |= bit[i]] * 2 - 1;
    }
    cout << ret + fabs(highest - m + 1) << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> Pi;
typedef pair<int, Pi> Pii;

const static int INF = 1 << 30;

int dp[1 << 15];
int flor[15], w[15];
int ptr;
int n, m, W;
map<Pii, int> memo;

int rec(int bits, int weight, int height) {
  if (bits == 0) {
    return (height * 2);
  }

  if (memo.find(make_pair(bits, make_pair(weight, height))) != memo.end()) {
    return (memo[make_pair(bits, make_pair(weight, height))]);
  }

  int ret = INF;
  for (int i = 0; i < ptr; i++) {
    if ((bits >> i) & 1) {
      if (weight + w[i] <= W) {
        ret = min(ret,
                  rec(bits & ~(1 << i), weight + w[i], max(flor[i], height)));
      } else {
        ret = min(ret, rec(bits & ~(1 << i), w[i], flor[i]) + height * 2);
      }
    }
  }
  return (memo[make_pair(bits, make_pair(weight, height))] = ret);
}

int main() {
  cin >> n >> m >> W;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int wid;
      cin >> wid;
      if (i > 0) {
        w[ptr] = wid;
        flor[ptr++] = i;
      }
    }
  }

  // (weight[i] + W - 1) / W * idx[i] これは嘘
  // これDPしないとわかんないよ
  //  dp[S][i]: i番目まで見たときの最大の個数
  dp[0] = 0;
  for (int i = 1; i < 1 << ptr; i++) {
    int ret = 0;
    for (int j = 0; j < ptr; j++) {
      if ((i >> j) & 1)
        ret = max(ret, flor[j]);
    }
    dp[i] = max(0, ret - m + 1) + rec(i, 0, 0) - ret;
  }

  // エレベータは 1階にあることを仮定して DP
  for (int i = 1; i < 1 << ptr; i++) {
    for (int j = 0; j < 1 << ptr; j++) {
      dp[i | j] = min(dp[i | j], dp[i] + rec(j, 0, 0));
    }
  }
  cout << dp[(1 << ptr) - 1] << endl;
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long int ll;
typedef double D;     // ??§?¨?????????????double???long double?????????
typedef complex<D> P; // Point
typedef pair<P, P> L; // Line
typedef vector<P> VP;

const int INF = (1 << 30) - 1;
const D EPS = 1e-9;

#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

#define NOT_YET -1
#define CAT 1
#define RABBIT 0

int memo[500][500 + 1];

bool is_cat(string &s, int b, int e) {
  if (memo[b][e] != NOT_YET)
    return memo[b][e];

  if (e - b == 0)
    return true;

  if (s[b] == 'm' && s[e - 1] == 'w') {
    bool ret = false;
    for (int i = b; i < e; i++) {
      if (s[i] == 'e')
        ret |= is_cat(s, b + 1, i) && is_cat(s, i + 1, e - 1);
    }
    return memo[b][e] = ret;
  } else {
    return memo[b][e] = false;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  memset(memo, -1, sizeof(memo));

  string s;
  cin >> s;
  cout << (is_cat(s, 0, s.size()) ? "Cat" : "Rabbit") << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long int ll;
typedef double D;     // ??§?¨?????????????double???long double?????????
typedef complex<D> P; // Point
typedef pair<P, P> L; // Line
typedef vector<P> VP;

const int INF = (1 << 30) - 1;
const D EPS = 1e-9;

#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

bool is_cat(string &s, int b, int e) {
  if (e - b == 0)
    return true;

  if (s[b] == 'm' && s[e - 1] == 'w') {
    bool ret = false;
    for (int i = b; i < e; i++) {
      if (s[i] == 'e')
        ret |= is_cat(s, b + 1, i) && is_cat(s, i + 1, e - 1);
    }
    return ret;
  } else {
    return false;
  }
}

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

  string s;
  cin >> s;
  cout << (is_cat(s, 0, s.size()) ? "Cat" : "Rabbit") << endl;

  return 0;
}
Problem p01486 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

  stack<char> stk;
  int cnt = 0;
  REP(i, SZ(s)) {
    if (s[i] == 'm') {
      if (stk.empty())
        cnt++;
      stk.push(s[i]);
    } else if (s[i] == 'e') {
      stk.push(s[i]);
    } else if (s[i] == 'w') {
      if (stk.empty()) {
        cout << "Rabbit" << endl;
        goto END;
      }
      char c2 = stk.top();
      stk.pop();
      if (stk.empty()) {
        cout << "Rabbit" << endl;
        goto END;
      }
      char c1 = stk.top();
      stk.pop();
      if (c1 == 'm' && c2 == 'e') {
      } else {
        cout << "Rabbit" << endl;
        goto END;
      }
    }
  }
  if (stk.empty() && cnt == 1) {
    cout << "Cat" << endl;
  } else {
    cout << "Rabbit" << endl;
  }
END : {}
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

  stack<char> stk;
  int cnt = 0;
  REP(i, SZ(s)) {
    if (s[i] == 'm') {
      if (stk.empty())
        cnt++;
      stk.push(s[i]);
    } else if (s[i] == 'e') {
      stk.push(s[i]);
    } else if (s[i] == 'w') {
      char c2 = stk.top();
      stk.pop();
      char c1 = stk.top();
      stk.pop();
      if (c1 == 'm' && c2 == 'e') {
      } else {
        cout << "Rabbit" << endl;
        goto END;
      }
    }
  }
  if (stk.empty() && cnt == 1) {
    cout << "Cat" << endl;
  } else {
    cout << "Rabbit" << endl;
  }
END : {}
}
Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  bool iscat = true;
  for (int i = 0; i < (int)str.length() - 1; i++) {
    if ((str[i] == 'm' && str[i + 1] == 'w') ||
        (str[i] == 'e' && str[i + 1] == 'e') ||
        (str[i] == 'w' && str[i + 1] == 'm')) {
      iscat = false;
      break;
    }
  }

  int cm = 0, ce = 0, cw = 0;
  for (int i = 0; i < (int)str.length(); i++) {
    if (str[i] == 'm')
      cm++;
    if (str[i] == 'e')
      ce++;
    if (str[i] == 'w')
      cw++;
    if (cm < ce || cm < cw || ce < cw) {
      iscat = false;
      break;
    }
  }
  if (cm != ce || cm != cw) {
    iscat = false;
  }

  if (iscat) {
    cout << "Cat" << endl;
  } else {
    cout << "Rabbit" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

bool isCat(string s) {
  int len = s.length();
  if (len == 0)
    return true;
  if (len % 3 != 0)
    return false;
  if (s[0] != 'm' || s[len - 1] != 'w')
    return false;
  for (int i = 1; i < (int)s.length(); i += 3) {
    if (s[i] == 'e') {
      if (isCat(s.substr(1, i - 1)) && isCat(s.substr(i + 1, len - i - 2))) {
        return true;
      }
    }
  }
  return false;
}

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

  if (isCat(str)) {
    cout << "Cat" << endl;
  } else {
    cout << "Rabbit" << endl;
  }
  return 0;
}