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):


"""
参考：https://img.atcoder.jp/arc069/editorial.pdf
"""

N = int(input())
s = list(input())

def verify(t):
    
    for i in range(1, N-1):
        
        if t[i] == 'S' and s[i] == 'o' or t[i] == 'W' and s[i] == 'x':
            
            if t[i-1] == 'S':
                t[i+1] = 'S'
            else:
                t[i+1] = 'W'
        
        else:
            
            if t[i-1] == 'S':
                t[i+1] = 'W'
            else:
                t[i+1] = 'S'
    
    if t[-1] == 'S' and s[-1] == 'o' or t[-1] == 'W' and s[-1] == 'x':
        if not (t[-2] == 'S' and t[0] == 'S' or t[-2] == 'W' and t[0] == 'W'):
            return False
    else:
        if not (t[-2] == 'S' and t[0] == 'W' or t[-2] == 'W' and t[0] == 'S'):
            return False
    
    if t[0] == 'S' and s[0] == 'o' or t[0] == 'W' and s[0] == 'x':
        if not (t[-1] == 'S' and t[1] == 'S' or t[-1] == 'W' and t[1] == 'W'):
            return False
    else:
        if not (t[-1] == 'S' and t[1] == 'W' or t[-1] == 'W' and t[1] == 'S'):
            return False
    return True


for a, b in [('S', 'S'), ('W', 'W'), ('S', 'W'), ('W', 'S')]:
    t = [''] * N
    t[0] = a
    t[1] = b
    if verify(t): 
        print(''.join(t))
        exit()
print(-1)

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

animals_ans = []

def hypo(sheep, ans, w):
    animals = []
    for _ in range(N):
        animals.append(-1)
    animals[0] = sheep
    if sheep + ans != 1:
        if w == 0:
            animals[N-1] = 1
            animals[1] = 1
        else:
            animals[N-1] = 0
            animals[1] = 0
    else:
        if w == 0:
            animals[N-1] = 0
            animals[1] = 1
        else:
            animals[N-1] = 1
            animals[1] = 0
    for n in range(1, N):
        if (animals[n] == 1 and S[n] == 'o') or  (animals[n] == 0 and S[n] == 'x'):
            if n == N-2 and animals[n+1] != animals[n-1]:
                return 0
            elif n == N-1:
                if animals[n-1] != animals[0]:
                    return 0
                else:
                    return animals
            else:
                animals[n+1] = animals[n-1]
        else:
            if n == N-2 and animals[n+1] == animals[n-1]:
                return 0
            elif n == N-1:
                if animals[n-1] == animals[0]:
                    return 0
                else:
                    return animals
            else:
                animals[n+1] = 1 - animals[n-1]

def arr2str(arr):
    st = ''
    for n in arr:
        if n == 1:
            st += 'S'
        else:
            st += 'W'
    return st

ans = 1 if S[0] == 'o' else 0


animals_ans = hypo(1, ans, 0)
if animals_ans == 0:
    animals_ans = hypo(1, ans, 1)
    if animals_ans == 0:
        animals_ans = hypo(0, ans, 0)
        if animals_ans == 0:
            animals_ans = hypo(0, ans, 1)
            if animals_ans == 0:
                print(-1)
            else:
                print(arr2str(animals_ans))
        else:
            print(arr2str(animals_ans))
    else:
        print(arr2str(animals_ans))
else:
    print(arr2str(animals_ans))

