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
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial,sqrt
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
s=SI()
def nex(i):
    if i==n-1:
        i=-1
    if lis[i]==1:
        if s[i]=="o":
            if lis[i-1]==1:
                lis[i+1]=1
            else:
                lis[i+1]=0
        else:
            if lis[i-1]==1:
                lis[i+1]=0
            else:
                lis[i+1]=1
    else:
        if s[i]=="o":
            if lis[i-1]==1:
                lis[i+1]=0
            else:
                lis[i+1]=1
        else:
            if lis[i-1]==0:
                lis[i+1]=0
            else:
                lis[i+1]=1

for i in product([0,1],repeat=2):
    lis=[0 for i in range(n)]
    lis[0],lis[1]=i[0],i[1]
    for i in range(1,n-1):
        nex(i)
    
    x=lis[0]
    y=lis[1]
    nex(n-1)
    nex(0)
    if x==lis[0] and y==lis[1]:
        for i in range(n):
            if lis[i]==1:
                print("S",end="")
            else:
                print("W",end="")
        sys.exit()
print(-1)

Solution (Python):
import sys
from enum import Enum

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from itertools import product
    n = int(readline())
    s = input()

    for a, b in product(("S", "W"), repeat=2):
        kinds = [""] * n
        kinds[0] = a
        kinds[1] = b

        for i in range(1, n - 1):
            if kinds[i] is "S":
                if s[i] == "o":
                    kinds[i + 1] = kinds[i - 1]
                else:
                    if kinds[i - 1] == "S":
                        kinds[i + 1] = "W"
                    else:
                        kinds[i + 1] = "S"
            else:
                if s[i] == "o":
                    if kinds[i - 1] == "S":
                        kinds[i + 1] = "W"
                    else:
                        kinds[i + 1] = "S"
                else:
                    kinds[i + 1] = kinds[i - 1]
        ff = (s[n - 1] == "o") ^ (kinds[n - 1] == "W")
        fs = (s[0] == "o") ^ (kinds[0] == "W")
        f1 = False
        f2 = False
        if ff and (kinds[n - 2] == kinds[0]):
            f1 = True
        if not ff and (kinds[n - 2] != kinds[0]):
            f1 = True
        if fs and (kinds[n - 1] == kinds[1]):
            f2 = True
        if not fs and (kinds[n - 1] != kinds[1]):
            f2 = True

        if f1 and f2:
            res = "".join(kinds)
            print(res)
            sys.exit()
    print(-1)


if __name__ == '__main__':
    main()

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(readline())
    s = input()

    if s[1] == "o":
        first = ((True, True, True),
                 (False, True, False),
                 (True, False, False),
                 (False, False, True))
    else:
        first = ((True, True, False),
                 (False, True, True),
                 (True, False, True),
                 (False, False, False))

    for a, b, c in first:
        kinds = [None] * (n + 1)
        kinds[1] = b
        kinds[0] = a
        kinds[2] = c

        for i in range(2, n):
            if kinds[i]:
                if s[i] == "o":
                    kinds[i + 1] = kinds[i - 1]
                else:
                    kinds[i + 1] = not kinds[i - 1]
            else:
                if s[i] == "o":
                    kinds[i + 1] = not kinds[i - 1]
                else:
                    kinds[i + 1] = kinds[i - 1]

        res = ""
        flag = True
        for i in range(n):
            left = kinds[(i - 1) % n]
            right = kinds[(i + 1) % n]
            res += "S" if kinds[i] else "W"
            if (s[i] == "o" and kinds[i]) or (s[i] == "x" and not kinds[i]):
                if left != right:
                    flag = False
                    break
            else:
                if left == right:
                    flag = False
                    break
        if flag:
            print(res)
            sys.exit()

    print(-1)


if __name__ == '__main__':
    main()

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

s=list(input())

memo_ss = ['S','S']
memo_sw = ['S','W']
memo_ws = ['W','S']
memo_ww = ['W','W']

def check(memo_ss):
    for i in range(1,n):
        if s[i] == 'o' :
            if memo_ss[i] == 'S':
                if memo_ss[i-1] == 'S':
                    memo_ss.append('S')
                elif memo_ss[i-1] == 'W':
                    memo_ss.append('W')
            elif memo_ss[i] == 'W':
                if memo_ss[i-1] == 'S':
                    memo_ss.append('W')
                elif memo_ss[i-1] == 'W':
                    memo_ss.append('S')
        elif s[i] == 'x' :
            if memo_ss[i] == 'S':
                if memo_ss[i-1] == 'S':
                    memo_ss.append('W')
                elif memo_ss[i-1] == 'W':
                    memo_ss.append('S')
            elif memo_ss[i] == 'W':
                if memo_ss[i-1] == 'S':
                    memo_ss.append('S')
                elif memo_ss[i-1] == 'W':
                    memo_ss.append('W')
    return memo_ss

