Problem p03800 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int N;
string S;

void rec(int idx1, bool t1, int idx2, bool t2, int idx, bool t3) {
  vector<int> v(N, -1);
  v[idx] = t3;
  v[idx1] = t1;
  v[idx2] = t2;

  queue<int> que;
  que.push(idx1);
  que.push(idx2);

  while (!que.empty()) {
    int p = que.front();
    que.pop();
    int s = (p - 1 + N) % N;
    int t = (p + 1) % N;
    if (v[p]) { // 真実
      if (S[p] == 'o') {
        if (~v[s] && ~v[t]) {
          if (v[s] != v[t])
            return;
          continue;
        }
        if (~v[s])
          v[t] = v[s], que.push(t);
        else
          v[s] = v[t], que.push(s);
      } else if (S[p] == 'x') {
        if (~v[s] && ~v[t]) {
          if (v[s] == v[t])
            return;
          continue;
        }
        if (~v[s])
          v[t] = v[s] ^ 1, que.push(t);
        else
          v[s] = v[t] ^ 1, que.push(s);
      }
    } else { // 逆
      if (S[p] == 'o') {
        if (~v[s] && ~v[t]) {
          if (v[s] == v[t])
            return;
          continue;
        }
        if (~v[s])
          v[t] = v[s] ^ 1, que.push(t);
        else
          v[s] = v[t] ^ 1, que.push(s);
      } else if (S[p] == 'x') {
        if (~v[s] && ~v[t]) {
          if (v[s] != v[t])
            return;
          continue;
        }
        if (~v[s])
          v[t] = v[s], que.push(t);
        else
          v[s] = v[t], que.push(s);
      }
    }
  }

  for (int i = 0; i < N; i++) {
    if (v[i])
      cout << "S";
    else
      cout << "W";
  }
  cout << endl;
  exit(0);
}

int main() {
  cin >> N;
  cin >> S;

  if (count(begin(S), end(S), 'o') == N) {
    cout << string(N, 'S') << endl;
    return (0);
  }

  for (int i = 0; i < S.size(); i++) {
    if (S[i] == 'x') {
      for (int j = 0; j < 2; j++) {
        for (int k = 0; k < 2; k++) {
          rec((i + N - 1) % N, j, (i + 1) % N, k, i, j != k);
        }
      }
    }
  }

  cout << -1 << endl;
}

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

using namespace std;

int N;
string S;

void rec(int idx1, bool t1, int idx2, bool t2, int idx, bool t3) {
  vector<int> v(N, -1);
  v[idx] = t3;
  v[idx1] = t1;
  v[idx2] = t2;

  queue<int> que;
  que.push(idx1);
  que.push(idx2);

  while (!que.empty()) {
    int p = que.front();
    que.pop();
    int s = (p - 1 + N) % N;
    int t = (p + 1) % N;
    if (v[p]) { // 真実
      if (S[p] == 'o') {
        if (~v[s] && ~v[t]) {
          if (v[s] != v[t])
            return;
          continue;
        }
        if (~v[s])
          v[t] = v[s], que.push(t);
        else
          v[s] = v[t], que.push(s);
      } else if (S[p] == 'x') {
        if (~v[s] && ~v[t]) {
          if (v[s] == v[t])
            return;
          continue;
        }
        if (~v[s])
          v[t] = v[s] ^ 1, que.push(t);
        else
          v[s] = v[t] ^ 1, que.push(s);
      }
    } else { // 逆
      if (S[p] == 'o') {
        if (~v[s] && ~v[t]) {
          if (v[s] == v[t])
            return;
          continue;
        }
        if (~v[s])
          v[t] = v[s] ^ 1, que.push(t);
        else
          v[s] = v[t] ^ 1, que.push(s);
      } else if (S[p] == 'x') {
        if (~v[s] && ~v[t]) {
          if (v[s] != v[t])
            return;
          continue;
        }
        if (~v[s])
          v[t] = v[s], que.push(t);
        else
          v[s] = v[t], que.push(s);
      }
    }
  }

  for (int i = 0; i < N; i++) {
    if (v[i])
      cout << "S";
    else
      cout << "W";
  }
  cout << endl;
  exit(0);
}

int main() {
  cin >> N;
  cin >> S;

  if (count(begin(S), end(S), 'o') == N) {
    cout << string(N, 'S') << endl;
    return (0);
  }

  for (int i = 0; i < S.size(); i++) {
    if (S[i] == 'x') {
      for (int j = 0; j < 2; j++) {
        for (int k = 0; k < 2; k++) {
          rec((i + N - 1) % N, j, (i + 1) % N, k, i, j != k);
        }
      }
      break;
    }
  }

  cout << -1 << endl;
}