Problem p01486 (C++) — buggy submission, verdict: Time Limit Exceeded

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int arr[3][501];
string str;
bool check(int s, int t) {
  if (s == t)
    return true;
  if (str[s] != 'm' || str[t - 1] != 'w')
    return false;
  for (int i = s + 1; i < t - 1; ++i) {
    if (str[i] != 'e')
      continue;
    int j;
    for (j = 0; j < 2; ++j)
      if (arr[j][i - 1] - arr[j][s] != arr[j + 1][i - 1] - arr[j + 1][s])
        break;
    if (j < 2)
      continue;
    for (j = 0; j < 2; ++j)
      if (arr[j][t - 2] - arr[j][i] != arr[j + 1][t - 2] - arr[j + 1][i])
        break;
    if (j < 2)
      continue;

    if (check(s + 1, i) && check(i + 1, t - 1))
      return true;
  }
  return false;
}

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

  cin >> str;
  REP(i, SZ(str)) {
    if (i > 0)
      REP(j, 3) arr[j][i] = arr[j][i - 1];
    if (str[i] == 'm')
      arr[0][i]++;
    else if (str[i] == 'e')
      arr[1][i]++;
    else if (str[i] == 'w')
      arr[2][i]++;
  }
  cout << (check(0, SZ(str)) ? "Cat" : "Rabbit") << endl;

  return 0;
}

Error: TLE

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

string str;
bool check(int s, int t) {
  if (s == t)
    return true;
  if (str[s] != 'm' || str[t - 1] != 'w')
    return false;
  for (int i = s + 1; i < t - 1; ++i)
    if (str[i] == 'e' && check(s + 1, i) && check(i + 1, t - 1))
      return true;
  return false;
}

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

  cin >> str;
  cout << (check(0, SZ(str)) ? "Cat" : "Rabbit") << endl;

  return 0;
}