memo_ss = check(memo_ss)
memo_sw = check(memo_sw)
memo_ws = check(memo_ws)
memo_ww = check(memo_ww)

for memo in [memo_ss,memo_sw,memo_ws,memo_ww]:
    if memo[0] == memo[-1]:
        if s[0] == 'o':
            if memo[0] == 'S' and memo[1] == memo[n-1]:
                print(''.join(map(str,memo[:n])))
                exit()
            elif memo[0] == 'W' and memo[1] != memo[n-1]:
                print(''.join(map(str,memo[:n])))
                exit()
        elif s[0] == 'x':
                if memo[0] == 'S' and memo[1] != memo[n-1]:
                    print(''.join(map(str,memo[:n])))
                    exit()
                elif memo[0] == 'W' and memo[1] == memo[n-1]:
                    print(''.join(map(str,memo[:n])))
                    exit()
print(-1)

Solution (Python):
def main():
    N = int(input())
    S = input()
    M = [{'o': 0, 'x': 1}, {'o': 1, 'x': 0}]
    for s1, sn in [(0, 0), (0, 1), (1, 0), (1, 1)]:
        T = [s1]
        p, pp = s1, sn
        for s in S:
            pp, p = p, M[p ^ pp][s]
            T.append(p)
        if (s1, sn) == (p, pp):
            return T[:-1]
    return None

T = main()
if not T:
    print(-1)
else:
    print(''.join('S' if s == 0 else 'W' for s in T))

Solution (Python):
def main():
    N = int(input())
    s = input()
    
    for i in [[0,0],[0,1],[1,0],[1,1]]:
        t = i[:]
        for j in s[1:]:
            if t[-1] == 0:
                if j == 'o':
                    t.append(t[-2])
                else:
                    t.append(1-t[-2])
            elif j == 'o':
                t.append(1-t[-2])
            else:
                t.append(t[-2])
        if t[-1] == t[0]:
            if s[0] == 'o' and t[0] == 0 and t[1] != t[-2]:
                continue
            if s[0] == 'o' and t[0] == 1 and t[1] == t[-2]:
                continue
            if s[0] == 'x' and t[0] == 0 and t[1] == t[-2]:
                continue
            if s[0] == 'x' and t[0] == 1 and t[1] != t[-2]:
                continue
            print(''.join(['S' if i == 0 else 'W' for i in t[:-1]]))
            return
    print(-1)
    return
main()

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

P = [
    ['S', 'S'],['S', 'W'],
    ['W', 'S'],['W', 'W']
]

if S[0] == 'o':
    n1 = ['S','W','W','S']
else:
    n1 = ['W','S','S','W']

for idx, p in enumerate(P):
    for i in range(1, n-1):
        if S[i] == 'o':
            if p[i] == 'S':
                p.append(p[i-1])
            else:
                p.append('W' if p[i-1] == 'S' else 'S')
        else:
            if p[i] == 'S':
                p.append('W' if p[i-1] == 'S' else 'S')
            else:
                p.append(p[i-1])
    if n1[idx] == p[-1]:
        if  (S[-1] == 'o' and n1[idx] == 'S' and p[0] == p[-2]) or\
            (S[-1] == 'o' and n1[idx] == 'W' and p[0] != p[-2]) or\
            (S[-1] == 'x' and n1[idx] == 'W' and p[0] == p[-2]) or\
            (S[-1] == 'x' and n1[idx] == 'S' and p[0] != p[-2]):
                print(''.join(p))
                break
else:
    print(-1)

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

assignable = False

