Problem:
 Score : 500 points    Problem Statement Snuke, who loves animals, built a zoo.  There are N animals in this zoo. They are conveniently numbered 1 through N , and arranged in a circle.
The animal numbered i (2≤i≤N-1) is adjacent to the animals numbered i-1 and i+1 . Also, the animal numbered 1 is adjacent to the animals numbered 2 and N , and the animal numbered N is adjacent to the animals numbered N-1 and 1 .  There are two kinds of animals in this zoo: honest sheep that only speak the truth, and lying wolves that only tell lies.  Snuke cannot tell the difference between these two species, and asked each animal the following question: "Are your neighbors of the same species?" The animal numbered i answered s_i . Here, if s_i is o , the animal said that the two neighboring animals are of the same species, and if s_i is x , the animal said that the two neighboring animals are of different species.  More formally, a sheep answered o if the two neighboring animals are both sheep or both wolves, and answered x otherwise.
Similarly, a wolf answered x if the two neighboring animals are both sheep or both wolves, and answered o otherwise.  Snuke is wondering whether there is a valid assignment of species to the animals that is consistent with these responses. If there is such an assignment, show one such assignment. Otherwise, print -1 .      Constraints  3 ≤ N ≤ 10^{5}  s is a string of length N consisting of o and x .         Input The input is given from Standard Input in the following format:  N  s       Output If there does not exist an valid assignment that is consistent with s , print -1 .
Otherwise, print an string t in the following format. The output is considered correct if the assignment described by t is consistent with s .   t is a string of length N consisting of S and W .  If t_i is S , it indicates that the animal numbered i is a sheep. If t_i is W , it indicates that the animal numbered i is a wolf.         Sample Input 1 6
ooxoox      Sample Output 1 SSSWWS  For example, if the animals numbered 1 , 2 , 3 , 4 , 5 and 6 are respectively a sheep, sheep, sheep, wolf, wolf, and sheep, it is consistent with their responses. Besides, there is another valid assignment of species: a wolf, sheep, wolf, sheep, wolf and wolf.  Let us remind you: if the neiboring animals are of the same species, a sheep answers o and a wolf answers x . If the neiboring animals are of different species, a sheep answers x and a wolf answers o .          Sample Input 2 3
oox      Sample Output 2 -1  Print -1 if there is no valid assignment of species.       Sample Input 3 10
oxooxoxoox      Sample Output 3 SSWWSSSWWS

Solution (Python):
import sys
input = sys.stdin.readline
n=int(input())
s=input()
def solve():
    for i in range(1,n):
        if (ans[i]=="S" and s[i]=="o") or (ans[i]=="W" and s[i]=="x"):
            ans[i+1]=ans[i-1]
        else:
            if ans[i-1]=="S":
                ans[i+1]="W"
            else:
                ans[i+1]="S"

        if (ans[0]=="S" and s[0]=="o") or (ans[0]=="W" and s[0]=="x"):
            sl=ans[1]
        else:
            if ans[1]=="S":
                sl="W"
            else:
                sl="S"
    if ans[0]==ans[-1] and sl==ans[n-1]:
        return True
    else:
        return False

ans=[0]*(n+1)

for i in ["S","W"]:
    for j in ["S","W"]:
        ans[0]=i
        ans[1]=j
        if solve():
            print("".join(ans[:-1]))
            exit()
print(-1)

Solution (Python):
n=int(input())
s=input()
x=["SS","SW","WS","WW"]

def check(i,j):
    if x[i][j]=="S":
        if s[j]=="o":
            if x[i][j-1]=="S":
                return x[i][j+1]=="S"
            else:
                return x[i][j+1]=="W"
        else:
            if x[i][j-1]=="S":
                return x[i][j+1]=="W"
            else:
                return x[i][j+1]=="S"
    else:
        if s[j]=="o":
            if x[i][j-1]=="S":
                return x[i][j+1]=="W"
            else:
                return x[i][j+1]=="S"
        else:
            if x[i][j-1]=="S":
                return x[i][j+1]=="S"
            else:
                return x[i][j+1]=="W"

a=["S","W"]
for i in range(4):
    for j in range(n-2):
        if x[i][j+1]=="S":
            if s[j+1]=="o":
                x[i]+=a[x[i][j]!="S"]
            else:
                x[i]+=a[x[i][j]=="S"]
        else:
            if s[j+1]=="o":
                x[i]+=a[x[i][j]=="S"]
            else:
                x[i]+=a[x[i][j]!="S"]
    if check(i,0) and check(i,-1):
        print(x[i])
        break
