Language: C++

#include <bits/stdc++.h>
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
typedef long long ll;
using namespace std;

int uni[1000010];
void init(int N){
  for(int i=0;i<N;i++){
    uni[i]=-1;
  }
}

int root(int a){
  if(uni[a] < 0)return a;
  else return uni[a] = root(uni[a]);
}

bool unite(int a,int b){
  a = root(a);
  b = root(b);
  if(a==b) return false;
  if(uni[a] > uni[b]) swap(a,b);

  uni[a]+=uni[b];
  uni[b]=a;

}

bool same(int a,int b){
  a = root(a);
  b = root(b);
  if(a==b)return true;
  else return false;
}

int animal[100010];

void ch(){
  for(int i=0;i<n-1;i++){
    if(s[i]=='o' && animal[i]==1){
      if(i-1<0){
        unite(n-1,1);
        animal[1]=1;
      }else{
        animal[i+1]=animal[i-1];
        unite(i-1,i+1);
      }
    }

    if(s[i]=='x' && animal[i]==1){
      if(i-1<0){
        animal[1]=2;
      }else{
        if(animal[i-1]==1)animal[i+1]=2;
        if(animal[i-1]==2)animal[i+1]=1;
      }
    }

    if(s[i]=='x' && animal[i]==2){
      if(i-1<0){
        unite(n-1,1);
      }else{
        animal[i+1]=animal[i-1];
        unite(i-1,i+1);
      }
    }
    if(s[i]=='o' && animal[i]==2){
      if(i-1<0){
        //
        animal[1]=1;
      }else{
        if(animal[i-1]==1)animal[i+1]=2;
        if(animal[i-1]==2)animal[i+1]=1;

      }
    }
  }
}
int main(){
  //最後と始めが羊だとする。
  int n;
  string s;
  cin>>n;
  cin>>s;

  init(n);

  //1羊　2狼
  for(int i=0;i<n;i++)animal[i]=1;


  ch();


  int px=n-1;

  bool flag=true;

  if(s[px]=='o' && animal[px]==1){
    if(!same(px-1,0))flag=false;
  }
  if(s[px]=='o' && animal[px]==2){
    if(same(px-1,0))flag=false;
  }
  if(s[px]=='x' && animal[px]==1){
    if(same(px-1,0))flag=false;
  }
  if(s[px]=='x' && animal[px]==2){
    if(!same(px-1,0))flag=false;
  }

  if(!flag){

    ch();
    int px=n-1;

    bool flag=true;

    if(s[px]=='o' && animal[px]==1){
      if(!same(px-1,0))flag=false;
    }
    if(s[px]=='o' && animal[px]==2){
      if(same(px-1,0))flag=false;
    }
    if(s[px]=='x' && animal[px]==1){
      if(same(px-1,0))flag=false;
    }
    if(s[px]=='x' && animal[px]==2){
      if(!same(px-1,0))flag=false;
    }
    if(!flag)
    cout<<"-1"<<endl;

    else{
      string ans="";
      for(int i=0;i<n;i++){
        if(animal[i]==1)ans+="S";
        else ans+="W";

        //cout<<"uni["<<i<<"]"<<uni[i]<<endl;
      }
      cout<<ans<<endl;
    }
  }else{
    string ans="";
    for(int i=0;i<n;i++){
      if(animal[i]==1)ans+="S";
      else ans+="W";

      //cout<<"uni["<<i<<"]"<<uni[i]<<endl;
    }
    cout<<ans<<endl;
  }


}


Compiler output:
a.cc: In function 'void ch()':
a.cc:40:17: error: 'n' was not declared in this scope
   40 |   for(int i=0;i<n-1;i++){
      |                 ^
a.cc:41:8: error: 's' was not declared in this scope
   41 |     if(s[i]=='o' && animal[i]==1){
      |        ^
a.cc:51:8: error: 's' was not declared in this scope
   51 |     if(s[i]=='x' && animal[i]==1){
      |        ^
a.cc:60:8: error: 's' was not declared in this scope
   60 |     if(s[i]=='x' && animal[i]==2){
      |        ^
a.cc:68:8: error: 's' was not declared in this scope
   68 |     if(s[i]=='o' && animal[i]==2){
      |        ^
a.cc: In function 'bool unite(int, int)':
a.cc:26:9: warning: control reaches end of non-void function [-Wreturn-type]
   26 |   uni[b]=a;
      |   ~~~~~~^~