for k in range(4):
    res = [None for _ in range(N)]
    res[0] = k % 2
    res[-1] = k // 2
    for i in range(N - 2):
        if S[i] == 'o':
            if res[i] == 0:
                res[i + 1] = res[i - 1]
            else:
                res[i + 1] = 1 - res[i - 1]
        else:
            if res[i] == 0:
                res[i + 1] = 1 - res[i - 1]
            else:
                res[i + 1] = res[i - 1]
    if S[-2] == 'o':
        if res[-2] == 0:
            if res[-1] != res[-3]:
                continue
        else:
            if res[-1] != 1 - res[-3]:
                continue
    else:
        if res[-2] == 0:
            if res[-1] != 1 - res[-3]:
                continue
        else:
            if res[-1] != res[-3]:
                continue
    if S[-1] == 'o':
        if res[-1] == 0:
            if res[0] == res[-2]:
                assignable = True
        else:
            if res[0] == 1 - res[-2]:
                assignable = True
    else:
        if res[-1] == 0:
            if res[0] == 1 - res[-2]:
                assignable = True
        else:
            if res[0] == res[-2]:
                assignable = True
    if assignable:
        for i in range(N):
            if res[i]:
                print('W', end='')
            else:
                print('S', end='')
        break
else:
    print(-1)

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

X = [''] * (N + 1)
X[0] = 'S'
X[1] = 'S'
for i in range(1, N):
    if X[i] == 'S':
        if S[i] == 'o':
            X[i + 1] = X[i - 1]
        else:
            if X[i - 1] == 'S':
                X[i + 1] = 'W'
            else:
                X[i + 1] = 'S'
    else:
        if S[i] == 'x':
            X[i + 1] = X[i - 1]
        else:
            if X[i - 1] == 'S':
                X[i + 1] = 'W'
            else:
                X[i + 1] = 'S'
if X[-1] == 'S':
    if (X[-2] == 'S' and S[0] == 'o') or (X[-2] == 'W' and S[0] == 'x'):
        X.pop()
        for x in X:
            print(x, end='')
        print('')
        sys.exit()

X = [''] * (N + 1)
X[0] = 'S'
X[1] = 'W'
for i in range(1, N):
    if X[i] == 'S':
        if S[i] == 'o':
            X[i + 1] = X[i - 1]
        else:
            if X[i - 1] == 'S':
                X[i + 1] = 'W'
            else:
                X[i + 1] = 'S'
    else:
        if S[i] == 'x':
            X[i + 1] = X[i - 1]
        else:
            if X[i - 1] == 'S':
                X[i + 1] = 'W'
            else:
                X[i + 1] = 'S'
if X[-1] == 'S':
    if (X[-2] == 'S' and S[0] == 'x') or (X[-2] == 'W' and S[0] == 'o'):
        X.pop()
        for x in X:
            print(x, end='')
        print('')
        sys.exit()

X = [''] * (N + 1)
X[0] = 'W'
X[1] = 'S'
for i in range(1, N):
    if X[i] == 'S':
        if S[i] == 'o':
            X[i + 1] = X[i - 1]
        else:
            if X[i - 1] == 'S':
                X[i + 1] = 'W'
            else:
                X[i + 1] = 'S'
    else:
        if S[i] == 'x':
            X[i + 1] = X[i - 1]
        else:
            if X[i - 1] == 'S':
                X[i + 1] = 'W'
            else:
                X[i + 1] = 'S'
if X[-1] == 'W':
    if (X[-2] == 'S' and S[0] == 'x') or (X[-2] == 'W' and S[0] == 'o'):
        X.pop()
        for x in X:
            print(x, end='')
        print('')
        sys.exit()

X = [''] * (N + 1)
X[0] = 'W'
X[1] = 'W'
for i in range(1, N):
    if X[i] == 'S':
        if S[i] == 'o':
            X[i + 1] = X[i - 1]
        else:
            if X[i - 1] == 'S':
                X[i + 1] = 'W'
            else:
                X[i + 1] = 'S'
    else:
        if S[i] == 'x':
            X[i + 1] = X[i - 1]
        else:
            if X[i - 1] == 'S':
                X[i + 1] = 'W'
            else:
                X[i + 1] = 'S'
if X[-1] == 'W':
    if (X[-2] == 'S' and S[0] == 'o') or (X[-2] == 'W' and S[0] == 'x'):
        X.pop()
        for x in X:
            print(x, end='')
        print('')
        sys.exit()

print('-1')

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7






def rand_letter(size):
    ascii_original='ox'
    digits_original='01'

    digits='0123456789'
    ascii_lowercase='abcdefghijklmnopqrstuvwxyz'
    ascii_uppercase='ABCDEFGHIJKLMNOPQRSTUVWXYZ'

    
    psuedo = ascii_original

    return ''.join([random.choice(psuedo) for i in range(size)])

N = getN()
S = input()
lista = ['SS', 'SW', 'WS', 'WW']