else:
    print(-1)

Solution (Python):
n=int(input())
s=input()
x=["SS","SW","WS","WW"]

def check0(i):
    if x[i][0]=="S":
        if s[0]=="o":
            if x[i][-1]=="S":
                return x[i][1]=="S"
            else:
                return x[i][1]=="W"
        else:
            if x[i][-1]=="S":
                return x[i][1]=="W"
            else:
                return x[i][1]=="S"
    else:
        if s[0]=="o":
            if x[i][-1]=="S":
                return x[i][1]=="W"
            else:
                return x[i][1]=="S"
        else:
            if x[i][-1]=="S":
                return x[i][1]=="S"
            else:
                return x[i][1]=="W"
def check1(i):
    if x[i][n-1]=="S":
        if s[n-1]=="o":
            if x[i][n-2]=="S":
                return x[i][0]=="S"
            else:
                return x[i][0]=="W"
        else:
            if x[i][n-2]=="S":
                return x[i][0]=="W"
            else:
                return x[i][0]=="S"
    else:
        if s[n-1]=="o":
            if x[i][n-2]=="S":
                return x[i][0]=="W"
            else:
                return x[i][0]=="S"
        else:
            if x[i][n-2]=="S":
                return x[i][0]=="S"
            else:
                return x[i][0]=="W"

a=["S","W"]
for i in range(4):
    for j in range(n-2):
        if x[i][j+1]=="S":
            if s[j+1]=="o":
                x[i]+=a[x[i][j]!="S"]
            else:
                x[i]+=a[x[i][j]=="S"]
        else:
            if s[j+1]=="o":
                x[i]+=a[x[i][j]=="S"]
            else:
                x[i]+=a[x[i][j]!="S"]
    if check0(i) and check1(i):
        print(x[i])
        break
else:
    print(-1)

Solution (Python):
n=int(input())
s=input()
x=["SS","SW","WS","WW"]

def check0(i):
    if x[i][0]=="S":
        if s[0]=="o":
            if x[i][-1]=="S":
                return x[i][1]=="S"
            else:
                return x[i][1]=="W"
        else:
            if x[i][-1]=="S":
                return x[i][1]=="W"
            else:
                return x[i][1]=="S"
    else:
        if s[0]=="o":
            if x[i][-1]=="S":
                return x[i][1]=="W"
            else:
                return x[i][1]=="S"
        else:
            if x[i][-1]=="S":
                return x[i][1]=="S"
            else:
                return x[i][1]=="W"
def check1(i):
    if x[i][n-1]=="S":
        if s[n-1]=="o":
            if x[i][n-2]=="S":
                return x[i][0]=="S"
            else:
                return x[i][0]=="W"
        else:
            if x[i][n-2]=="S":
                return x[i][0]=="W"
            else:
                return x[i][0]=="S"
    else:
        if s[n-1]=="o":
            if x[i][n-2]=="S":
                return x[i][0]=="W"
            else:
                return x[i][0]=="S"
        else:
            if x[i][n-2]=="S":
                return x[i][0]=="S"
            else:
                return x[i][0]=="W"


for i in range(4):
    for j in range(n-2):
        if x[i][j+1]=="S":
            if s[j+1]=="o":
                if x[i][j]=="S":
                    x[i]+="S"
                else:
                    x[i]+="W"
            else:
                if x[i][j]=="S":
                    x[i]+="W"
                else:
                    x[i]+="S"
        else:
            if s[j+1]=="o":
                if x[i][j]=="S":
                    x[i]+="W"
                else:
                    x[i]+="S"
            else:
                if x[i][j]=="S":
                    x[i]+="S"
                else:
                    x[i]+="W"
    
    if check0(i) and check1(i):
        print(x[i])
        break
else:
    print(-1)

Solution (Python):
N = int(input())
s= input()

