Problem p03800 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
#define REP(i, n) for (int i = 0; i < n; i++)
const long long INF = 1LL << 60;

int MOD = 1000000007;

string g(std::vector<bool> v) {
  string toret = "";
  REP(i, v.size()) {
    if (v[i])
      toret += "S";
    else
      toret += "W";
  }
  return toret;
}

int main() {
  std::ios_base::sync_with_stdio(false);

  int n;
  string s;
  cin >> n >> s;
  std::vector<bool> sheep(63853, false);

  REP(i, n) sheep[i] = false;
  sheep[0] = true;
  sheep[1] = true;
  bool ans;
  if (s[0] == 'o')
    ans = true;
  else
    ans = false;

  for (int i = 2; i < n; ++i) {
    if (s[i - 1] == 'o' && sheep[i - 1]) {
      sheep[i] = sheep[i - 2];
    } else if (s[i - 1] == 'x' && sheep[i - 1]) {
      sheep[i] = !sheep[i - 2];
    } else if (s[i - 1] == 'o' && !sheep[i - 1]) {
      sheep[i] = !sheep[i - 2];
    } else {
      sheep[i] = sheep[i - 2];
    }
  }

  if (ans == sheep[n - 1]) {
    if (sheep[n - 1] && s[n - 1] == 'o' && (sheep[0] == sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (sheep[n - 1] && s[n - 1] == 'x' && (sheep[0] ^ sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (!sheep[n - 1] && s[n - 1] == 'o' && (sheep[0] ^ sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (!sheep[n - 1] && s[n - 1] == 'x' && (sheep[0] == sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }
  }

  REP(i, n) sheep[i] = false;
  sheep[0] = true;
  sheep[1] = false;
  if (s[0] == 'o')
    ans = false;
  else
    ans = true;

  for (int i = 2; i < n; ++i) {
    if (s[i - 1] == 'o' && sheep[i - 1]) {
      sheep[i] = sheep[i - 2];
    } else if (s[i - 1] == 'x' && sheep[i - 1]) {
      sheep[i] = !sheep[i - 2];
    } else if (s[i - 1] == 'o' && !sheep[i - 1]) {
      sheep[i] = !sheep[i - 2];
    } else {
      sheep[i] = sheep[i - 2];
    }
  }

  if (ans == sheep[n - 1]) {
    if (sheep[n - 1] && s[n - 1] == 'o' && (sheep[0] == sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (sheep[n - 1] && s[n - 1] == 'x' && (sheep[0] ^ sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (!sheep[n - 1] && s[n - 1] == 'o' && (sheep[0] ^ sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (!sheep[n - 1] && s[n - 1] == 'x' && (sheep[0] == sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }
  }

  REP(i, n) sheep[i] = false;
  sheep[0] = false;
  sheep[1] = true;
  if (s[0] == 'o')
    ans = false;
  else
    ans = true;

  for (int i = 2; i < n; ++i) {
    if (s[i - 1] == 'o' && sheep[i - 1]) {
      sheep[i] = sheep[i - 2];
    } else if (s[i - 1] == 'x' && sheep[i - 1]) {
      sheep[i] = !sheep[i - 2];
    } else if (s[i - 1] == 'o' && !sheep[i - 1]) {
      sheep[i] = !sheep[i - 2];
    } else {
      sheep[i] = sheep[i - 2];
    }
  }

  if (ans == sheep[n - 1]) {
    if (sheep[n - 1] && s[n - 1] == 'o' && (sheep[0] == sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (sheep[n - 1] && s[n - 1] == 'x' && (sheep[0] ^ sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (!sheep[n - 1] && s[n - 1] == 'o' && (sheep[0] ^ sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (!sheep[n - 1] && s[n - 1] == 'x' && (sheep[0] == sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }
  }

  REP(i, n) sheep[i] = false;
  sheep[0] = false;
  sheep[1] = false;
  if (s[0] == 'o')
    ans = true;
  else
    ans = false;

  for (int i = 2; i < n; ++i) {
    if (s[i - 1] == 'o' && sheep[i - 1]) {
      sheep[i] = sheep[i - 2];
    } else if (s[i - 1] == 'x' && sheep[i - 1]) {
      sheep[i] = !sheep[i - 2];
    } else if (s[i - 1] == 'o' && !sheep[i - 1]) {
      sheep[i] = !sheep[i - 2];
    } else {
      sheep[i] = sheep[i - 2];
    }
  }

  if (ans == sheep[n - 1]) {
    if (sheep[n - 1] && s[n - 1] == 'o' && (sheep[0] == sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (sheep[n - 1] && s[n - 1] == 'x' && (sheep[0] ^ sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (!sheep[n - 1] && s[n - 1] == 'o' && (sheep[0] ^ sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (!sheep[n - 1] && s[n - 1] == 'x' && (sheep[0] == sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }
  }

  cout << -1 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
#define REP(i, n) for (int i = 0; i < n; i++)
const long long INF = 1LL << 60;

int MOD = 1000000007;

string g(std::vector<bool> v) {
  string toret = "";
  REP(i, v.size()) {
    if (v[i])
      toret += "S";
    else
      toret += "W";
  }
  return toret;
}

int main() {
  std::ios_base::sync_with_stdio(false);

  int n;
  string s;
  cin >> n >> s;
  std::vector<bool> sheep(n, false);

  REP(i, n) sheep[i] = false;
  sheep[0] = true;
  sheep[1] = true;
  bool ans;
  if (s[0] == 'o')
    ans = true;
  else
    ans = false;

  for (int i = 2; i < n; ++i) {
    if (s[i - 1] == 'o' && sheep[i - 1]) {
      sheep[i] = sheep[i - 2];
    } else if (s[i - 1] == 'x' && sheep[i - 1]) {
      sheep[i] = !sheep[i - 2];
    } else if (s[i - 1] == 'o' && !sheep[i - 1]) {
      sheep[i] = !sheep[i - 2];
    } else {
      sheep[i] = sheep[i - 2];
    }
  }

  if (ans == sheep[n - 1]) {
    if (sheep[n - 1] && s[n - 1] == 'o' && (sheep[0] == sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (sheep[n - 1] && s[n - 1] == 'x' && (sheep[0] ^ sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (!sheep[n - 1] && s[n - 1] == 'o' && (sheep[0] ^ sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (!sheep[n - 1] && s[n - 1] == 'x' && (sheep[0] == sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }
  }

  REP(i, n) sheep[i] = false;
  sheep[0] = true;
  sheep[1] = false;
  if (s[0] == 'o')
    ans = false;
  else
    ans = true;

  for (int i = 2; i < n; ++i) {
    if (s[i - 1] == 'o' && sheep[i - 1]) {
      sheep[i] = sheep[i - 2];
    } else if (s[i - 1] == 'x' && sheep[i - 1]) {
      sheep[i] = !sheep[i - 2];
    } else if (s[i - 1] == 'o' && !sheep[i - 1]) {
      sheep[i] = !sheep[i - 2];
    } else {
      sheep[i] = sheep[i - 2];
    }
  }

  if (ans == sheep[n - 1]) {
    if (sheep[n - 1] && s[n - 1] == 'o' && (sheep[0] == sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (sheep[n - 1] && s[n - 1] == 'x' && (sheep[0] ^ sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (!sheep[n - 1] && s[n - 1] == 'o' && (sheep[0] ^ sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (!sheep[n - 1] && s[n - 1] == 'x' && (sheep[0] == sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }
  }

  REP(i, n) sheep[i] = false;
  sheep[0] = false;
  sheep[1] = true;
  if (s[0] == 'o')
    ans = false;
  else
    ans = true;

  for (int i = 2; i < n; ++i) {
    if (s[i - 1] == 'o' && sheep[i - 1]) {
      sheep[i] = sheep[i - 2];
    } else if (s[i - 1] == 'x' && sheep[i - 1]) {
      sheep[i] = !sheep[i - 2];
    } else if (s[i - 1] == 'o' && !sheep[i - 1]) {
      sheep[i] = !sheep[i - 2];
    } else {
      sheep[i] = sheep[i - 2];
    }
  }

  if (ans == sheep[n - 1]) {
    if (sheep[n - 1] && s[n - 1] == 'o' && (sheep[0] == sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (sheep[n - 1] && s[n - 1] == 'x' && (sheep[0] ^ sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (!sheep[n - 1] && s[n - 1] == 'o' && (sheep[0] ^ sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (!sheep[n - 1] && s[n - 1] == 'x' && (sheep[0] == sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }
  }

  REP(i, n) sheep[i] = false;
  sheep[0] = false;
  sheep[1] = false;
  if (s[0] == 'o')
    ans = true;
  else
    ans = false;

  for (int i = 2; i < n; ++i) {
    if (s[i - 1] == 'o' && sheep[i - 1]) {
      sheep[i] = sheep[i - 2];
    } else if (s[i - 1] == 'x' && sheep[i - 1]) {
      sheep[i] = !sheep[i - 2];
    } else if (s[i - 1] == 'o' && !sheep[i - 1]) {
      sheep[i] = !sheep[i - 2];
    } else {
      sheep[i] = sheep[i - 2];
    }
  }

  if (ans == sheep[n - 1]) {
    if (sheep[n - 1] && s[n - 1] == 'o' && (sheep[0] == sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (sheep[n - 1] && s[n - 1] == 'x' && (sheep[0] ^ sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (!sheep[n - 1] && s[n - 1] == 'o' && (sheep[0] ^ sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }

    if (!sheep[n - 1] && s[n - 1] == 'x' && (sheep[0] == sheep[n - 2])) {
      cout << g(sheep) << endl;
      return 0;
    }
  }

  cout << -1 << endl;
  return 0;
}