Solution (Python):
n = int(input())
s = input()
sw4li = ["SS", "SW", "WS", "WW"]
t = ""
for v in sw4li:
    t = ""
    t += v
    for i in range(1, len(s)):
        if t[-2] == "S" and t[-1] == "S":
            if s[i] == "o":
                t += "S"
            elif s[i] == "x":
                t += "W"
        elif t[-2] == "S" and t[-1] == "W":
            if s[i] == "o":
                t += "W"
            elif s[i] == "x":
                t += "S"
        elif t[-2] == "W" and t[-1] == "S":
            if s[i] == "o":
                t += "W"
            elif s[i] == "x":
                t += "S"
        elif t[-2] == "W" and t[-1] == "W":
            if s[i] == "o":
                t += "S"
            elif s[i] == "x":
                t += "W"
    if t[-1] == t[0]:
        if t[-1] == "S" and t[1] == "S":
            if s[0] == "o":
                if t[-2] == "S":
                    print(t[:-1])
                    exit()
            elif s[0] == "x":
                if t[-2] == "W":
                    print(t[:-1])
                    exit()
        elif t[-1] == "S" and t[1] == "W":
            if s[0] == "o":
                if t[-2] == "W":
                    print(t[:-1])
                    exit()
            elif s[0] == "x":
                if t[-2] == "S":
                    print(t[:-1])
                    exit()
        elif t[-1] == "W" and t[1] == "S":
            if s[0] == "o":
                if t[-2] == "W":
                    print(t[:-1])
                    exit()
            elif s[0] == "x":
                if t[-2] == "S":
                    print(t[:-1])
                    exit()
        elif t[-1] == "W" and t[1] == "W":
            if s[0] == "o":
                if t[-2] == "S":
                    print(t[:-1])
                    exit()
            elif s[0] == "x":
                if t[-2] == "W":
                    print(t[:-1])
                    exit()
print(-1)

Solution (Python):


import sys
import itertools

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


n = II()
S = [c == 'o' for c in SI()]
S += S[:2]

SW = {'S': 'W', 'W': 'S'}
for a1, a2 in itertools.product('SW', repeat=2):
    M = [a1, a2]
    for i in range(n):
        if S[i + 1]:
            M.append(M[-2] if M[-1] == 'S' else SW[M[-2]])
        else:
            M.append(M[-2] if M[-1] == 'W' else SW[M[-2]])

    if M[0] == M[-2] and M[1] == M[-1]:
        print(*M[:-2], sep='')
        exit()

print(-1)

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


X = ['S', 'W']
for P in [[0, 0], [0, 1], [1, 0], [1, 1]]:
    for i, s in enumerate(S[1:], start=1):
        if s == 'o' and P[i] == 0:
            P.append(P[i - 1])

        if s == 'o' and P[i] == 1:
            P.append(P[i - 1] ^ 1)

        if s == 'x' and P[i] == 0:
            P.append(P[i - 1] ^ 1)

        if s == 'x' and P[i] == 1:
            P.append(P[i - 1])

    P = P[:-1]
    
    for i in range(N):
        if (S[i] == 'o') and (P[i] == 0) and (P[i - 1] != P[(i + 1) % N]):
            break

        if (S[i] == 'o') and (P[i] == 1) and (P[i - 1] == P[(i + 1) % N]):
            break

        if (S[i] == 'x') and (P[i] == 0) and (P[i - 1] == P[(i + 1) % N]):
            break

        if (S[i] == 'x') and (P[i] == 1) and (P[i - 1] != P[(i + 1) % N]):
            break

    else:
        ans = [X[p] for p in P]
        print(''.join(ans))
        exit()

print(-1)

Solution (Python):


import sys


INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


n = II()
s = SI()

s = s[-1] + s + s[0]
P = ('00', '01', '10', '11')  
for p in P:
    for i in range(1, n):
        
        if s[i + 1] == 'o':
            p += p[-2] if p[-1] == '0' else str(int(p[-2]) ^ 1)
        else:
            p += p[-2] if p[-1] == '1' else str(int(p[-2]) ^ 1)
    else:
        if s[1] == 'o':
            tmp = p[1] if p[0] == '0' else str(int(p[1]) ^ 1)
        else:
            tmp = p[1] if p[0] == '1' else str(int(p[1]) ^ 1)
        p = tmp + p

    if p[1] == p[-1] and p[-2] == p[0]:
        ans = ''
        for a in p[1:-1]: ans += 'S' if a == '0' else 'W'
        print(ans)
        exit()

print(-1)

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

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