def judge(start):
    ans = start
    for i in range(1, N):
        
        if ans[i]=="S":
            
            if s[i]=="o":
                 ans[i+1]=ans[i-1]
            else:
                  
                 ans[i+1]= "S" if ans[i-1]=="W" else "W"
        else:
            if s[i]=="o":
                
                ans[i+1]= "S" if ans[i-1]=="W" else "W"
            else:
                  
                 ans[i+1]=ans[i-1]

    
    if ans[0]==ans[N]:
        if ans[0]=="S":
            if s[0]=="o" and ans[1] == ans[N-1]:
                print("".join(ans[:-1]))
                exit()
            elif s[0]=="x" and ans[1] != ans[N-1]:
                print("".join(ans[:-1]))
                exit()
        elif ans[0]=="W":
            if s[0]=="o" and ans[1] != ans[N-1]:
                print("".join(ans[:-1]))
                exit()
            elif s[0]=="x" and ans[1] == ans[N-1]:
                print("".join(ans[:-1]))
                exit()
    else:
        return(-1)

judge(list("W"+"W"*(N)))
judge(list("W"+"S"*(N)))
judge(list("S"+"S"*(N)))
judge(list("S"+"W"*(N)))

print(-1)

Solution (Python):
import sys
n=int(input())
s=input()
S=[]
for i in range(n):
  if s[i]=="o":
    S.append(1)
  else:
    S.append(-1)
A=[1,1]
for i in range(1,n-1):
  A.append(S[i]*A[-2]*A[-1])
if S[-1]==A[-2]*A[-1]*A[0]:
  if S[0]==A[-1]*A[0]*A[1]:
    for i in range(n):
      if A[i]==1:
        print("S",end="")
      else:
        print("W",end="")
      if i==n-1:
        print()
        sys.exit()
A=[1,-1]
for i in range(1,n-1):
  A.append(S[i]*A[-2]*A[-1])
if S[-1]==A[-2]*A[-1]*A[0]:
  if S[0]==A[-1]*A[0]*A[1]:
    for i in range(n):
      if A[i]==1:
        print("S",end="")
      else:
        print("W",end="")
      if i==n-1:
        print()
        sys.exit()
A=[-1,1]
for i in range(1,n-1):
  A.append(S[i]*A[-2]*A[-1])
if S[-1]==A[-2]*A[-1]*A[0]:
  if S[0]==A[-1]*A[0]*A[1]:
    for i in range(n):
      if A[i]==1:
        print("S",end="")
      else:
        print("W",end="")
      if i==n-1:
        print()
        sys.exit()
A=[-1,-1]
for i in range(1,n-1):
  A.append(S[i]*A[-2]*A[-1])
if S[-1]==A[-2]*A[-1]*A[0]:
  if S[0]==A[-1]*A[0]*A[1]:
    for i in range(n):
      if A[i]==1:
        print("S",end="")
      else:
        print("W",end="")
      if i==n-1:
        print()
        sys.exit()
print(-1)

Solution (Python):
n = int(input())
s = input()

w = {-1: 'W', 1: 'S'}
p = {1:'o', -1:'x'}

for tmp in [[1, 1], [-1, -1], [1, -1], [-1, 1]]:
    for i in range(1,n):
        tmp.append(tmp[i-1] if s[i] == p[tmp[i]] else (tmp[i-1] * -1))
    if tmp[0] == tmp[-1]:
        flag = 1 if s[0] == p[tmp[0]] else -1
        if tmp[-2] == tmp[1] * flag:
            tmp.pop()
            print(''.join(map(w.get, tmp)))
            break
else:
    print(-1)

Solution (Python):
n = int(input())
s = input()

w = {-1: 'W', 1: 'S'}
p = {1:'o', -1:'x'}
lst = [[1, 1], [-1, -1], [1, -1], [-1, 1]]

tmp = [0 for i in range(n+1)]
for a in lst:
    tmp[0] = a[0]
    tmp[1] = a[1]
    for i in range(1,n):
        tmp[i+1] = tmp[i-1] if s[i] == p[tmp[i]] else (tmp[i-1] * -1)
    if tmp[0] == tmp[-1]:
        flag = 1 if s[0] == p[tmp[0]] else -1
        if tmp[-2] == tmp[1] * flag:
            tmp.pop()
            print(''.join(map(w.get, tmp)))
            break
else:
    print(-1)

Solution (Python):


import sys
from itertools import product

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
S = input()
S += S[0]

