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

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

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP(x) cerr << #x << " = " << (x) << endl

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

map<string, bool> memo;

bool is_cat(string s) {
  if (memo.count(s))
    return memo[s];
  if (!s.size())
    return true;
  if (s.size() < 3)
    return false;
  if (s[0] != 'm' || s.back() != 'w')
    return false;

  int n = s.size();

  for (int i = 1; i < n - 1; i++) {
    if (s[i] != 'e')
      continue;

    string x = s.substr(1, i - 1);
    string y = s.substr(i + 1, n - (i + 2));

    //		printf("%s => m + %s + e + %s + w\\nm+%s+e+%s+w;\\n\\n", s.c_str(),
    //x.c_str(), y.c_str(), 				x.c_str(), y.c_str());

    if (is_cat(x) && is_cat(y)) {
      return memo[s] = true;
    }
  }

  return memo[s] = false;
}

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

  cout << (is_cat(s) ? "Cat" : "Rabbit") << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP(x) cerr << #x << " = " << (x) << endl

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

map<string, bool> memo;

bool is_cat(string s) {
  if (!s.size())
    return true;
  if (s.size() < 3)
    return false;
  if (s[0] != 'm' || s.back() != 'w')
    return false;

  int n = s.size();

  for (int i = 1; i < n - 1; i++) {
    if (s[i] != 'e')
      continue;

    string x = s.substr(1, i - 1);
    string y = s.substr(i + 1, n - (i + 2));

    //		printf("%s => m + %s + e + %s + w\\nm+%s+e+%s+w;\\n\\n", s.c_str(),
    //x.c_str(), y.c_str(), 				x.c_str(), y.c_str());

    if (is_cat(x) && is_cat(y)) {
      return true;
    }
  }

  return false;
}

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

  cout << (is_cat(s) ? "Cat" : "Rabbit") << endl;

  return 0;
}