Problem p03800 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for (auto i = (m).begin(); i != (m).end(); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1 << 30;

template <typename T> void printv(vector<T> const &s) {
  REP(i, SZ(s)) { cout << s[i] << " "; }
  cout << endl;
}

int main() {
  cin.tie(0);
  cout << setprecision(10);

  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<bool> in(n);
  REP(i, n) in[i] = (s[i] == 'o') ? 1 : 0;

  vector<vector<bool>> v(4);
  vector<bool> now(4);
  REP(i, 4) v[i].resize(n);
  v[0][0] = 0;
  v[0][1] = 0;
  now[0] = 0;
  v[1][0] = 0;
  v[1][1] = 1;
  now[1] = 1;
  v[2][0] = 1;
  v[2][1] = 0;
  now[2] = 1;
  v[3][0] = 1;
  v[3][1] = 1;
  now[3] = 0;

  REP(i, 4) {
    REP(j, n) {
      v[i][j + 2] = now[i] ^ in[j];
      now[i] = v[i][j + 1] ^ v[i][j + 2];
    }
  }

  int ans = -1;
  REP(i, 4) {
    if (v[i][0] == v[i][n] && v[i][1] == v[i][n + 1])
      ans = i;
  }

  if (ans == -1)
    cout << -1;
  else
    REP(i, n) cout << ((v[ans][(i + 1) % n]) ? 'S' : 'W');
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for (auto i = (m).begin(); i != (m).end(); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1 << 30;

template <typename T> void printv(vector<T> const &s) {
  REP(i, SZ(s)) { cout << s[i] << " "; }
  cout << endl;
}

int main() {
  cin.tie(0);
  cout << setprecision(10);

  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<bool> in(n);
  REP(i, n) in[i] = (s[i] == 'o') ? 1 : 0;

  vector<vector<bool>> v(4);
  vector<bool> now(4);
  REP(i, 4) v[i].resize(n + 2);
  v[0][0] = 0;
  v[0][1] = 0;
  now[0] = 0;
  v[1][0] = 0;
  v[1][1] = 1;
  now[1] = 1;
  v[2][0] = 1;
  v[2][1] = 0;
  now[2] = 1;
  v[3][0] = 1;
  v[3][1] = 1;
  now[3] = 0;

  REP(i, 4) {
    REP(j, n) {
      v[i][j + 2] = now[i] ^ in[j];
      now[i] = v[i][j + 1] ^ v[i][j + 2];
    }
  }

  int ans = -1;
  REP(i, 4) {
    if (v[i][0] == v[i][n] && v[i][1] == v[i][n + 1])
      ans = i;
  }

  if (ans == -1)
    cout << -1;
  else
    REP(i, n) cout << ((v[ans][(i + 1) % n]) ? 'S' : 'W');
  cout << endl;
}