def judge(string):
    alta = copy.deepcopy(string)
    for i in range(1, N - 1):
        if alta[-1] == "S" and alta[-2] == "S":
            if S[i] == "o":
                alta.append('S')
            else:
                alta.append("W")
        elif alta[-1] == "S" and alta[-2] == "W":
            if S[i] == "o":
                alta.append('W')
            else:
                alta.append("S")
        elif alta[-1] == "W" and alta[-2] == "S":
            if S[i] == "o":
                alta.append('W')
            else:
                alta.append("S")
        else:
            if S[i] == "o":
                alta.append('S')
            else:
                alta.append("W")

    return alta

def judge_2(before, now, after, j):
    if now == "S":
        if j == "o":
            if before == after:
                return True

        else:
            if before != after:
                return True

    else:
        if j == "o":
            if before != after:
                return True

        else:
            if before == after:
                return True
    return False

for i in lista:
    opt = judge(list(i))
    if judge_2(opt[-2], opt[-1], opt[0], S[-1]) and judge_2(opt[-1], opt[0], opt[1], S[0]):
        print(''.join(opt))
        exit()
print(-1)

Solution (Python):
N=int(input())
S=input()
S=[1 if S[i]=='o' else -1 for i in range(N)]


tmp=[0]*N
tmp[0]=1
if S[0]==1: 
  
  tmp[1]=1
  tmp[N-1]=1
  
  for i in range(2, N-1):
    tmp[i]=tmp[i-1]*tmp[i-2]*S[i-1]
    
  if tmp[N-1]==tmp[N-2]*tmp[N-3]*S[N-2] and tmp[0]==tmp[N-1]*tmp[N-2]*S[N-1]:
    ans=['S' if tmp[i]==1 else 'W' for i in range(N)]
    print(''.join(ans))
    exit()
    
  
  tmp[1]=-1
  tmp[N-1]=-1
  
  for i in range(2, N-1):
    tmp[i]=tmp[i-1]*tmp[i-2]*S[i-1]
    
  if tmp[N-1]==tmp[N-2]*tmp[N-3]*S[N-2] and tmp[0]==tmp[N-1]*tmp[N-2]*S[N-1]:
    ans=['S' if tmp[i]==1 else 'W' for i in range(N)]
    print(''.join(ans))
    exit()
  
else: 
  
  tmp[1]=1
  tmp[N-1]=-1
  
  for i in range(2, N-1):
    tmp[i]=tmp[i-1]*tmp[i-2]*S[i-1]
    
  if tmp[N-1]==tmp[N-2]*tmp[N-3]*S[N-2] and tmp[0]==tmp[N-1]*tmp[N-2]*S[N-1]:
    ans=['S' if tmp[i]==1 else 'W' for i in range(N)]
    print(''.join(ans))
    exit()
  
  
  tmp[1]=-1
  tmp[N-1]=1
  
  for i in range(2, N-1):
    tmp[i]=tmp[i-1]*tmp[i-2]*S[i-1]
    
  if tmp[N-1]==tmp[N-2]*tmp[N-3]*S[N-2] and tmp[0]==tmp[N-1]*tmp[N-2]*S[N-1]:
    ans=['S' if tmp[i]==1 else 'W' for i in range(N)]
    print(''.join(ans))
    exit()
  

tmp=[0]*N
tmp[0]=-1
if S[0]==-1: 
  
  tmp[1]=1
  tmp[N-1]=1
  
  for i in range(2, N-1):
    tmp[i]=tmp[i-1]*tmp[i-2]*S[i-1]
    
  if tmp[N-1]==tmp[N-2]*tmp[N-3]*S[N-2] and tmp[0]==tmp[N-1]*tmp[N-2]*S[N-1]:
    ans=['S' if tmp[i]==1 else 'W' for i in range(N)]
    print(''.join(ans))
    exit()
    
  
  tmp[1]=-1
  tmp[N-1]=-1
  
  for i in range(2, N-1):
    tmp[i]=tmp[i-1]*tmp[i-2]*S[i-1]
    
  if tmp[N-1]==tmp[N-2]*tmp[N-3]*S[N-2] and tmp[0]==tmp[N-1]*tmp[N-2]*S[N-1]:
    ans=['S' if tmp[i]==1 else 'W' for i in range(N)]
    print(''.join(ans))
    exit()
  