S1 = "SS"
S2 = "WW"
S3 = "SW"
S4 = "WS"
S_list = [S1, S2, S3, S4]
for S in S_list:
    last = ""
    for i in range(n):
        if i == 0:
            if S[0] == "S":
                if s[i] == "o":
                    last = S[1]
                else:
                    last = "S" if S[1] == "W" else "W"
            else:
                if s[i] == "o":
                    last = "S" if S[1] == "W" else "W"
                else:
                    last = "S" if S[1] == "S" else "W"

        elif 1 <= i < n - 1:
            if S[i] == "S":
                if s[i] == "o":
                    S += S[i-1]
                else:
                    S += "W" if S[i-1] == "S" else "S"
            else:
                if s[i] == "o":
                    S += "S" if S[i-1] == "W" else "W"
                else:
                    S += "S" if S[i-1] == "S" else "W"
        elif i == n - 1:
            if S[i] == "S":
                if s[i] == "o" and S[0] == S[i - 1] and S[i] == last:
                    print(S)
                    exit()
                if s[i] == "x" and S[0] != S[i - 1] and S[i] == last:
                    print(S)
                    exit()
            else:
                if s[i] == "o" and S[0] != S[i - 1] and S[i] == last:
                    print(S)
                    exit()
                if s[i] == "x" and S[0] == S[i - 1] and S[i] == last:
                    print(S)
                    exit()

print(-1)

Solution (Python):
def derive_adjacency(animal, statement, neighbor):
    if animal + statement in ['So', 'Wx']:
        return neighbor
    else:
        return 'W' if neighbor == 'S' else 'S'

input()
S = input()
for a, b in ['SS', 'SW', 'WS', 'WW']:
    tmp = derive_adjacency(a, S[0], b) + a + b
    for c in S[1:]:
        tmp += derive_adjacency(tmp[-1], c, tmp[-2])
    if tmp[:2] == tmp[-2:]:
        print(tmp[1:-1])
        break
else:
    print(-1)

Solution (Python):
def inductive_inference(animal, statement, other):
    if animal == 'S':
        if statement == 'o':
            return other
        else:
            return {'S': 'W', 'W': 'S'}[other]
    else:
        if statement == 'x':
            return other
        else:
            return {'S': 'W', 'W': 'S'}[other]

N = int(input())
S = input()
for a1, a2 in ['SS', 'SW', 'WS', 'WW']:
    assumption = inductive_inference(a1, S[0], a2)  
    a, b = a1, a2
    animals = [assumption, a, b]
    for c in S[1:]:
        a, b = b, inductive_inference(b, c, a)
        animals.append(b)
    if animals[1] == animals[-1] and animals[0] == animals[N]:
        print(''.join(animals[1:-1]))
        exit()
print(-1)

Solution (Python):
import sys
N, = map(int, raw_input().split())
s  = raw_input()
for i in ["SS", "SW", "WS", "WW"]:
  r = [None] * (N+2)
  r[0:2] = list(i)
  for j in range(2, N+2):
    r[j] = "S" if bool(r[j-2]=="S") ^ bool(r[j-1]=="S") ^ bool(s[(j-1)%N]=="o") else "W"
  if r[N:N+2]==list(i):
    print "".join(r[:N])
    sys.exit(0)
print -1

Solution (Python):
def swap(w):
    return("S" if w == "W" else "W")

N = int(input())
s = input()

inits = ["SS","SW","WS","WW"]

for init in inits:
    ans = init
    for i in range(1,N):
        if(ans[i] == 'S'):
            if(s[i] == 'o'):
                ans = ans + ans[i-1]
            else:
                ans = ans + swap(ans[i-1])
        else:
            if(s[i] == 'o'):
                ans = ans + swap(ans[i-1])
            else:
                ans = ans + ans[i-1]

    if(ans[0] == ans[-1]):
        ans = ans[0:N]

        if(ans[0] == 'S'):
            if(s[0] == 'o'):
                val = (ans[1] == ans[-1])
            else:
                val = (ans[1] != ans[-1])
        else:
            if(s[0] == 'o'):
                val = (ans[1] != ans[-1])
            else:
                val = (ans[1] == ans[-1])
        
        if(val):
            break

    ans = "-1"

