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



Sample input:
6
ooxoox


Sample output:
SSSWWS

Submission status: Accepted

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

# 0: S, 1: Wとする
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)

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
# from collections import Counter, defaultdict

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')  # 0: S, 1: W
for p in P:
    for i in range(1, n):
        # print(i, p[-1], s[i])
        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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)  # the animal numbered N
    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)

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

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

# n=1,Nが(S,S)(S,W)(W,S)(W,W)の4通りを仮定してずらずら決めていけばよいはず
# S=0, W=1として, 1-xで切り替える
# 最後に矛盾が生じないやつだけえらぶO(1)で判定できそうだけど。。

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
  
    #print(i, ST[i], x_prev, x_now, x_next)
    ans.append(x_next)
    x_prev, x_now = x_now, x_next

  #if ans[-1] == ans[0]:
  if ans[-2] == p[1] and ans[-1] == ans[0]:
    #correct case
    fl = True
    #print(ans)
    break

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, S):
    def f(cnd):
        for i in range(1, N):
            if S[i] == 'o' and cnd[i] == 'S':
                cnd.append(cnd[i-1])
            elif S[i] == 'o' and cnd[i] == 'W':
                cnd.append('S' if cnd[i-1] == 'W' else 'W')
            elif S[i] == 'x' and cnd[i] == 'S':
                cnd.append('S' if cnd[i-1] == 'W' else 'W')
            elif S[i] == 'x' and cnd[i] == 'W':
                cnd.append(cnd[i-1])

        cnd.pop()
        for i in range(N):
            if cnd[i] == 'S' and S[i] == 'o':
                if cnd[(i+1) % N] != cnd[i-1]:
                    return None
            elif cnd[i] == 'S' and S[i] == 'x':
                if cnd[(i+1) % N] == cnd[i-1]:
                    return None
            elif cnd[i] == 'W' and S[i] == 'o':
                if cnd[(i+1) % N] == cnd[i-1]:
                    return None
            elif cnd[i] == 'W' and S[i] == 'x':
                if cnd[(i+1) % N] != cnd[i-1]:
                    return None
        return ''.join(cnd)
    # SS
    ans = f(['S', 'S'])
    if ans:
        return ans

    # SW
    ans = f(['S', 'W'])
    if ans:
        return ans

    # WS
    ans = f(['W', 'S'])
    if ans:
        return ans

    # WW
    ans = f(['W', 'W'])
    if ans:
        return ans

    return -1


def main():
    N = read_int()
    S = read_str()
    print(slv(N, S))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
s += s[0]

is_ans = False
for ans in ['SS', 'SW', 'WS', 'WW']:
    for i in range(1, n+1):
        a_0 = ans[i-1]
        a_1 = ans[i]
        if s[i] == 'o' and a_1 == 'S':
            if a_0 == 'S':
                ans += 'S'
            else:
                ans += 'W'
        elif s[i] == 'x' and a_1 == 'S':
            if a_0 == 'S':
                ans += 'W'
            else:
                ans += 'S'
        elif s[i] == 'o' and a_1 == 'W':
            if a_0 == 'S':
                ans += 'W'
            else:
                ans += 'S'
        else:
            if a_0 == 'S':
                ans += 'S'
            else:
                ans += 'W'
    if ans[-2:] == ans[:2]:
        print(ans[:-2])
        is_ans = True
        break
if not is_ans:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
s = list(input())
c_list = [['W','W'],['W','S'],['S','W'],['S','S']]

for c in c_list:
    for current in range(1,N):
        pre = current-1
        if ((c[current] == 'W' and s[current] == 'o') or (c[current] == 'S' and s[current] == 'x')):
            if(c[pre] == 'W'):
                c.append('S')
            else:
                c.append('W')
        elif ((c[current] == 'W' and s[current] == 'x') or (c[current] == 'S' and s[current] == 'o')):
            c.append(c[pre])
    if(c[0] == c[N]):
        if ((c[0] == 'W' and s[0] == 'o') or (c[0] == 'S' and s[0] == 'x')):
            if(c[1] != c[N-1]):
                print(''.join(c[0:N]))
                quit()
        else:
            if(c[1] == c[N-1]):
                print(''.join(c[0:N]))
                quit()
    """
    if ((c[0] == 'W' and s[0] == 'o') or (c[0] == 'S' and s[0] == 'x')):
        if (c[N-1] != c[1]):
            print(''.join(c[0:N]))
            quit()
    else:
        if (c[N-1] == c[1]):
            print(''.join(c[0:N]))
            quit()
    """
print(-1)