else: 
  
  tmp[1]=1
  tmp[N-1]=-1
  
  for i in range(2, N-1):
    tmp[i]=tmp[i-1]*tmp[i-2]*S[i-1]
    
  if tmp[N-1]==tmp[N-2]*tmp[N-3]*S[N-2] and tmp[0]==tmp[N-1]*tmp[N-2]*S[N-1]:
    ans=['S' if tmp[i]==1 else 'W' for i in range(N)]
    print(''.join(ans))
    exit()
  
  
  tmp[1]=-1
  tmp[N-1]=1
  
  for i in range(2, N-1):
    tmp[i]=tmp[i-1]*tmp[i-2]*S[i-1]
    
  if tmp[N-1]==tmp[N-2]*tmp[N-3]*S[N-2] and tmp[0]==tmp[N-1]*tmp[N-2]*S[N-1]:
    ans=['S' if tmp[i]==1 else 'W' for i in range(N)]
    print(''.join(ans))
    exit()
  
print(-1)

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache





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






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


def gcd(a, b):
    while b: a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





N = I()
S = S()

cases = [(1,1),(1,0),(0,1),(0,0)]

flag = 0
for prev,now in cases:
  prev_0 = prev
  ans = [now]
  for s in S[:-1]:
    if (now == 1 and s == "o" ) or (now == 0 and s == "x"):
      ans.append(prev)
    else:
      ans.append(1-prev)
    prev = now
    now = ans[-1]
  if ans[-1] == prev_0 == 1:
    if (S[-1] == "o" and ans[0]==ans[-2]) or (S[-1] == "x" and ans[0]!=ans[-2]):
      flag = 1
      break
  if ans[-1] == prev_0 == 0:
    if (S[-1] == "o" and ans[0]!=ans[-2]) or (S[-1] == "x" and ans[0]==ans[-2]):
      flag = 1
      break
if flag:
  print(*["S" if a else "W" for a in ans],sep="")
else:
  print(-1)

Solution (Python):
def main():
    n = int(input())
    s = input()
    for t in ["SS", "SW", "WS", "WW"]:
        for i in s[1:-1]:
            if t[-1] == "S":
                if i == "o":
                    t += t[-2]
                else:
                    t += "S" if t[-2] == "W" else "W"
            else:
                if i == "x":
                    t += t[-2]
                else:
                    t += "S" if t[-2] == "W" else "W"
        
        if t[0] == "S":
            if s[0] == "o":
                f = t[-1] == t[1]
            else:
                f = t[-1] != t[1]
        else:
            if s[0] == "x":
                f = t[-1] == t[1]
            else:
                f = t[-1] != t[1]

        
        if t[-1] == "S":
            if s[-1] == "o":
                g = t[-2] == t[0]
            else:
                g = t[-2] != t[0]
        else:
            if s[-1] == "x":
                g = t[-2] == t[0]
            else:
                g = t[-2] != t[0]

        if f and g:
            print(t)
            break

    else:
        print(-1)


if __name__ == '__main__':
    main()

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

    def get_next(p_prev, prev, prev_mark):
        anms = ["S", "W"]
        p_prev_idx = anms.index(p_prev)
        if prev_mark == "o":
            return anms[p_prev_idx] if prev== "S" else anms[(p_prev_idx+1)%2]
        else:
            return anms[(p_prev_idx+1)%2] if prev == "S" else anms[p_prev_idx]

    out = ["S", "S"]
    for i in range(2, N):
        out.append(get_next(out[i-2], out[i-1], S[i-1]))
    if get_next(out[-2], out[-1], S[-1]) == out[0] and get_next(out[-1], out[0], S[0]) == out[1]:
        print("".join(out))
        return

    out = ["S", "W"]
    for i in range(2, N):
        out.append(get_next(out[i-2], out[i-1], S[i-1]))
    if get_next(out[-2], out[-1], S[-1]) == out[0] and get_next(out[-1], out[0], S[0]) == out[1]:
        print("".join(out))
        return
    
    out = ["W", "S"]
    for i in range(2, N):
        out.append(get_next(out[i-2], out[i-1], S[i-1]))
    if get_next(out[-2], out[-1], S[-1]) == out[0] and get_next(out[-1], out[0], S[0]) == out[1]:
        print("".join(out))
        return
    
    out = ["W", "W"]
    for i in range(2, N):
        out.append(get_next(out[i-2], out[i-1], S[i-1]))
    if get_next(out[-2], out[-1], S[-1]) == out[0] and get_next(out[-1], out[0], S[0]) == out[1]:
        print("".join(out))
        return
    
    print(-1)

if __name__ == '__main__':
    resolve()

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