print(ans)

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

def next(previous, myself, ans):
    if myself == "S":
        if previous == "S":
            if ans == "o":
                return "S"
            else:
                return "W"
        else:
            if ans == "o":
                return "W"
            else:
                return "S"
    else:
        if previous == "S":
            if ans == "o":
                return "W"
            else:
                return "S"
        else:
            if ans == "o":
                return "S"
            else:
                return "W"


ret = "SS"
for i in range(len(s))[1:-1]:
    ret += next(ret[i - 1], ret[i], s[i])
if next(ret[-2], ret[-1], s[-1]) == ret[0] and next(ret[-1], ret[0], s[0]) == ret[1]:
    print(ret)
    quit()

ret = "SW"
for i in range(len(s))[1:-1]:
    ret += next(ret[i - 1], ret[i], s[i])
if next(ret[-2], ret[-1], s[-1]) == ret[0] and next(ret[-1], ret[0], s[0]) == ret[1]:
    print(ret)
    quit()

ret = "WS"
for i in range(len(s))[1:-1]:
    ret += next(ret[i - 1], ret[i], s[i])
if next(ret[-2], ret[-1], s[-1]) == ret[0] and next(ret[-1], ret[0], s[0]) == ret[1]:
    print(ret)
    quit()

ret = "WW"
for i in range(len(s))[1:-1]:
    ret += next(ret[i - 1], ret[i], s[i])
if next(ret[-2], ret[-1], s[-1]) == ret[0] and next(ret[-1], ret[0], s[0]) == ret[1]:
    print(ret)
    quit()

print(-1)

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

def LieDetector(SW):
    SW = [SW[0]]+[0]*N+[SW[1]]
    for i in range(N):
        if s[i]=='o':
            if SW[i-1]=='S':
                if SW[i]=='S':
                    SW[i+1]='S'
                else:
                    SW[i+1]='W'
            else:
                if SW[i]=='S':
                    SW[i+1]='W'
                else:
                    SW[i+1]='S'
        else:
            if SW[i-1]=='S':
                if SW[i]=='S':
                    SW[i+1]='W'
                else:
                    SW[i+1]='S'
            else:
                if SW[i]=='S':
                    SW[i+1]='S'
                else:
                    SW[i+1]='W'
    return SW[0]==SW[N] and SW[N-1]==SW[N+1],SW

SW = [['S','S'],['S','W'],['W','S'],['W','W']]
for x in SW:
    ans = LieDetector(x)
    if ans[0]:
        break
if ans[0]:
    print(''.join(ans[1][:-2]))
else:
    print(-1)

Solution (Python):
N = int(input())
s = input()
s += s[:2]
rec = [[""] * (N + 2) for i in range(4)]
rec[0][0] = "S"
rec[0][1] = "S"
rec[1][0] = "S"
rec[1][1] = "W"
rec[2][0] = "W"
rec[2][1] = "S"
rec[3][0] = "W"
rec[3][1] = "W"
k = -1
for i in range(4):
    for j in range(1, N + 1):
        if s[j] == "o":
            if rec[i][j] == "S":
                rec[i][j + 1] = rec[i][j - 1]
            else:
                if rec[i][j - 1] == "S":
                    rec[i][j + 1] = "W"
                else:
                    rec[i][j + 1] = "S"
        else:
            if rec[i][j] == "S":
                if rec[i][j - 1] == "S":
                    rec[i][j + 1] = "W"
                else:
                    rec[i][j + 1] = "S"
            else:
                rec[i][j + 1] = rec[i][j - 1]

if rec[0][0] == rec[0][N] and rec[0][1] == rec[0][N + 1]:
    k = 0
elif rec[1][0] == rec[1][N] and rec[1][1] == rec[1][N + 1]:
    k = 1
