Problem p03800 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>

#define prev aewrxew
#define next fawexsr
using namespace std;
typedef long long ll;
typedef long double ld;
const ll inf = numeric_limits<ll>::max() / 2 - 1;
const ld eps = 1e-7;
const ll mod = 1e9 + 7;
const ll maxn = 2456;
ll n;
string s;

char pr(char c) {
  if (c == 'S')
    return 'W';
  else
    return 'S';
}

void test(string ans) {
  for (int i = 1; i < n; ++i) {
    if (ans[i] == 'S') {
      if (s[i] == 'o') {
        ans.push_back(ans[i - 1]);
      } else {
        ans.push_back(pr(ans[i - 1]));
      }
    } else {
      if (s[i] == 'o') {
        ans.push_back(pr(ans[i - 1]));
      } else {
        ans.push_back(ans[i - 1]);
      }
    }
  }
  if (ans[0] != ans[n]) {
    return;
  }
  ans.pop_back();
  if (ans[0] == 'S') {
    if (s[0] == 'o' && ans[1] != ans[n - 1])
      return;
    if (s[0] == 'x' && ans[1] == ans[n - 1])
      return;
  } else {
    if (s[0] == 'x' && ans[1] != ans[n - 1])
      return;
    if (s[0] == 'o' && ans[1] == ans[n - 1])
      return;
  }
  if (ans[1] == 'S') {
    if (s[1] == 'o' && ans[0] != ans[2])
      return;
    if (s[1] == 'x' && ans[0] == ans[2])
      return;
  } else {
    if (s[1] == 'x' && ans[0] != ans[2])
      return;
    if (s[1] == 'o' && ans[0] == ans[2])
      return;
  }
  cout << ans;
  exit(1);
}

signed run() {
  cin >> n;
  cin >> s;
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 2; ++j) {
      string ans = "";
      if (i)
        ans.push_back('S');
      else
        ans.push_back('W');
      if (j)
        ans.push_back('S');
      else
        ans.push_back('W');
      test(ans);
    }
  }
  cout << -1;
  return 0;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#else
#define TASK ""
  if (strlen(TASK) > 0) {
    freopen(TASK ".in", "r", stdin);
    freopen(TASK ".out", "w", stdout);
  }
#endif
  run();
  cout.flush();
  cerr << "time: " << clock() * 1000 / CLOCKS_PER_SEC << endl;
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>

#define prev aewrxew
#define next fawexsr
using namespace std;
typedef long long ll;
typedef long double ld;
const ll inf = numeric_limits<ll>::max() / 2 - 1;
const ld eps = 1e-7;
const ll mod = 1e9 + 7;
const ll maxn = 2456;
ll n;
string s;

char pr(char c) {
  if (c == 'S')
    return 'W';
  else
    return 'S';
}

void test(string ans) {
  for (int i = 1; i < n; ++i) {
    if (ans[i] == 'S') {
      if (s[i] == 'o') {
        ans.push_back(ans[i - 1]);
      } else {
        ans.push_back(pr(ans[i - 1]));
      }
    } else {
      if (s[i] == 'o') {
        ans.push_back(pr(ans[i - 1]));
      } else {
        ans.push_back(ans[i - 1]);
      }
    }
  }
  if (ans[0] != ans[n]) {
    return;
  }
  ans.pop_back();
  if (ans[0] == 'S') {
    if (s[0] == 'o' && ans[1] != ans[n - 1])
      return;
    if (s[0] == 'x' && ans[1] == ans[n - 1])
      return;
  } else {
    if (s[0] == 'x' && ans[1] != ans[n - 1])
      return;
    if (s[0] == 'o' && ans[1] == ans[n - 1])
      return;
  }
  if (ans[1] == 'S') {
    if (s[1] == 'o' && ans[0] != ans[2])
      return;
    if (s[1] == 'x' && ans[0] == ans[2])
      return;
  } else {
    if (s[1] == 'x' && ans[0] != ans[2])
      return;
    if (s[1] == 'o' && ans[0] == ans[2])
      return;
  }
  cout << ans;
  exit(0);
}

signed run() {
  cin >> n;
  cin >> s;
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 2; ++j) {
      string ans = "";
      if (i)
        ans.push_back('S');
      else
        ans.push_back('W');
      if (j)
        ans.push_back('S');
      else
        ans.push_back('W');
      test(ans);
    }
  }
  cout << -1;
  return 0;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#else
#define TASK ""
  if (strlen(TASK) > 0) {
    freopen(TASK ".in", "r", stdin);
    freopen(TASK ".out", "w", stdout);
  }
#endif
  run();
  cout.flush();
  cerr << "time: " << clock() * 1000 / CLOCKS_PER_SEC << endl;
  return 0;
}