def check(a, b):
    res = [a, b]
    
    for i in range(1, N+1):
        
        if res[i-1] == res[i] and S[i] == 'o' \
                or res[i-1] != res[i] and S[i] == 'x':
            res.append('S')
        
        else:
            res.append('W')
    
    if res[0] == res[N] and res[1] == res[N+1]:
        return res
    else:
        return []


for a, b in product(['S', 'W'], repeat=2):
    res = check(a, b)
    if res:
        print(''.join(res[:N]))
        exit()
print(-1)

Solution (Python):
import sys

def rev(t):
    if t == "S":
        return "W"
    else:
        return "S"

N = int(input())

s = input()

for p in range(2):

    for q in range(2):

        lis = []

        if p == 0:
            lis.append("S")
        else:
            lis.append("W")
        if q == 0:
            lis.append("S")
        else:
            lis.append("W")

        for i in range(N-2):

            if lis[-1] == "S":
                if s[i+1] == "o":
                    lis.append(lis[i])
                else:
                    lis.append(rev(lis[i]))

            else:
                if s[i+1] == "o":
                    lis.append(rev(lis[i]))
                else:
                    lis.append(lis[i])

        flag = True
        
        if lis[0] == "S":
            if s[0] == "o" and lis[-1] != lis[1]:
                flag = False
            elif s[0] == "x" and lis[-1] == lis[1]:
                flag = False

        else:
            if s[0] == "o" and lis[-1] == lis[1]:
                flag = False
            elif s[0] == "x" and lis[-1] != lis[1]:
                flag = False

        
        if lis[-1] == "S":
            if s[-1] == "o" and lis[-2] != lis[0]:
                flag = False
            elif s[-1] == "x" and lis[-2] == lis[0]:
                flag = False

        else:
            if s[-1] == "o" and lis[-2] == lis[0]:
                flag = False
            elif s[-1] == "x" and lis[-2] != lis[0]:
                flag = False

        if flag:
            print ("".join(lis))
            sys.exit()

print (-1)

Solution (Python):
N = int(input())
S = input()

a = {'S':'W', 'W':'S'}
for t0, t1 in ('SS', 'SW', 'WS', 'WW'):
    T = [t0, t1]
    for i in range(2, N+2):
        if (S[(i-1)%N]=='o' and T[-1]=='S') or (S[(i-1)%N]=='x' and T[-1]=='W'):
            T.append(T[-2])
        else:
            T.append(a[T[-2]])

    if T[0]==T[-2] and T[1]==T[-1]:
        print(''.join(T[:-2]))
        exit()
print(-1)

Solution (Python):
n = int(input())
s = input()
ans = ["SS","SW","WS","WW"]
for a in ans:
    for i in range(2,n+1):
        if s[i-1] == "o":
            if a[-2:] == "SS" or a[-2:] == "WW":
                a += "S"
            else:
                a += "W"
        if s[i-1] == "x":
            if a[-2:] == "SW" or a[-2:] == "WS":
                a += "S"
            else:
                a += "W"
    else:
        if a[n] == a[0]:
            if (((s[0],a[0]) in (("o","S"),("x","W")) and a[1] == a[n-1])
                or ((s[0],a[0]) in (("x","S"),("o","W")) and a[1] != a[n-1])):
                print(a[:n])
                exit()
print(-1)

Solution (Python):
n=int(input())
s=input()


import itertools
for x,y in itertools.product(["S","W"], repeat=2):
  ans=[None]*n
  ans[-1]=x
  ans[0]=y
  flag=1
  for i in range(n): 
    if 0<=i<=n-3: 
      if ans[i]=="S":
        if s[i]=="o":
          ans[i+1]=ans[i-1] 
        else: 
          if ans[i-1]=="W":
            ans[i+1]="S"
          else:
            ans[i+1]="W"
      
      if ans[i]=="W": 
        if s[i]=="x":
          ans[i+1]=ans[i-1]
        else:
          if ans[i-1]=="W":
            ans[i+1]="S" 
          else:
            ans[i+1]="W"
    else: 
      if ans[i]=="S":
        if s[i]=="o":
          if ans[(i+1)%n]!=ans[i-1]:
            flag=0
        else:
          if ans[(i+1)%n]==ans[i-1]:
            flag=0
      
      if ans[i]=="W":
        if s[i]=="x":
          if ans[(i+1)%n]!=ans[i-1]:
            flag=0
        else:
          if ans[(i+1)%n]==ans[i-1]:
            flag=0
  if flag==1:
    print("".join(ans))
    exit()


