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: Time Limit Exceeded

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

for a in [["S","S"],["S","W"],["W","S"],["W","W"]]:
    ans = a[0]+a[1]
    for i in range(1,N-1):
        if S[i] == "o":
            if ans[i] == "S" and ans[i-1] == "S":
                ans += "S"
            elif ans[i] == "S" and ans[i-1] == "W":
                ans += "W"
            elif ans[i] =="W" and ans[i-1] == "S":
                ans += "W"
            elif ans[i] =="W" and ans[i-1] == "W":
                ans += "S"
        elif S[i] == "x":
            if ans[i] == "S" and ans[i-1] == "S":
                ans += "W"
            elif ans[i] == "S" and ans[i-1] == "W":
                ans += "S"
            elif ans[i] =="W" and ans[i-1] == "S":
                ans += "S"
            elif ans[i] =="W" and ans[i-1] == "W":
                ans += "W"

    if S[-1] == "o":
        if ans[-1] == "S":
            if ans[-2] != ans[0]:
                ans = ""
        else:
            if ans[-2] == ans[0]:
                ans = ""
    elif S[-1] == "x":
        if ans[-1] == "S":
            if ans[-2] == ans[0]:
                ans = ""
        else:
            if ans[-2] != ans[0]:
                ans = ""
    
    if ans == "":
        continue

    if S[0] == "o":
        if ans[0] == "S":
            if ans[1] != ans[-1]:
                ans = ""
        else:
            if ans[1] == ans[-1]:
                ans = ""
    elif S[0] == "x":
        if ans[0] == "S":
            if ans[1] == ans[-1]:
                ans = ""
        else:
            if ans[1] != ans[-1]:
                ans = ""
    
    if ans != "":
        print(ans)
        exit()
print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
cases = ['SS', 'SW', 'WS', 'WW']
ans = '-1'
for i in range(0, 4):
	animals = cases[i][0] + cases[i][1]
	for j in range(1, N-1):
		if S[j] == 'o':
			if animals[j] == 'S':
				if animals[j-1] == 'S': animals = animals + 'S'
				else: animals = animals = animals + 'W'
			else:
				if animals[j-1] == 'S': animals = animals + 'W'
				else: animals = animals + 'S'
		else:
			if animals[j] == 'S':
				if animals[j-1] == 'S': animals = animals + 'W'
				else: animals = animals + 'S'
			else:
				if animals[j-1] == 'S': animals = animals + 'S'
				else: animals = animals + 'W'
	flag1 = False
	flag2 = False
	if S[0] == 'o':
		if animals[0] == 'S' and animals[-1] == animals[1]:
			flag1 = True
		elif animals[0] == 'W' and animals[-1] != animals[1]:
			flag1 = True
	else:
		if animals[0] == 'S' and animals[-1] != animals[1]:
			flag1 = True
		elif animals[0] == 'W' and animals[-1] == animals[1]:
			flag1 = True

	if S[-1] == 'o':
		if animals[-1] == 'S' and animals[-2] == animals[0]:
			flag2 = True
		elif animals[-1] == 'W' and animals[-2] != animals[0]:
			flag2 = True
	else:
		if animals[-1] == 'S' and animals[-2] != animals[0]:
			flag2 = True
		elif animals[-1] == 'W' and animals[-2] == animals[0]:
			flag2 = True
	if flag1 and flag2:
		ans = animals
		break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import math

N = int(input())
s = input()
for t in ["SS","SW","WS","WW"]:
    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: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
a = "SS"
b = "SW"
c = "WS"
d = "WW"

#ox
ans = "-1"