elif rec[2][0] == rec[2][N] and rec[2][1] == rec[2][N + 1]:
    k = 2
elif rec[3][0] == rec[3][N] and rec[3][1] == rec[3][N + 1]:
    k = 3

if k == -1:
    print(-1)
else:
    print("".join(rec[k][:N]))

Solution (Python):
N = int( input())
s = list( input())
I = ['S', 'W']
Flag = False
for x in I:
    for y in I:
        ANS = ['']*N
        ANS[0] = x
        ANS[-1] = y
        for i in range(N-2):
            if ANS[i] == 'S':
                if s[i] == 'o':
                    ANS[i+1] = ANS[i-1]
                else:
                    if ANS[i-1] == 'S':
                        ANS[i+1] = 'W'
                    else:
                        ANS[i+1] = 'S'
            else:
                if 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[-2] == 'S':
            if s[-2] == 'o':
                if ANS[-3] == ANS[-1]:
                    pass
                else:
                    continue
            else:
                if ANS[-3] == ANS[-1]:
                    continue
                else:
                    pass
        else:
            if s[-2] == 'o':
                if ANS[-3] != ANS[-1]:
                    pass
                else:
                    continue
            else:
                if ANS[-3] != ANS[-1]:
                    continue
                else:
                    pass
        if ANS[-1] == 'S':
            if s[-1] == 'o':
                if ANS[-2] == ANS[0]:
                    pass
                else:
                    continue
            else:
                if ANS[-2] == ANS[0]:
                    continue
                else:
                    pass
        else:
            if s[-1] == 'o':
                if ANS[-2] != ANS[0]:
                    pass
                else:
                    continue
            else:
                if ANS[-2] != ANS[0]:
                    continue
                else:
                    pass
        print(''.join(ANS))
        Flag = True
        break
    if Flag:
        break
if Flag == False:
    print(-1)

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

def SorW(i, now, pre):
    if s[i] == "o":
        if now == "S":
            return pre
        elif now == "W":
            return ("S" if pre == "W" else "W")
    elif s[i] == "x":
        if now == "S":
            return ("S" if pre == "W" else "W")
        elif now == "W":
            return pre

if s[0] == "o":
    Ini = ["SSS", "WSW", "SWW", "WWS"]
else:
    Ini = ["SSW", "WSS", "SWS", "WWW"]

for start in Ini:
    pre, now = start[1], start[2]
    ans, i = pre + now, 1
    while i <= N-1:
        next = SorW(i, now, pre)
        now, pre = next, now
        i += 1
        ans += next
    if start[1] == now and start[0] == pre:
        print(ans[:-1])
        break
else:
    print(-1)

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def ok(last, first, S):
    N = len(S)
    ans = [None] * N
    ans[-1], ans[0] = last, first

    for i in range(N):

        if ans[i] == 0:
            if S[i] == "o":
                nex = ans[i - 1]
            else:
                nex = 1 - ans[i - 1]
        else:
            if S[i] == "o":
                nex = 1 - ans[i - 1]
            else:
                nex = ans[i - 1]

        if ans[(i + 1) % N] is None:
            ans[(i + 1) % N] = nex
        else:
            if ans[(i + 1) % N] != nex:
                return None
    return ans


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

    for l, f in [(0, 0), (0, 1), (1, 0), (1, 1)]:
        ans = ok(l, f, S)
        if ans is not None:
            print(*["S" if v == 0 else "W" for v in ans], sep="")
            return
    print(-1)


if __name__ == '__main__':
    main()

Solution (Python):
from itertools import product
 
def solve():
	N = int(input())
	S = [0 if i == 'o' else 1 for i in input()]
 
	for i, j in product(range(2), repeat=2):
		pattern = [i, j] + [0] * (N - 2) 
		for k in range(2, N):
			pattern[k] = (pattern[k - 1] + pattern[k - 2] + S[k-1] ) % 2
		if S[-1] != (pattern[-2] + pattern[-1] + pattern[0]) % 2 : 
			continue
		if S[0] != (pattern[-1] + pattern[0] + pattern[1]) % 2 :
			continue 	
		answer = [ 'S' if pattern[k] == 0 else 'W' for k in range(N)]
		print(''.join(answer))
		return None
	print(-1)
 