print(-1)

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    S=ST()
    S+=S[0]
    def is_ok():
        for i,s in enumerate(S[1:-1],start=1):
            if ans[i]==(s=="o"):
                ans[i+1]=ans[i-1]
            else:
                ans[i+1]=ans[i-1]^1
        ok = (ans[0]==ans[-1])
        if ans[0]==(S[0]=="o"):
            if ans[1]!=ans[-2]:
                ok=False
        else:
            if ans[1]==ans[-2]:
                ok=False
        return ok

    for bit in range(1<<2):
        ans=[0]*(N+1)
        for i in range(2):
            ans[i] = bit>>i&1
        if is_ok():
            ans="".join(map(str,ans[:-1]))
            print(ans.replace("1","S").replace("0","W"))
                
            return
    print(-1)
if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    S = input()

    def make(S0, S1) -> str:
        rec = S0+S1
        for i in range(1, N-1):
            if S[i] == "o" and rec[i] == "S":
                rec = rec + rec[i-1]
            elif S[i] == "o" and rec[i] == "W":
                if rec[i-1] == "S":
                    rec = rec + "W"
                else:
                    rec = rec + "S"
            elif S[i] == "x" and rec[i] == "S":
                if rec[i-1] == "S":
                    rec = rec + "W"
                else:
                    rec = rec + "S"
            elif S[i] == "x" and rec[i] == "W":
                rec = rec + rec[i-1]

        return rec

    def checker(rec) -> bool:
        for i in range(N-1):
            if S[i] == "o" and rec[i] == "S":
                if rec[i-1] != rec[i+1]:
                    return False
            elif S[i] == "o" and rec[i] == "W":
                if rec[i-1] == rec[i+1]:
                    return False
            elif S[i] == "x" and rec[i] == "S":
                if rec[i-1] == rec[i+1]:
                    return False
            elif S[i] == "x" and rec[i] == "W":
                if rec[i-1] != rec[i+1]:
                    return False
        if S[N-1] == "o" and rec[N-1] == "S":
            return rec[N-2] == rec[0]
        elif S[N-1] == "o" and rec[N-1] == "W":
            return rec[N-2] != rec[0]
        elif S[N-1] == "x" and rec[N-1] == "S":
            return rec[N-2] != rec[0]
        elif S[N-1] == "x" and rec[N-1] == "W":
            return rec[N-2] == rec[0]

    for S0, S1 in (("S", "S"), ("S", "W"), ("W", "S"), ("W", "W")):
        rec = make(S0, S1)
        if checker(rec):
            print(rec)
            break
    else:
        print(-1)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from itertools import product

n = int(sys.stdin.readline().rstrip())
s = sys.stdin.readline().rstrip()
resp = s
resp = resp.replace('o', '1')
resp = resp.replace('x', '0')
resp = [None] + [int(x) for x in resp]

def restore(pattern):
    for i in range(1, n):
        if resp[i] ^ pattern[i]:
            pattern[i+1] = 1 if pattern[i-1] ^ 1 else 0
        else:
            pattern[i+1] = pattern[i-1]
    return pattern

def contradicting(pattern):
    if pattern[0] != pattern[-1]:
        return True
    if resp[-1] ^ pattern[-1]:
        if not pattern[-2] ^ pattern[1]:
            return True
    else:
        if pattern[-2] ^ pattern[1]:
            return True   
    return False

def main():
    res = [None] * (n + 1)
    for i in range(4):
        pattern = res.copy()
        pattern[0] = i >> 0 & 1
        pattern[1] = i >> 1 & 1
        ans = restore(pattern)
        if not contradicting(ans):
            t = ''
            for i in range(1, n+1):
                t += 'S' if ans[i] & 1 else 'W'
            return t
    return -1

if __name__ == '__main__':
    ans = main()
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    N = int(input())
    S = input()
    for key in ['SS', 'SW', 'WS', 'WW']:
        animals = list(key)
        for i in range(N + 1):
            if i < 1:
                continue
            S_i = S[i] if i < N else S[0]
            if animals[i] == 'S':
                if S_i == 'o':
                    animals += animals[-2]
                else:
                    animals += 'S' if animals[-2] == 'W' else 'W'
            else:
                if S_i == 'x':
                    animals += animals[-2]
                else:
                    animals += 'S' if animals[-2] == 'W' else 'W'
        if animals[0] == animals[-2] and animals[1] == animals[-1]:
            print(''.join(animals[:-2]))
            exit(0)
    print(-1)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