for ans0, ans_last in [ ('S','S'),('S','W'),('W','S'),('W','W') ]:
    ans = ['tbd']*n
    ans[0] = ans0
    ans[-1] = ans_last
        
    for i in range(n-2):
        if (s[i] == 'o' and ans[i] == 'S') or (s[i] == 'x' and ans[i] == 'W'):
            ans[i+1] = ans[i-1]
        else:
            ans[i+1] = 'S' if ans[i-1] == 'W' else 'W'

    last_two_flag = False
    last_one_flag = False

    if (s[n-2] == 'o' and ans[n-2] == 'S') or (s[n-2] == 'x' and ans[n-2] == 'W'):
        if ans[n-3] == ans[n-1]:
            last_two_flag = True
    else:
        if ans[n-3] != ans[n-1]:
            last_two_flag = True


    if (s[n-1] == 'o' and ans[n-1] == 'S') or (s[n-1] == 'x' and ans[n-1] == 'W'):
        if ans[n-2] == ans[0]:
            last_one_flag = True
    else:
        if ans[n-2] != ans[0]:
            last_one_flag = True

    if last_one_flag and last_two_flag:
        print(''.join(ans))
        exit()

print(-1)

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

for p1,p2 in [(True,True),(True,False),(False,True),(False,False)]:
  ans = []
  ans.extend([p1,p2])  
  for i in range(1,n-1):
    if ans[i] == True:
      if s[i]=='o':
        ans.append(ans[-2])
      else:
        ans.append(not ans[-2])
    else:
      if s[i]=='o':
        ans.append(not ans[-2])
      else:
        ans.append(ans[-2])
  
  f = False
  if ans[-1]==True:
    if s[-1]=='o' and ans[-2]==ans[0]:        
      f = True
    if s[-1]=='x' and ans[-2]!=ans[0]:      
      f = True
  else:
    if s[-1]=='o' and ans[-2]!=ans[0]:
      f = True
    if s[-1]=='x' and ans[-2]==ans[0]:
      f = True
      
  if f:    
    if ans[0] and s[0]=='o' and ans[1] == ans[-1]:
      break
    if ans[0] and s[0]=='x' and ans[1] != ans[-1]:
      break
    if not ans[0] and s[0]=='o' and ans[1] != ans[-1]:
      break
    if not ans[0] and s[0]=='x' and ans[1] == ans[-1]:
      break
  ans = []
  
if len(ans)>0:
  print(''.join(['S' if a else 'W' for a in ans] ))
else:
  print(-1)

Solution (Python):
n=int(input())
s=list(input())
def func(a,b):
  ans=[a,b] 
  for i in range(1,n):
    if s[i]=='o':
      if ans[-1]:
        ans.append(ans[-2])
      else:
        ans.append(not ans[-2])
    else:
      if ans[-1]:
        ans.append(not ans[-2])
      else:
        ans.append(ans[-2])
  if ans[0]==ans[-1]:
    flg=False
    if s[0]=='o' and a and ans[1]==ans[-2]:
      flg=True
    elif s[0]=='o' and not a and ans[1]!=ans[-2]:
      flg=True
    elif s[0]=='x' and a and ans[1]!=ans[-2]:
      flg=True
    elif s[0]=='x' and not a and ans[1]==ans[-2]:
      flg=True
    if flg:
      print(''.join(['S' if ansi else 'W' for ansi in ans[:-1]]))
      exit()
func(True,True)
func(True,False)
func(False,True)
func(False,False)
print(-1)

Solution (Python):



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

d = {"S": "W", "W": "S"}


def f(x, y, i, j):
    if y == "S":
        if s[j] == "o":
            return x
        else:
            return d[x]
    else:
        if s[j] == "o":
            return d[x]
        else:
            return x


def solve(t):
    l = [None] * N
    l[0], l[1] = t
    l[-1] = f(l[1], l[0], 1, 0)
    for i in range(2, N - 1):
        l[i] = f(l[i - 2], l[i - 1], i - 2, i - 1)
    x = f(l[-3], l[-2], -3, -2)
    y = f(l[-2], l[-1], -2, -1)
    if x == l[-1] and y == l[0]:
        return l
    else:
        return None
        

def main():
    l = [("S", "S"), ("S", "W"), ("W", "S"), ("W", "W")]
    ans = None
    for t in l:
        ans = solve(t)
        if ans is not None:
            break
    if ans is None:
        print(-1)
    else:
        print("".join(ans))
    

if __name__ == "__main__":
    main()