if __name__ == '__main__':
	solve()

Solution (Python):
from itertools import product

def solve():
    N = int(input())
    S = [0 if i == 'o' else 1 for i in input()]
    
    for i, j in product(range(2), repeat=2):
        pat = [i, j] + [0]*(N - 2)
        for k in range(2, N):
            pat[k] = (S[k - 1] - (pat[k - 1] + pat[k - 2])) % 2

        if S[-1] != (pat[0] + pat[-1] + pat[-2]) % 2:
            continue
        if S[0] != (pat[1] + pat[0] + pat[-1]) % 2:
            continue

        ans = ['S' if pat[k] == 0 else 'W' for k in range(N)]
        print(''.join(ans))
        return None

    print(-1)

if __name__ == '__main__':
    solve()

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





pat = [(0,0),(0,1),(1,0),(1,1)]
fl = False

for p in pat:
  ans = []
  x_now = p[0]
  x_prev = p[1]
  ans.append(x_now)
  for i in range(N):
    if ST[i] == 'o':
      if x_now == 0:
        x_next = x_prev
      else:
        x_next = 1 - x_prev
    else:
      if x_now == 0:
        x_next = 1 - x_prev
      else:
        x_next = x_prev
  
    
    ans.append(x_next)
    x_prev, x_now = x_now, x_next

  
  if ans[-2] == p[1] and ans[-1] == ans[0]:
    
    fl = True
    
    break

if fl:
  for i in range(N):
    print("S" if ans[i] == 0 else "W", end="")
  print()
else:
  print("-1")

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

def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())

def nex_animal(prev: str, cur:str, say: str) -> str:
    ret_dic = {("S", "S", "o"): "S", ("S", "S", "x"): "W",
               ("S", "W", "o"): "W", ("S", "W", "x"): "S",
               ("W", "S", "o"): "W", ("W", "S", "x"): "S",
               ("W", "W", "o"): "S", ("W", "W", "x"): "W"}
    
    return ret_dic[(prev, cur, say)]

n = ni()
s = lc()
ans = [""]*n

init = {"o":[("S", "S", "S"), ("W","S","W"), ("W","W","S"), ("S","W","W")],
        "x":[("W", "S", "S"), ("S","S","W"), ("S","W","S"), ("W","W","W")]}

exist = False
if s[-1] == "o":
    for a1,a2,a3 in init["o"]:
        ans = [""]*n
        ans[-2], ans[-1], ans[0] = a1, a2, a3
        
        for i in range(n-1):
            if i < n-3:
                ans[i+1] = nex_animal(ans[i-1], ans[i], s[i])
            elif i == n-3:
                if ans[i+1] != nex_animal(ans[i-1], ans[i], s[i]):
                    break
                
            else:
                if ans[i+1] != nex_animal(ans[i-1], ans[i], s[i]):
                    break
                
                else:
                    print("".join(ans))
                    exist = True
                    break
                
        if exist:
            break
                
else:
    for a1,a2,a3 in init["x"]:
        ans = [""]*n
        ans[-2], ans[-1], ans[0] = a1, a2, a3
        
        for i in range(n-1):
            if i < n-3:
                ans[i+1] = nex_animal(ans[i-1], ans[i], s[i])
            
            elif i == n-3:
                if ans[i+1] != nex_animal(ans[i-1], ans[i], s[i]):
                    break
            
            else:
                if ans[i+1] != nex_animal(ans[i-1], ans[i], s[i]):
                    break
                
                else:
                    print("".join(ans))
                    exist = True
                    break
                
        if exist:
            break
    
                    
if not exist:
    print(-1)

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

ans = ["SS", "SW", "WS", "WW"]