S = input()

T = [["S", "S"], ["S", "W"], ["W", "S"], ["W", "W"]]

for t in T:
  for i in range(1, N-1):
    if (t[i] == "S" and S[i] == "o") or (t[i] == "W" and S[i] == "x"):
        t.append(t[i-1])
    else:
        t.append("S" if t[i-1] == "W" else "W")
  
  
  flag = 1
  if (t[0] == "S" and S[0] == "o") or (t[0] == "W" and S[0] == "x"):
    if t[-1] != t[1]: flag = 0
  else:
    if t[-1] == t[1]: flag = 0
  if (t[-1] == "S" and S[-1] == "o") or (t[-1] == "W" and S[-1] == "x"):
    if t[-2] != t[0]: flag = 0
  else:
    if t[-2] == t[0]: flag = 0
  if flag:
    print("".join(t))
    exit()
print(-1)

Solution (Python):
import sys
input = sys.stdin.readline
from itertools import product

def main():
    n = int(input())
    s = input()
    ans = "-1"

    ls = list(product("SW", repeat = 2))

    for tpl in ls:
        anss = str(tpl[0]) + str(tpl[1])
        for i in range(1,n+1):
            if anss[-1] == "S":
                if s[i-1] == "o":
                    anss += anss[-2]
                else:
                    if anss[-2] == "S":
                        anss += "W"
                    else:
                        anss += "S"
            else:
                if s[i-1] == "x":
                    anss += anss[-2]
                else:
                    if anss[-2] == "S":
                        anss += "W"
                    else:
                        anss += "S"
        if anss[:2] == anss[-2:]:
            ans = anss[1:-1]
            break
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline
from itertools import product

def main():
    n = int(input())
    s = input()
    ans = "-1"

    ls = list(product("SW", repeat = 2))

    for tpl in ls:
        anss = str(tpl[0]) + str(tpl[1])
        for i in range(1,n+1):
            if anss[-1] == "S":
                if s[i-1] == "o":
                    anss += anss[-2]
                else:
                    if anss[-2] == "S":
                        anss += "W"
                    else:
                        anss += "S"
            else:
                if s[i-1] == "x":
                    anss += anss[-2]
                else:
                    if anss[-2] == "S":
                        anss += "W"
                    else:
                        anss += "S"
        if anss[:2] == anss[-2:]:
            ans = anss[1:-1]
            break
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):



N = int(input())
S = input()





for s1,sn in [("S","S"), ("S","W"), ("W","S"), ("W","W")]:
    ans = [""]*(N+2)
    ans[1] = s1
    ans[0] = sn
    for i in range(1,N+1):
       
        if ans[i] == "S":

            if S[i-1] == "o":
                ans[i+1] = "S" if ans[i-1] =="S" else "W"
            else:
                ans[i+1] = "W" if ans[i-1] =="S" else "S"
        else:
            if S[i-1] == "o":
                ans[i+1] = "S" if ans[i-1] =="W" else "W"
            else:
                ans[i+1] = "W" if ans[i-1] =="W" else "S"

    if ans[0] == ans[N] and ans[1] == ans[N+1]:
        print("".join(ans[1:N+1]))
        exit()

print(-1)

Solution (Python):
N = int(input())
S = list(input())

def output(lst):
    map_lst = map(str, lst[:-1])
    s = ''.join(map_lst)
    print (s.replace('0', 'S').replace('1', 'W'))
    return


for a in range(2):
    for b in range(2):
        lst = [-1] * (N + 1)
        lst[0] = a
        lst[1] = b
        for i in range(1, N):
            if S[i] == 'o':
                flag = True
            else:
                flag = False

            if flag == (lst[i] == 0):
                lst[i + 1] = lst[i - 1]
            else:
                lst[i + 1] = 1 - lst[i - 1]
        if lst[0] == lst[-1]: 
            if (S[0] == 'o') == (lst[0] == 0):
                if lst[1] == lst[N - 1]:
                    output(lst)
                    exit()
            else:
                if lst[1] != lst[N - 1]:
                    output(lst)
                    exit()

        

print (-1)

Solution (Python):
n = int(input())
s = input()

