Problem p03800 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef tuple<int, int> tii;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(), (a).end()
#define FST first
#define SEC second
const int INF = (INT_MAX / 2);
const ll LLINF = (LLONG_MAX / 2);
const double eps = 1e-8;
const double PI = M_PI;
#define DEB cerr << "!" << endl
#define SHOW(a, b) cerr << (a) << " " << (b) << endl
#define SHOWARRAY(ar, i, j)                                                    \
  REP(a, i)                                                                    \
      REP(b, j) cerr << ar[a][b]                                               \
                     << ((b == j - 1) ? ((a == i - 1) ? ("\n\n") : ("\n"))     \
                                      : (" "))
#define DIV int(1e9 + 7)
inline ll pow(ll x, ll n, ll m) {
  ll r = 1;
  while (n > 0) {
    if ((n & 1) == 1)
      r = r * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return r % m;
}
inline ll lcm(ll d1, ll d2) { return d1 / __gcd(d1, d2) * d2; }

/* Coding Space*/
int n;
string str;

void dfs(string s) {
  while (1) {
    int ss = s.size();
    char c = s[ss - 1];
    char cb = s[ss - 2];
    if (ss == n) {
      if ((s[ss - 1] == s[1] && ((s[0] == 'S' && str[0] == 'o') ||
                                 (s[0] == 'W' && str[0] == 'x'))) ||
          (s[ss - 1] != s[1] && ((s[0] == 'W' && str[0] == 'o') ||
                                 (s[0] == 'S' && str[0] == 'x')))) {
        if (cb != s[0]) {
          if ((str[ss - 1] == 'o' && c == 'W') ||
              (str[ss - 1] == 'x' && c == 'S')) {
            cout << s << endl;

            exit(0);
            // return;
          } else
            return;
        } else {
          if ((str[ss - 1] == 'o' && c == 'S') ||
              (str[ss - 1] == 'x' && c == 'W')) {
            cout << s << endl;
            exit(0);
            // return;
          } else
            return;
        }
      } else
        return;
    }
    if (c == 'S' && cb == 'W')
      (str[ss - 1] == 'o') ? s = s + "W" : s = s + "S";
    if (c == 'S' && cb == 'S')
      (str[ss - 1] == 'o') ? s = s + "S" : s = s + "W";
    if (c == 'W' && cb == 'S')
      (str[ss - 1] == 'o') ? s = s + "W" : s = s + "S";
    if (c == 'W' && cb == 'W')
      (str[ss - 1] == 'o') ? s = s + "S" : s = s + "W";
  }
  return;
}

int main() {
  cin >> n >> str;
  dfs("SS");
  dfs("SW");
  dfs("WS");
  dfs("WW");
  cout << -1 << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef tuple<int, int> tii;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(), (a).end()
#define FST first
#define SEC second
const int INF = (INT_MAX / 2);
const ll LLINF = (LLONG_MAX / 2);
const double eps = 1e-8;
const double PI = M_PI;
#define DEB cerr << "!" << endl
#define SHOW(a, b) cerr << (a) << " " << (b) << endl
#define SHOWARRAY(ar, i, j)                                                    \
  REP(a, i)                                                                    \
      REP(b, j) cerr << ar[a][b]                                               \
                     << ((b == j - 1) ? ((a == i - 1) ? ("\n\n") : ("\n"))     \
                                      : (" "))
#define DIV int(1e9 + 7)
inline ll pow(ll x, ll n, ll m) {
  ll r = 1;
  while (n > 0) {
    if ((n & 1) == 1)
      r = r * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return r % m;
}
inline ll lcm(ll d1, ll d2) { return d1 / __gcd(d1, d2) * d2; }

/* Coding Space*/
int n;
string str;

void dfs(string s) {
  while (1) {
    int ss = s.size();
    char c = s[ss - 1];
    char cb = s[ss - 2];
    if (ss == n) {
      if ((s[ss - 1] == s[1] && ((s[0] == 'S' && str[0] == 'o') ||
                                 (s[0] == 'W' && str[0] == 'x'))) ||
          (s[ss - 1] != s[1] && ((s[0] == 'W' && str[0] == 'o') ||
                                 (s[0] == 'S' && str[0] == 'x')))) {
        if (cb != s[0]) {
          if ((str[ss - 1] == 'o' && c == 'W') ||
              (str[ss - 1] == 'x' && c == 'S')) {
            cout << s << endl;

            exit(0);
            // return;
          } else
            return;
        } else {
          if ((str[ss - 1] == 'o' && c == 'S') ||
              (str[ss - 1] == 'x' && c == 'W')) {
            cout << s << endl;
            exit(0);
            // return;
          } else
            return;
        }
      } else
        return;
    }
    if (c == 'S' && cb == 'W')
      s.pb((str[ss - 1] == 'o') ? 'W' : 'S');
    else if (c == 'S' && cb == 'S')
      s.pb((str[ss - 1] == 'o') ? 'S' : 'W');
    else if (c == 'W' && cb == 'S')
      s.pb((str[ss - 1] == 'o') ? 'W' : 'S');
    else if (c == 'W' && cb == 'W')
      s.pb((str[ss - 1] == 'o') ? 'S' : 'W');
  }
  return;
}

int main() {
  cin >> n >> str;
  dfs("SS");
  dfs("SW");
  dfs("WS");
  dfs("WW");
  cout << -1 << endl;
}