for i in range(4):
    for j in range(1, n):
        if s[j] == "o" and ans[i][j] == "S":
            ans[i] += ans[i][j-1]
        elif s[j] == "o" and ans[i][j] == "W":
            ans[i] += "W" if ans[i][j-1] == "S" else "S"
        elif s[j] == "x" and ans[i][j] == "S":
            ans[i] += "W" if ans[i][j-1] == "S" else "S"
        else:
            ans[i] += ans[i][j - 1]

    if ans[i][0] == ans[i][n]:
        if ans[i][n-1] == ans[i][1] and ((s[0] == "o" and ans[i][0] == "S") or (s[0] == "x" and ans[i][0] == "W")):
            print(ans[i][:-1])
            break
        if ans[i][n-1] != ans[i][1] and ((s[0] == "o" and ans[i][0] == "W") or (s[0] == "x" and ans[i][0] == "S")):
            print(ans[i][:-1])
            break
else:
    print(-1)

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

def kakunin(animal):
    for i in range(2,n):
        if s[i-1]=='o':
            if (animal[i-1]==0) and (animal[i-2]==0 ):
                animal[i]=0
            elif (animal[i-1]==1) and (animal[i-2]==0 ):
                animal[i]+=1
            elif (animal[i-1]==0) and (animal[i-2]==1 ):
                animal[i]+=1
            elif (animal[i-1]==1) and (animal[i-2]==1 ):
                animal[i]=0
        else:
            if (animal[i-1]==0) and (animal[i-2]==0 ):
                animal[i]+=1
            elif (animal[i-1]==1) and (animal[i-2]==0 ):
                animal[i]=0
            elif (animal[i-1]==0) and (animal[i-2]==1 ):
                animal[i]=0
            elif (animal[i-1]==1) and (animal[i-2]==1 ):
                animal[i]+=1
    if s[0]=='o':
        if (animal[0]==0):
            if (animal[1]==0) and (animal[-1]==0 ):
                if ((s[-1]=='o')and(animal[-2]==0))or((s[-1]=='x')and(animal[-2]==1)):
                    return True
            elif (animal[1]==1) and (animal[-1]==1):
                if ((s[-1]=='o')and(animal[-2]==1))or((s[-1]=='x')and(animal[-2]==0)):
                    return True
        else:
            if (animal[1]==1) and (animal[-1]==0 ):
                if ((s[-1]=='o')and(animal[-2]==1))or((s[-1]=='x')and(animal[-2]==0)):
                    return True
            elif (animal[1]==0) and (animal[-1]==1):
                if ((s[-1]=='o')and(animal[-2]==0))or((s[-1]=='x')and(animal[-2]==1)):
                    return True
            
            
    else:
        if (animal[0]==0):
            if (animal[1]==1) and (animal[-1]==0 ):
                if ((s[-1]=='o')and(animal[-2]==0))or((s[-1]=='x')and(animal[-2]==1)):
                    return True
            elif (animal[1]==0) and (animal[-1]==1):
                if ((s[-1]=='o')and(animal[-2]==1))or((s[-1]=='x')and(animal[-2]==0)):
                    return True
        else:
            if (animal[1]==1) and (animal[-1]==1):
                if ((s[-1]=='o')and(animal[-2]==0))or((s[-1]=='x')and(animal[-2]==1)):
                    return True
            elif (animal[1]==0) and (animal[-1]==0):
                if ((s[-1]=='o')and(animal[-2]==0))or((s[-1]=='x')and(animal[-2]==1)):
                    return True
    return False
f = [False]     
for i in range(0,4):
    animal = [0]*n
    if i == 1:
        animal[0]+=1
    elif i==2:
        animal[1]+=1
    elif i==3:
        animal[0]+=1
        animal[1]+=1
    f[0]= kakunin(animal) 
    if f[0]==True:
        u = ''
        for j in range(0,n):
            if animal[j]==0:
                u = u+'S'
            else:
                u = u + 'W'
        print(u)
        break

if f[0] == False:
    print(-1)