def test(ans):
    f = True
    for i in range(1, n - 1):
        if ans[i] == 'S':
            if ans[i - 1] == 'S' and s[i] == 'o':
                ans += 'S'
            elif ans[i - 1] == 'S' and s[i] == 'x':
                ans += 'W'
            elif ans[i - 1] == 'W' and s[i] == 'o':
                ans += 'W'
            elif ans[i - 1] == 'W' and s[i] == 'x':
                ans += 'S'
        else:
            if ans[i - 1] == 'S' and s[i] == 'o':
                ans += 'W'
            elif ans[i - 1] == 'S' and s[i] == 'x':
                ans += 'S'
            elif ans[i - 1] == 'W' and s[i] == 'o':
                ans += 'S'
            elif ans[i - 1] == 'W' and s[i] == 'x':
                ans += 'W'

    
    if ans[n-1] == 'S' and s[n-1] == 'o':
        if ans[n-2] != ans[0]:
            f = False
    elif ans[n-1] == 'S' and s[n-1] == 'x':
        if ans[n-2] == ans[0]:
            f = False
    elif ans[n-1] == 'W' and s[n-1] == 'o':
        if ans[n-2] == ans[0]:
            f = False
    elif ans[n-1] == 'W' and s[n-1] == 'x':
        if ans[n-2] != ans[0]:
            f = False

    
    if ans[0] == 'S' and s[0] == 'o':
        if ans[n-1] != ans[1]:
            f = False
    elif ans[0] == 'S' and s[0] == 'x':
        if ans[n-1] == ans[1]:
            f = False
    elif ans[0] == 'W' and s[0] == 'o':
        if ans[n-1] == ans[1]:
            f = False
    elif ans[0] == 'W' and s[0] == 'x':
        if ans[n-1] != ans[1]:
            f = False

    return ans, f



ans1, f1 = test('SS')
ans2, f2 = test('SW')
ans3, f3 = test('WW')
ans4, f4 = test('WS')

if f1:
    print(ans1)
elif f2:
    print(ans2)
elif f3:
    print(ans3)
elif f4:
    print(ans4)
else:
    print(-1)

Solution (Python):

import sys

def convert(ss):
    if ss == 0:
        return "S"
    else:
        return "W"


def solve(N: int, s: str):
    animals = [0]*N
    
    init = [(0,1),(1,0),(0,0),(1,1)]

    for first,second in init:
        animals[0] = first
        animals[1] = second

        for j in range(1,N):
            
            if j == N-1:
                if animals[j] == 0:
                    if s[j] == "o":
                        if animals[j-1] != animals[0]:
                            break
                    else:
                       if animals[j-1] == animals[0]:
                            break
                elif animals[j] == 1:
                    if s[j] == "o":
                        if animals[j-1] == animals[0]:
                            break
                    else:
                       if animals[j-1] != animals[0]:
                            break

                if animals[0] == 0:
                    if s[0] == "o":
                        if animals[N-1] != animals[1]:
                            break
                    else:
                       if animals[N-1] == animals[1]:
                            break
                elif animals[0] == 1:
                    if s[0] == "o":
                        if animals[N-1] == animals[1]:
                            break
                    else:
                       if animals[N-1] != animals[1]:
                            break
                
                print(''.join(list(map(convert,animals))))
                return

            if animals[j] == 0:
                if s[j] == "o":
                    animals[j+1] = animals[j-1]
                else:
                    animals[j+1] = 1^animals[j-1]
            elif animals[j] == 1:
                if s[j] == "o":
                    animals[j+1] = 1^animals[j-1]
                else:
                    animals[j+1] = animals[j-1]    

    print(-1)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    s = next(tokens)  
    solve(N, s)

if __name__ == '__main__':
    main()

Solution (Python):
from itertools import product
n = int(input())
s = input()
s_dict = {"o":1, "x":-1}
flg = False
for combi in product([-1,1], repeat=2):
    state = list(combi)
    for i, _s in enumerate(s[1:], 1):
        cur = state[i]
        res = s_dict[_s] * cur * state[i-1]
        state.append(res)
    if state[0] == state[-1] and state[n-1] == state[0] * state[1] * s_dict[s[0]]:
        flg = True
        break
res = "".join(["S" if i == 1 else "W" for i in state])
if flg:
    print(res[:-1])
else:
    print(-1)