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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-10;
const double PI = acos(-1.0);

map<string, bool> m;
bool dfs(string &state, int player) {
  if (EXIST(m, state)) {
    return 1 - (player ^ m[state]);
  }
  string stones = "ox";
  int winner = -1;
  REP(i, 2) {
    bool flag = true;
    REP(j, state.size()) {
      if (state[j] != stones[i]) {
        flag = false;
        break;
      }
    }
    if (flag) {
      winner = i;
      break;
    }
  }
  if (winner != -1) {
    return winner == player;
  }
  int ret = false;
  bool canmove = false;
  REP(i, 2) {
    string s(state);
    int index = i ? 0 : s.size() - 1;
    int dir = i ? 1 : -1;
    if (s[index] == stones[1 - player]) {
      canmove = true;
      while (s[index] == stones[1 - player]) {
        s[index] = ' ';
        index += dir;
      }
      string ns;
      REP(i, s.size()) {
        if (s[i] != ' ') {
          ns += s[i];
        }
      }
      int d = dfs(ns, 1 - player);
      if (!d) {
        ret = true;
      }
    }
  }
  if (!canmove) {
    string s(state);
    int d = dfs(s, 1 - player);
    if (!d) {
      ret = true;
    }
  }
  if (player == 0) {
    m[state] = !ret;
  } else {
    m[state] = ret;
  }
  return ret;
}

int main() {
  string s;
  cin >> s;
  string state;
  char prev = ' ';
  REP(i, s.size()) {
    if (s[i] != prev) {
      state += s[i];
      prev = s[i];
    }
  }
  cout << (dfs(s, 0) ? 'o' : 'x') << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-10;
const double PI = acos(-1.0);

bool dfs(string &state, int player) {
  string stones = "ox";
  int winner = -1;
  REP(i, 2) {
    bool flag = true;
    REP(j, state.size()) {
      if (state[j] != stones[i]) {
        flag = false;
        break;
      }
    }
    if (flag) {
      winner = i;
      break;
    }
  }
  if (winner != -1) {
    return winner == player;
  }
  int ret = false;
  bool canmove = false;
  REP(i, 2) {
    string s(state);
    int index = i ? 0 : s.size() - 1;
    int dir = i ? 1 : -1;
    if (s[index] == stones[1 - player]) {
      canmove = true;
      while (s[index] == stones[1 - player]) {
        s[index] = stones[player];
        index += dir;
      }
      if (i) {
        s = stones[player] + s;
      } else {
        s = s + stones[player];
      }
      int d = dfs(s, 1 - player);
      if (!d) {
        ret = true;
      }
    }
  }
  if (!canmove) {
    string s(state);
    int d = dfs(s, 1 - player);
    if (!d) {
      ret = true;
    }
  }
  return ret;
}

int main() {
  string s;
  cin >> s;
  cout << (dfs(s, 0) ? 'o' : 'x') << endl;
}