for animals in [a,b,c,d]:
    for say in s:
        if (animals[-1] == "S" and say == "o" ) or (animals[-1] == "W" and say == "x"):
            animals += animals[-2]
        elif (animals[-1] == "S" and say == "x"):
            if animals[-2] == "S":
                animals += "W"
            else:
                animals += "S"
        elif (animals[-1] == "W" and say == "o"):
            if animals[-2] == "S":
                animals += "W"
            else:
                animals += "S"
    if animals[:2] == animals[-2:]:
        ans  =animals[1:-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:


def change(s, i, n, ans):
  if(i == n-1):
    for j in range(-1, 2):
      if(j == 1 and s[0] == "o"):s[0] = "x"

      elif(j == 1 and s[0] != "o"): s[0] = "o"
      elif(s[i + j] == "o"): s[i + j] = "x"
      else: s[i + j] = "o"
  else:
    for j in range(-1, 2):
      if(s[i + j] == "o"): s[i + j] = "x"
      else: s[i + j] = "o"

  if(ans[i] == "S"):
    ans[i] = "W"
  else:
    ans[i] = "S"
  return (s, ans)

def check(a, s, ans, count, n):
  if(a == s):
    return ans
  if(count == 0):
    return -1
  tmp = [change(list(a), i, n, list(ans)) for i in range(n)]
  c = [check(list(tmp[i][0]), s, list(tmp[i][1]), count-1, n) for i in range(n)]
  while(-1 in c):
    c.remove(-1)
  if(len(c) >= 1):
    return c[0]
  else:
    return -1

N = int(input())
r = input()
s = [r[i] for i in range(N)]

a = ["o" for _ in range(N)]
ans = ["S" for _ in range(N)]

if(a == s):
  print(''.join(ans))
else:
  c = check(a, s, ans, N, N)
  if(c == -1):
    print(-1)
  else:
    print(''.join(c))

Submission status: Time Limit Exceeded

Solution code:
import sys

n = int(input())
inputlst = list(input())

for i in range(n):
    if inputlst[i] == "o":
        inputlst[i] = int(1)
    else:
        inputlst[i] = int(-1)


def mainSS(k):
    if k == 1:
        return 1
    elif k == 2:
        return 1
    else:
        return mainSS(k-2)*mainSS(k-1)*inputlst[k-2]

def mainSW(k):
    if k == 1:
        return 1
    elif k == 2:
        return -1
    else:
        return mainSW(k-2)*mainSW(k-1)*inputlst[k-2]

def mainWS(k):
    if k == 1:
        return -1
    elif k == 2:
        return 1
    else:
        return mainWS(k-2)*mainWS(k-1)*inputlst[k-2]

def mainWW(k):
    if k == 1:
        return -1
    elif k == 2:
        return -1
    else:
        return mainWW(k-2)*mainWW(k-1)*inputlst[k-2]

l = [mainSS(k) for k in range(1,n+1)]
if inputlst[0] == mainSS(n)*mainSS(1)*mainSS(2):
    if inputlst[n-1] == mainSS(n-1)*mainSS(n)*mainSS(1):
        for i in range(n):
            if l[i] == 1:
                l[i] = "S"
            else:
                l[i] = "W"
        for j in range(n):
            print(l[j], end="")
        sys.exit()


l = [mainSW(k) for k in range(1,n+1)]
if inputlst[0] == mainSW(n)*mainSW(1)*mainSW(2):
    if inputlst[n-1] == mainSW(n-1)*mainSW(n)*mainSW(1):
        for i in range(n):
            if l[i] == 1:
                l[i] = "S"
            else:
                l[i] = "W"
        for j in range(n):
            print(l[j], end="")
        sys.exit()


l = [mainWS(k) for k in range(1,n+1)]
if inputlst[0] == mainWS(n)*mainWS(1)*mainWS(2):
    if inputlst[n-1] == mainWS(n-1)*mainWS(n)*mainWS(1):
        for i in range(n):
            if l[i] == 1:
                l[i] = "S"
            else:
                l[i] = "W"
        for j in range(n):
            print(l[j], end="")
        sys.exit()


l = [mainWW(k) for k in range(1,n+1)]
if inputlst[0] == mainWW(n)*mainWW(1)*mainWW(2):
    if inputlst[n-1] == mainWW(n-1)*mainWW(n)*mainWW(1):
        for i in range(n):
            if l[i] == 1:
                l[i] = "S"
            else:
                l[i] = "W"
        for j in range(n):
            print(l[j], end="")
    else:
        print("-1")

Submission status: Time Limit Exceeded

Solution code:
import sys

n = int(input())
inputlst = list(input())

for i in range(n):
    if inputlst[i] == "o":
        inputlst[i] = int(1)
    else:
        inputlst[i] = int(-1)


def mainSS(k):
    if k == 1:
        return 1
    elif k == 2:
        return 1
    else:
        return mainSS(k-2)*mainSS(k-1)*inputlst[k-2]

def mainSW(k):
    if k == 1:
        return 1
    elif k == 2:
        return -1
    else:
        return mainSW(k-2)*mainSW(k-1)*inputlst[k-2]

def mainWS(k):
    if k == 1:
        return -1
    elif k == 2:
        return 1
    else:
        return mainWS(k-2)*mainWS(k-1)*inputlst[k-2]

def mainWW(k):
    if k == 1:
        return -1
    elif k == 2:
        return -1
    else:
        return mainWW(k-2)*mainWW(k-1)*inputlst[k-2]

l = [mainSS(k) for k in range(1,n+1)]
if inputlst[0] == mainSS(n)*mainSS(1)*mainSS(2):
    if inputlst[n-1] == mainSS(n-1)*mainSS(n)*mainSS(1):
        for i in range(n):
            if l[i] == 1:
                l[i] = "S"
            else:
                l[i] = "W"
        for j in range(n):
            print(l[j], end="")
        sys.exit()
    else:
        pass
else:
    pass

l = [mainSW(k) for k in range(1,n+1)]
if inputlst[0] == mainSW(n)*mainSW(1)*mainSW(2):
    if inputlst[n-1] == mainSW(n-1)*mainSW(n)*mainSW(1):
        for i in range(n):
            if l[i] == 1:
                l[i] = "S"
            else:
                l[i] = "W"
        for j in range(n):
            print(l[j], end="")
        sys.exit()
    else:
        pass
else:
    pass

l = [mainWS(k) for k in range(1,n+1)]
if inputlst[0] == mainWS(n)*mainWS(1)*mainWS(2):
    if inputlst[n-1] == mainWS(n-1)*mainWS(n)*mainWS(1):
        for i in range(n):
            if l[i] == 1:
                l[i] = "S"
            else:
                l[i] = "W"
        for j in range(n):
            print(l[j], end="")
        sys.exit()
    else:
        pass
else:
    pass

l = [mainWW(k) for k in range(1,n+1)]
if inputlst[0] == mainWW(n)*mainWW(1)*mainWW(2):
    if inputlst[n-1] == mainWW(n-1)*mainWW(n)*mainWW(1):
        for i in range(n):
            if l[i] == 1:
                l[i] = "S"
            else:
                l[i] = "W"
        for j in range(n):
            print(l[j], end="")
    else:
        print("-1")

Submission status: Time Limit Exceeded

Solution code:
d={"o":1,"x":-1}
N=int(raw_input())
s=raw_input()
s+=s[0]
def ans(a):
    for i in range(1,N+1):
        a=a+[a[i]*a[i-1]*d[s[i]]]
    if a[:2]==a[-2:]:
        return a[:-2]
    else:
        return []
for ini in [[1,1],[1,-1],[-1,1],[-1,-1]]:
    a=ans(ini)
    if a:
        print "".join(['S' if x>0 else 'W' for x in a])
        break
else:
    print "-1"

Submission status: Time Limit Exceeded

Solution code:
from logging import getLogger, StreamHandler, DEBUG
logger = getLogger(__name__)
handler = StreamHandler()
handler.setLevel(DEBUG)
logger.setLevel(DEBUG)
logger.addHandler(handler)

import sys

def match(s, sw, index, N):
    if s[index]=='o':
        if sw[index]=='S':
            return sw[(index - 1 + N) % N] == sw[(index + 1 + N) % N]
        else:
            return sw[(index - 1 + N) % N] != sw[(index + 1 + N) % N]
    else:
        if sw[index]=='W':
            return sw[(index - 1 + N) % N] == sw[(index + 1 + N) % N]
        else:
            return sw[(index - 1 + N) % N] != sw[(index + 1 + N) % N]


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


	 
    

for j in range(4):
    sw = ""
    if j%2==0:
        sw+= 'S'
    else:
        sw+= 'W'

    if (int(j/2))%2==0:
        sw+= 'S'
    else:
        sw+= 'W'
    logger.debug(sw)


    for i in range(N-2):
        if (sw[i+1] == 'S' and s[i+1] == 'o') or (sw[i+1] == 'W' and s[i+1] == 'x'):
            sw+=sw[i]
        else:
            if sw[i] == 'S':
                sw+='W'
            else:
                sw+='S'

        logger.debug(sw)

    if match(s,sw,0,N) and match(s,sw,N-1,N):
        print(sw)
        sys.exit()

print(-1)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
# Sheep:0, Wolf:1
for s in range(2):
    for t in range(2):
        T = [None] * N
        T[0] = s
        T[1] = t
        for j in range(1, N-1):
            if (T[j] == 0) == (S[j] == 'o'):
                T[j + 1] = T[j-1]
            else:
                T[j + 1] = 1 - T[j-1]

        if ((T[N - 1] == 0) == (S[N - 1] == 'o')) == (T[N - 2] == T[0]):
            if ((s == 0) == (S[0] == 'o')) == (T[N - 1] == t):
                ans = [None] * N
                for i in range(N):
                    ans[i] = ['S', 'W'][T[i]]
                print(''.join(ans))
                exit()
print(-1)

Submission status: Accepted

Solution code:
N = int(input())
D = {True: 'S', False: 'W'}
E = {'o': True, 'x': False}
S = list(input())
for i in range(N):
    S[i] = E[S[i]]
# print(S)

L = {(True, True), (True, False), (False, True), (False, False)}

for a, b in L:
    ans = [None]*N
    ans[0], ans[1] = a, b
    for i in range(1, N-1):
        if not ans[i] ^ S[i]:
            ans[i+1] = ans[i-1]
        else:
            ans[i+1] = not ans[i-1]
    if ans[-1] ^ S[-1] == ans[-2] ^ ans[0] and ans[0] ^ S[0] == ans[-1] ^ ans[1]:
        for i in range(N):
            ans[i] = D[ans[i]]
        print(''.join(ans))
        exit()
else:
    print(-1)

Submission status: Accepted

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

# 1番目を決めると全て決まる→これは間違った性質
# 正しくはある連続した2匹の動物の種類がわかれば、その隣にいる動物の種類もわかるということ

pattern = [['S', 'S'], ['S', 'W'], ['W', 'S'], ['W', 'W']]

L = []
for i in range(4):
    ans = ''
    for j in range(N):
        if j == 0:
            ans += pattern[i][j]
            continue
        if j == 1:
            ans += pattern[i][j]
            continue

        if s[j -
             2] == 'o' and s[j -
                             1] == 'o' and ans[j -
                                               1] == 'S' and ans[j -
                                                                 2] == 'S':
            ans += 'S'
        elif s[j -
               2] == 'x' and s[j -
                               1] == 'o' and ans[j -
                                                 1] == 'S' and ans[j -
                                                                   2] == 'S':
            ans += 'S'
        elif s[j -
               2] == 'o' and s[j -
                               1] == 'x' and ans[j -
                                                 1] == 'S' and ans[j -
                                                                   2] == 'S':
            ans += 'W'
        elif s[j -
               2] == 'x' and s[j -
                               1] == 'x' and ans[j -
                                                 1] == 'S' and ans[j -
                                                                   2] == 'S':
            ans += 'W'

        elif s[j -
               2] == 'o' and s[j -
                               1] == 'o' and ans[j -
                                                 1] == 'S' and ans[j -
                                                                   2] == 'W':
            ans += 'W'
        elif s[j -
               2] == 'x' and s[j -
                               1] == 'o' and ans[j -
                                                 1] == 'S' and ans[j -
                                                                   2] == 'W':
            ans += 'W'
        elif s[j -
               2] == 'o' and s[j -
                               1] == 'x' and ans[j -
                                                 1] == 'S' and ans[j -
                                                                   2] == 'W':
            ans += 'S'
        elif s[j -
               2] == 'x' and s[j -
                               1] == 'x' and ans[j -
                                                 1] == 'S' and ans[j -
                                                                   2] == 'W':
            ans += 'S'

        elif s[j -
               2] == 'o' and s[j -
                               1] == 'o' and ans[j -
                                                 1] == 'W' and ans[j -
                                                                   2] == 'S':
            ans += 'W'
        elif s[j -
               2] == 'x' and s[j -
                               1] == 'o' and ans[j -
                                                 1] == 'W' and ans[j -
                                                                   2] == 'S':
            ans += 'W'
        elif s[j -
               2] == 'o' and s[j -
                               1] == 'x' and ans[j -
                                                 1] == 'W' and ans[j -
                                                                   2] == 'S':
            ans += 'S'
        elif s[j -
               2] == 'x' and s[j -
                               1] == 'x' and ans[j -
                                                 1] == 'W' and ans[j -
                                                                   2] == 'S':
            ans += 'S'

        elif s[j -
               2] == 'o' and s[j -
                               1] == 'o' and ans[j -
                                                 1] == 'W' and ans[j -
                                                                   2] == 'W':
            ans += 'S'
        elif s[j -
               2] == 'x' and s[j -
                               1] == 'o' and ans[j -
                                                 1] == 'W' and ans[j -
                                                                   2] == 'W':
            ans += 'S'
        elif s[j -
               2] == 'o' and s[j -
                               1] == 'x' and ans[j -
                                                 1] == 'W' and ans[j -
                                                                   2] == 'W':
            ans += 'W'
        elif s[j -
               2] == 'x' and s[j -
                               1] == 'x' and ans[j -
                                                 1] == 'W' and ans[j -
                                                                   2] == 'W':
            ans += 'W'

    if ans[-1] == 'S' and s[-1] == 'o' and ans[0] != ans[-2]:
        continue
    if ans[-1] == 'S' and s[-1] == 'x' and ans[0] == ans[-2]:
        continue
    if ans[-1] == 'W' and s[-1] == 'o' and ans[0] == ans[-2]:
        continue
    if ans[-1] == 'W' and s[-1] == 'x' and ans[0] != ans[-2]:
        continue

    if ans[0] == 'S' and s[0] == 'o' and ans[-1] != ans[1]:
        continue
    if ans[0] == 'S' and s[0] == 'x' and ans[-1] == ans[1]:
        continue
    if ans[0] == 'W' and s[0] == 'o' and ans[-1] == ans[1]:
        continue
    if ans[0] == 'W' and s[0] == 'x' and ans[-1] != ans[1]:
        continue

    L.append(ans)

if len(L) != 0:
    print(L[0])
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
for i in range(4):
    ans = ['?'] * n
    flg = True
# 1,2 = 羊,羊 の場合
    if i == 0:
        ans[0] = 'S'
        ans[1] = 'S'
        if s[0] == 'o':
            ans[-1] = 'S'
        else:
            ans[-1] = 'W'
        if s[1] == 'o':
            if ans[2] == 'W':
                flg = False
            else:
                ans[2] = 'S'
        else:
            if ans[2] == 'S':
                flg = False
            else:
                ans[2] = 'W'
# 1,2 = 羊,狼 の場合
    if i == 1:
        ans[0] = 'S'
        ans[1] = 'W'
        if s[0] == 'o':
            ans[-1] = 'W'
        else:
            ans[-1] = 'S'
        if s[1] == 'o':
            if ans[2] == 'S':
                flg = False
            else:
                ans[2] = 'W'
        else:
            if ans[2] == 'W':
                flg = False
            else:
                ans[2] = 'S'
# 1,2 = 狼,羊 の場合
    if i == 2:
        ans[0] = 'W'
        ans[1] = 'S'
        if s[0] == 'o':
            ans[-1] = 'W'
        else:
            ans[-1] = 'S'
        if s[1] == 'o':
            if ans[2] == 'S':
                flg = False
            else:
                ans[2] = 'W'
        else:
            if ans[2] == 'W':
                flg = False
            else:
                ans[2] = 'S'
# 1,2 = 狼,狼 の場合
    if i == 3:
        ans[0] = 'W'
        ans[1] = 'W'
        if s[0] == 'o':
            ans[-1] = 'S'
        else:
            ans[-1] = 'W'
        if s[1] == 'o':
            if ans[2] == 'W':
                flg = False
            else:
                ans[2] = 'S'
        else:
            if ans[2] == 'S':
                flg = False
            else:
                ans[2] = 'W'
    for i in range(2,n-2):
        if ans[i] == 'S':
            if s[i] == 'o':
                if ans[i-1] == 'S':
                    ans[i+1] = 'S'
                else:
                    ans[i+1] = 'W'
            else:
                if ans[i-1] == 'S':
                    ans[i+1] = 'W'
                else:
                    ans[i+1] = 'S'
        else:
            if s[i] == 'o':
                if ans[i-1] == 'S':
                    ans[i+1] = 'W'
                else:
                    ans[i+1] = 'S'
            else:
                if ans[i-1] == 'S':
                    ans[i+1] = 'S'
                else:
                    ans[i+1] = 'W'
    if ans[-2] == 'S':
        if s[-2] == 'o':
            if ans[-3] != ans[-1]:
                flg = False
        else:
            if ans[-3] == ans[-1]:
                flg = False
    else:
        if s[-2] == 'o':
            if ans[-3] == ans[-1]:
                flg = False
        else:
            if ans[-3] != ans[-1]:
                flg = False
    if ans[-1] == 'S':
        if s[-1] == 'o':
            if ans[-2] != ans[0]:
                flg = False
        else:
            if ans[-2] == ans[0]:
                flg = False
    else:
        if s[-1] == 'o':
            if ans[-2] == ans[0]:
                flg = False
        else:
            if ans[-2] != ans[0]:
                flg = False
    if flg:
        print(''.join(ans))
        exit(0)
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
for i in range(4):
# 1,2 = 羊,羊 の場合
    ans = ['?'] * n
    flg = True
    if i == 0:
        ans[0] = 'S'
        ans[1] = 'S'
        if s[0] == 'o':
            ans[-1] = 'S'
        else:
            ans[-1] = 'W'
        if s[1] == 'o':
            if ans[2] == 'W':
                flg = False
            else:
                ans[2] = 'S'
        else:
            if ans[2] == 'S':
                flg = False
            else:
                ans[2] = 'W'
# 1,2 = 羊,狼 の場合
    if i == 1:
        ans[0] = 'S'
        ans[1] = 'W'
        if s[0] == 'o':
            ans[-1] = 'W'
        else:
            ans[-1] = 'S'
        if s[1] == 'o':
            if ans[2] == 'S':
                flg = False
            else:
                ans[2] = 'W'
        else:
            if ans[2] == 'W':
                flg = False
            else:
                ans[2] = 'S'
# 1,2 = 狼,羊 の場合
    if i == 2:
        ans[0] = 'W'
        ans[1] = 'S'
        if s[0] == 'o':
            ans[-1] = 'W'
        else:
            ans[-1] = 'S'
        if s[1] == 'o':
            if ans[2] == 'S':
                flg = False
            else:
                ans[2] = 'W'
        else:
            if ans[2] == 'W':
                flg = False
            else:
                ans[2] = 'S'
# 1,2 = 狼,狼 の場合
    if i == 3:
        ans[0] = 'W'
        ans[1] = 'W'
        if s[0] == 'o':
            ans[-1] = 'S'
        else:
            ans[-1] = 'W'
        if s[1] == 'o':
            if ans[2] == 'W':
                flg = False
            else:
                ans[2] = 'S'
        else:
            if ans[2] == 'S':
                flg = False
            else:
                ans[2] = 'W'
    if flg:
        for i in range(2,n-1):
            if ans[i] == 'S':
                if s[i] == 'o':
                    if ans[i-1] == 'S':
                        if ans[i+1] == 'W':
                            flg = False
                        else:
                            ans[i+1] = 'S'
                    else:
                        if ans[i+1] == 'S':
                            flg = False
                        else:
                            ans[i+1] = 'W'
                else:
                    if ans[i-1] == 'S':
                        if ans[i+1] == 'S':
                            flg = False
                        else:
                            ans[i+1] = 'W'
                    else:
                        if ans[i+1] == 'W':
                            flg = False
                        else:
                            ans[i+1] = 'S'
            else:
                if s[i] == 'o':
                    if ans[i-1] == 'S':
                        if ans[i+1] == 'S':
                            flg = False
                        else:
                            ans[i+1] = 'W'
                    else:
                        if ans[i+1] == 'W':
                            flg = False
                        else:
                            ans[i+1] = 'S'
                else:
                    if ans[i-1] == 'S':
                        if ans[i+1] == 'W':
                            flg = False
                        else:
                            ans[i+1] = 'S'
                    else:
                        if ans[i+1] == 'S':
                            flg = False
                        else:
                            ans[i+1] = 'W'
        if ans[-1] == 'S':
            if s[-1] == 'o':
                if ans[-2] != ans[0]:
                    flg = False
            else:
                if ans[-2] == ans[0]:
                    flg = False
        else:
            if s[-1] == 'o':
                if ans[-2] == ans[0]:
                    flg = False
            else:
                if ans[-2] != ans[0]:
                    flg = False
    if flg:
        print(''.join(ans))
        exit(0)
print(-1)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
s=s+s
x=[["S","S"],["S","W"],["W","S"],["W","W"]]

for temp1 in x:
    if s[0]=="o":
        c=temp1[1]
    else:
        if temp1[1]=="S":
            c="W"
        else:
            c="S"
    for i in range(1,n+1):
        if s[i]=="o":
            if temp1[i]=="S":
                temp1.append(temp1[i-1])
                
            else:
                if temp1[i-1]=="S":
                    temp1.append("W")
                else:
                    temp1.append("S")
        else:
            if temp1[i]=="W":
                temp1.append(temp1[i-1])
            else:
                if temp1[i-1]=="W":
                    temp1.append("S")
                else:
                    temp1.append("W")
    
    
    if temp1[0]==temp1[-2] and temp1[1]==temp1[-1]:
        print("".join(temp1[:n]))
        exit()
print(-1)

Submission status: Accepted

Solution code:
N=int(input())
S=list(str(input()))
hyp=[["S","S"],["S","W"],["W","S"],["W","W"]]
flag=0
for i in range(4):
    if flag==2:
        break
    flag=0
    ans=[]
    ans.append(hyp[i][0])
    ans.append(hyp[i][1])
    for j in range(1,N+1):
        if j>=N-1:
            if j==N:
                j=0
                t=1
            else:
                t=0
            
            if ans[j]=="S":
                if S[j]=="o":
                    if ans[j-1]=="S":
                        if ans[t]=="S":
                            flag+=1
                    else:
                        if ans[t]=="W":
                            flag+=1
                else:
                    if ans[j-1]=="S":
                        if ans[t]=="W":
                            flag+=1
                    else:
                        if ans[t]=="S":
                            flag+=1
            else:
                if S[j]=="o":
                    if ans[j-1]=="S":
                        if ans[t]=="W":
                            flag+=1
                    else:
                        if ans[t]=="S":
                            flag+=1
                else:
                    if ans[j-1]=="S":
                        if ans[t]=="S":
                            flag+=1
                    else:
                        if ans[t]=="W":
                            flag+=1
                            
        else:
            if ans[j]=="S":
                if S[j]=="o":
                    if ans[j-1]=="S":
                        ans.append("S")
                    else:
                        ans.append("W")
                else:
                    if ans[j-1]=="S":
                        ans.append("W")
                    else:
                        ans.append("S")
            else:
                if S[j]=="o":
                    if ans[j-1]=="S":
                        ans.append("W")
                    else:
                        ans.append("S")
                else:
                    if ans[j-1]=="S":
                        ans.append("S")
                    else:
                        ans.append("W")

if flag!=2:
    print(-1)
else:
    for i in range(len(ans)):
        if i!=len(ans)-1:
            print(ans[i],end="")
        else:
            print(ans[i])