Problem p03800 (C++)

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

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                                                                \
  cerr << #v ":[ ";                                                            \
  for (auto macro_vi : v) {                                                    \
    cerr << macro_vi << " ";                                                   \
  }                                                                            \
  cerr << "]" << endl;
#define ddump(v)                                                               \
  cerr << #v ":" << endl;                                                      \
  for (auto macro_row : v) {                                                   \
    cerr << "[";                                                               \
    for (auto macro__vi : macro_row) {                                         \
      cerr << macro__vi << " ";                                                \
    }                                                                          \
    cerr << "]" << endl;                                                       \
  }
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const lint MOD = 1e9 + 7;
const double EPS = 1e-10;

int main() {
  int N;
  string S;
  cin >> N >> S;
  S.push_back(S[0]);
  vector<char> C1{'S', 'W'}, C2{'S', 'W'};
  for (auto c1 : C1) {
    for (auto c2 : C2) {
      dd(c1, c2);
      string ans('X', N);
      ans[0] = c1;
      ans[1] = c2;
      for (int i = 1; i < N - 1; i++) {
        if (ans[i - 1] == 'S' and ans[i] == 'S') {
          if (S[i - 1] == 'o' and S[i] == 'o') {
            ans[i + 1] = 'S';
          } else if (S[i - 1] == 'o' and S[i] == 'x') {
            ans[i + 1] = 'W';
          } else if (S[i - 1] == 'x' and S[i] == 'o') {
            ans[i + 1] = 'S';
          } else if (S[i - 1] == 'x' and S[i] == 'x') {
            ans[i + 1] = 'W';
          }
        } else if (ans[i - 1] == 'S' and ans[i] == 'W') {
          if (S[i - 1] == 'o' and S[i] == 'o') {
            ans[i + 1] = 'W';
          } else if (S[i - 1] == 'o' and S[i] == 'x') {
            ans[i + 1] = 'S';
          } else if (S[i - 1] == 'x' and S[i] == 'o') {
            ans[i + 1] = 'W';
          } else if (S[i - 1] == 'x' and S[i] == 'x') {
            ans[i + 1] = 'S';
          }
        } else if (ans[i - 1] == 'W' and ans[i] == 'S') {
          if (S[i - 1] == 'o' and S[i] == 'o') {
            ans[i + 1] = 'W';
          } else if (S[i - 1] == 'o' and S[i] == 'x') {
            ans[i + 1] = 'S';
          } else if (S[i - 1] == 'x' and S[i] == 'o') {
            ans[i + 1] = 'W';
          } else if (S[i - 1] == 'x' and S[i] == 'x') {
            ans[i + 1] = 'S';
          }
        } else if (ans[i - 1] == 'W' and ans[i] == 'W') {
          if (S[i - 1] == 'o' and S[i] == 'o') {
            ans[i + 1] = 'S';
          } else if (S[i - 1] == 'o' and S[i] == 'x') {
            ans[i + 1] = 'W';
          } else if (S[i - 1] == 'x' and S[i] == 'o') {
            ans[i + 1] = 'S';
          } else if (S[i - 1] == 'x' and S[i] == 'x') {
            ans[i + 1] = 'W';
          }
        }
      }
      int ok = true;
      if (ans[0] == 'S') {
        if (S[0] == 'o') {
          if (not((ans[1] == 'S' and ans[N - 1] == 'S') or
                  (ans[1] == 'W' and ans[N - 1] == 'W'))) {
            ok = false;
          }
        } else {
          if (not((ans[1] == 'S' and ans[N - 1] == 'W') or
                  (ans[1] == 'W' and ans[N - 1] == 'S'))) {
            ok = false;
          }
        }
      } else {
        if (S[0] == 'o') {
          if (not((ans[1] == 'S' and ans[N - 1] == 'W') or
                  (ans[1] == 'W' and ans[N - 1] == 'S'))) {
            ok = false;
          }
        } else {
          if (not((ans[1] == 'S' and ans[N - 1] == 'S') or
                  (ans[1] == 'W' and ans[N - 1] == 'W'))) {
            ok = false;
          }
        }
      }
      for (int i = 1; i < N - 1; i++) {
        if (ans[i] == 'S') {
          if (S[i] == 'o') {
            if (not((ans[i + 1] == 'S' and ans[i - 1] == 'S') or
                    (ans[i + 1] == 'W' and ans[i - 1] == 'W'))) {
              ok = false;
            }
          } else {
            if (not((ans[i + 1] == 'S' and ans[i - 1] == 'W') or
                    (ans[i + 1] == 'W' and ans[i - 1] == 'S'))) {
              ok = false;
            }
          }
        } else {
          if (S[i] == 'o') {
            if (not((ans[i + 1] == 'S' and ans[i - 1] == 'W') or
                    (ans[i + 1] == 'W' and ans[i - 1] == 'S'))) {
              ok = false;
            }
          } else {
            if (not((ans[i + 1] == 'S' and ans[i - 1] == 'S') or
                    (ans[i + 1] == 'W' and ans[i - 1] == 'W'))) {
              ok = false;
            }
          }
        }
      }
      if (ans[N - 1] == 'S') {
        if (S[N - 1] == 'o') {
          if (not((ans[0] == 'S' and ans[N - 2] == 'S') or
                  (ans[0] == 'W' and ans[N - 2] == 'W'))) {
            ok = false;
          }
        } else {
          if (not((ans[0] == 'S' and ans[N - 2] == 'W') or
                  (ans[0] == 'W' and ans[N - 2] == 'S'))) {
            ok = false;
          }
        }
      } else {
        if (S[N - 1] == 'o') {
          if (not((ans[0] == 'S' and ans[N - 2] == 'W') or
                  (ans[0] == 'W' and ans[N - 2] == 'S'))) {
            ok = false;
          }
        } else {
          if (not((ans[0] == 'S' and ans[N - 2] == 'S') or
                  (ans[0] == 'W' and ans[N - 2] == 'W'))) {
            ok = false;
          }
        }
      }
      // d(ans);
      if (ok) {
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << -1 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                                                                \
  cerr << #v ":[ ";                                                            \
  for (auto macro_vi : v) {                                                    \
    cerr << macro_vi << " ";                                                   \
  }                                                                            \
  cerr << "]" << endl;
#define ddump(v)                                                               \
  cerr << #v ":" << endl;                                                      \
  for (auto macro_row : v) {                                                   \
    cerr << "[";                                                               \
    for (auto macro__vi : macro_row) {                                         \
      cerr << macro__vi << " ";                                                \
    }                                                                          \
    cerr << "]" << endl;                                                       \
  }
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const lint MOD = 1e9 + 7;
const double EPS = 1e-10;

int main() {
  int N;
  string S;
  cin >> N >> S;
  S.push_back(S[0]);
  vector<char> C1{'S', 'W'}, C2{'S', 'W'};
  for (auto c1 : C1) {
    for (auto c2 : C2) {
      // dd(c1, c2);
      string ans = "";
      rep(i, N) { ans += 'X'; }
      // d(ans);
      ans[0] = c1;
      ans[1] = c2;
      for (int i = 1; i < N - 1; i++) {
        if (ans[i - 1] == 'S' and ans[i] == 'S') {
          if (S[i - 1] == 'o' and S[i] == 'o') {
            ans[i + 1] = 'S';
          } else if (S[i - 1] == 'o' and S[i] == 'x') {
            ans[i + 1] = 'W';
          } else if (S[i - 1] == 'x' and S[i] == 'o') {
            ans[i + 1] = 'S';
          } else if (S[i - 1] == 'x' and S[i] == 'x') {
            ans[i + 1] = 'W';
          }
        } else if (ans[i - 1] == 'S' and ans[i] == 'W') {
          if (S[i - 1] == 'o' and S[i] == 'o') {
            ans[i + 1] = 'W';
          } else if (S[i - 1] == 'o' and S[i] == 'x') {
            ans[i + 1] = 'S';
          } else if (S[i - 1] == 'x' and S[i] == 'o') {
            ans[i + 1] = 'W';
          } else if (S[i - 1] == 'x' and S[i] == 'x') {
            ans[i + 1] = 'S';
          }
        } else if (ans[i - 1] == 'W' and ans[i] == 'S') {
          if (S[i - 1] == 'o' and S[i] == 'o') {
            ans[i + 1] = 'W';
          } else if (S[i - 1] == 'o' and S[i] == 'x') {
            ans[i + 1] = 'S';
          } else if (S[i - 1] == 'x' and S[i] == 'o') {
            ans[i + 1] = 'W';
          } else if (S[i - 1] == 'x' and S[i] == 'x') {
            ans[i + 1] = 'S';
          }
        } else if (ans[i - 1] == 'W' and ans[i] == 'W') {
          if (S[i - 1] == 'o' and S[i] == 'o') {
            ans[i + 1] = 'S';
          } else if (S[i - 1] == 'o' and S[i] == 'x') {
            ans[i + 1] = 'W';
          } else if (S[i - 1] == 'x' and S[i] == 'o') {
            ans[i + 1] = 'S';
          } else if (S[i - 1] == 'x' and S[i] == 'x') {
            ans[i + 1] = 'W';
          }
        }
      }
      int ok = true;
      if (ans[0] == 'S') {
        if (S[0] == 'o') {
          if (not((ans[1] == 'S' and ans[N - 1] == 'S') or
                  (ans[1] == 'W' and ans[N - 1] == 'W'))) {
            ok = false;
          }
        } else {
          if (not((ans[1] == 'S' and ans[N - 1] == 'W') or
                  (ans[1] == 'W' and ans[N - 1] == 'S'))) {
            ok = false;
          }
        }
      } else {
        if (S[0] == 'o') {
          if (not((ans[1] == 'S' and ans[N - 1] == 'W') or
                  (ans[1] == 'W' and ans[N - 1] == 'S'))) {
            ok = false;
          }
        } else {
          if (not((ans[1] == 'S' and ans[N - 1] == 'S') or
                  (ans[1] == 'W' and ans[N - 1] == 'W'))) {
            ok = false;
          }
        }
      }
      for (int i = 1; i < N - 1; i++) {
        if (ans[i] == 'S') {
          if (S[i] == 'o') {
            if (not((ans[i + 1] == 'S' and ans[i - 1] == 'S') or
                    (ans[i + 1] == 'W' and ans[i - 1] == 'W'))) {
              ok = false;
            }
          } else {
            if (not((ans[i + 1] == 'S' and ans[i - 1] == 'W') or
                    (ans[i + 1] == 'W' and ans[i - 1] == 'S'))) {
              ok = false;
            }
          }
        } else {
          if (S[i] == 'o') {
            if (not((ans[i + 1] == 'S' and ans[i - 1] == 'W') or
                    (ans[i + 1] == 'W' and ans[i - 1] == 'S'))) {
              ok = false;
            }
          } else {
            if (not((ans[i + 1] == 'S' and ans[i - 1] == 'S') or
                    (ans[i + 1] == 'W' and ans[i - 1] == 'W'))) {
              ok = false;
            }
          }
        }
      }
      if (ans[N - 1] == 'S') {
        if (S[N - 1] == 'o') {
          if (not((ans[0] == 'S' and ans[N - 2] == 'S') or
                  (ans[0] == 'W' and ans[N - 2] == 'W'))) {
            ok = false;
          }
        } else {
          if (not((ans[0] == 'S' and ans[N - 2] == 'W') or
                  (ans[0] == 'W' and ans[N - 2] == 'S'))) {
            ok = false;
          }
        }
      } else {
        if (S[N - 1] == 'o') {
          if (not((ans[0] == 'S' and ans[N - 2] == 'W') or
                  (ans[0] == 'W' and ans[N - 2] == 'S'))) {
            ok = false;
          }
        } else {
          if (not((ans[0] == 'S' and ans[N - 2] == 'S') or
                  (ans[0] == 'W' and ans[N - 2] == 'W'))) {
            ok = false;
          }
        }
      }
      // d(ans);
      if (ok) {
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << -1 << endl;
  return 0;
}