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: Wrong Answer

Solution code:
n = int(input())
s = input()
a = "SS"
ct=0
while ct<6:    
    for i in range(n-1):
        p=a[i]
        q="A"
        print("hi")
        if a[i]=="S":
            q="W"
        else:
            q="S"
        if a[i+1]=="S":
            if s[i+1] == "o":
                a=a+a[i]
            else:
                a=a+q
        else:
            if s[i+1] == "o":
                 a=a+q
            else:
                a=a+a[i]
    if a[0]==a[n]:
        print(a[0:n])
        break
    else:
        ct=ct+1
        if ct==2:
            a="SW"
        elif ct==3:
            a="WS"
        elif ct==4:
            a="WW"
        else:
            print(-1)
            break

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
ans = -1
for x in ['WW']:
    for i, s in enumerate(S):
        if s=='o':
            if x[-1]=='S':
                x += x[-2]
            else:
                if x[-2]=='S':
                    x += 'W'
                else:
                    x += 'S'
        else:
            if x[-1] == 'S':
                if x[-2]=='S':
                    x += 'W'
                else:
                    x += 'S'
            else:
                x += x[-2]
    if x[0]==x[-2] and x[1]==x[-1]:
        ans = x[1:-1]
        break
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
ans = -1
for x in ['SS', 'SW', 'WS', 'WW']:
    for i, s in enumerate(S):
        if s=='o':
            if x[-1]=='S':
                x += x[-1]
            else:
                if x[-2]=='S':
                    x += 'W'
                else:
                    x += 'S'
        else:
            if x[-1] == 'S':
                if x[-2]=='S':
                    x += 'W'
                else:
                    x += 'S'
            else:
                x += x[-1]
    if x[0]==x[-2] and x[1]==x[-1]:
        ans = x[1:-1]
        break
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
ans = -1
for x in ['SS', 'SW', 'WS', 'WW']:
    for i, s in enumerate(S):
        if s=='o':
            if x[-1]=='S':
                x += x[-1]
            else:
                if x[-2]=='S':
                    x += 'W'
                else:
                    x += 'S'
        else:
            if x[-1] == 'S':
                if x[-2]=='S':
                    x += 'W'
                else:
                    x += 'S'
            else:
                x += x[-1]
    x = x[1:]
    if x[0]==x[-1]:
        ans = x[:-1]
        break
print(ans)

Submission status: Wrong Answer

Solution code:
def add_ans(i,ans,circle):
    if circle[i] == 'o':
        if ans[i] == 'S':
            ans += ans[i-1]

        else:
            if ans[i-1] == 'S':
                ans += 'W'
            else:
                ans += 'S'
            
        
        
    if circle[i] == 'x':
        if ans[i] == 'W':
            ans += ans[i-1]
        
        else:
            if ans[i-1] == 'S':
                ans += 'W'
            else:
                ans += 'S'
            
    return ans
def check_last(ans,circle):
    if circle[0] == 'o':
        if ans[0] == 'S':
            last  = ans[1]

        else:
            if ans[1] == 'S':
                last = 'W'
            else:
                last = 'S'



    if circle[0] == 'x':
        if ans[0] == 'W':
            last = ans[1]

        else:
            if ans[1] == 'S':
                last = 'W'
            else:
                last = 'S'
                
                
    if circle[-1] == 'o':
        if ans[-1] == 'S':
            first  = ans[-2]

        else:
            if ans[-2] == 'S':
                first = 'W'
            else:
                first = 'S'



    if circle[-1] == 'x':
        if ans[-1] == 'W':
            first = ans[-2]

        else:
            if ans[-2] == 'S':
                first = 'W'
            else:
                first = 'S'
                
                
    if (last == ans[0]) & (first == ans[-1]):
        return 1
    else :
        return 0


flag = True
n = int(input())
circle = input()

ans_list = ['SS' , 'SW' , 'WS' , 'WW']
for ans in ans_list:
    for i in range(1,len(circle)-1):
        ans = add_ans(i,ans,circle)


    if check_last(ans , circle) == 1:
        print(ans)
        flag = False
        break

if flag:
    print('-1')

Submission status: Wrong Answer

Solution code:
def add_ans(i,ans,circle):
    if circle[i] == 'o':
        if ans[i] == 'S':
            ans += ans[i-1]

        else:
            if ans[i-1] == 'S':
                ans += 'W'
            else:
                ans += 'S'
            
        
        
    if circle[i] == 'x':
        if ans[i] == 'W':
            ans += ans[i-1]
        
        else:
            if ans[i-1] == 'S':
                ans += 'W'
            else:
                ans += 'S'
            
    return ans
def check_last(ans,circle):
    if circle[0] == 'o':
        if ans[0] == 'S':
            last  = ans[1]

        else:
            if ans[1] == 'S':
                last = 'W'
            else:
                last = 'S'



    if circle[0] == 'x':
        if ans[0] == 'W':
            last = ans[1]

        else:
            if ans[1] == 'S':
                last = 'W'
            else:
                last = 'S'
                
                
    if last == ans[-1]:
        return 1
    else :
        return 0


flag = True
n = int(input())
circle = input()

ans_list = ['SS' , 'SW' , 'WS' , 'WW']
for ans in ans_list:
    for i in range(1,len(circle)-1):
        ans = add_ans(i,ans,circle)


    if check_last(ans , circle) == 1:
        print(ans)
        flag = False
        break

if flag:
    print('-1')

Submission status: Wrong Answer

Solution code:
def add_ans(i,ans,circle):
    if circle[i] == 'o':
        if ans[i] == 'S':
            ans += ans[i-1]

        else:
            if ans[i-1] == 'S':
                ans += 'W'
            else:
                ans += 'S'
            
        
        
    if circle[i] == 'x':
        if ans[i] == 'W':
            ans += ans[i-1]
        
        else:
            if ans[i-1] == 'S':
                ans += 'W'
            else:
                ans += 'S'
            
    return ans
def check_last(ans,circle):
    if circle[0] == 'o':
        if ans[0] == 'S':
            last  = ans[1]

        else:
            if ans[1] == 'S':
                last = 'W'
            else:
                last = 'S'



    if circle[0] == 'x':
        if ans[0] == 'W':
            last = ans[1]

        else:
            if ans[1] == 'S':
                last = 'W'
            else:
                last = 'S'
                
                
    if last == ans[-1]:
        return 1
    else :
        return 0


flag = True
n = int(input())
circle = input()

ans_list = ['SS' , 'SW' , 'WS' , 'WW']
for ans in ans_list:
    for i in range(1,len(circle)):
        ans = add_ans(i,ans,circle)


    if check_last(ans , circle) == 1:
        print(ans)
        flag = False
        break

if flag:
    print('-1')

Submission status: Wrong Answer

Solution code:
import sys
n=int(input())
s=str(input())
l=[0]*n
flag=1

if s[0]=='x':
  flag=0
  new_s=''
  for j in range(n):
    if s[j]=='x':
      new_s+='o'
    else:
      new_s+='x'
  s=new_s
  
def check(i,l):
  sys.setrecursionlimit(10000000)
  if i==0:
    l11=[0]*n
    l12=[0]*n
    l21=[0]*n
    l22=[0]*n
    l11[0]='S'
    l11[1]=l11[n-1]='S'
    l12[0]='S'
    l12[1]=l12[n-1]='W'
    tmp1=check(i+1,l11)
    tmp2=check(i+1,l12)
    l21[0]=l21[n-1]='W'
    l21[1]='S'
    l22[0]=l22[1]='W'
    l22[n-1]='S'
    tmp3=check(i+1,l21)
    tmp4=check(i+1,l22)
    if tmp1==tmp2==tmp3==tmp4==-1:
      return -1
    else:
      if tmp1!=-1:
        return tmp1
      if tmp2!=-1:
        return tmp2
      if tmp3!=-1:
        return tmp3
      if tmp4!=-1:
        return tmp4
  elif 1<=i<=n-2:
    if s[i]=='o':
      if l[i]=='S':
        l[i+1]=l[i-1]
        return check(i+1,l)
      else:
        if l[i-1]=='W':
          l[i+1]='S'
          return check(i+1,l)
        else:
          l[i+1]='W'
          return check(i+1,l)
    else:
      if l[i]=='W':
        l[i+1]=l[i-1]
        return check(i+1,l)
      else:
        if l[i-1]=='S':
          l[i+1]='W'
          return check(i+1,l)
        else:
          l[i+1]='S'
          return check(i+1,l)
  else:
    if s[i]=='o':
      if l[i]=='S':
        if l[i-1]==l[0]:
          return l
        else:
          return -1
      else:
        if l[i-1]!=l[0]:
          return l
        else:
          return -1
    else:
      if l[i]=='S':
        if l[i-1]!=l[0]:
          return l
        else:
          return -1
      else:
        if l[i-1]==l[0]:
          if l[0]=='W' and l[i]==l[1]:
            return -1
          else:
            return l
        else:
          return -1      
ans=check(0,l)
if ans==-1:
  print(-1)
else:
  if flag:
    print(''.join(ans))
  else:
    for i in range(n):
      if ans[i]=='S':
        ans[i]='W'
      else:
        ans[i]='S'
    print(''.join(ans))

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())
s = input()

array = [[True, True], [True, False], [False, False], [False, True]]

for i in range(1, n):
  for v in array:
    flag = not((s[i] == 'o') ^ v[i])
    v.append(not(flag ^ v[i-1]))
    
for v in array:
  flag1 = (array[1] == array[-2])
  flag2 = not((s[0] == 'o') ^ v[0])
  flag = not(flag1 ^ flag2)
  
  if v[0] == v[-1] and flag:
    for c in v[0:len(v)-1]:
      if c:
        print('S', end='')
      else:
        print('W', end='')
        
    print()
    sys.exit(0)
    
print(-1)

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())
s = input()

array = [[True, True], [True, False], [False, False], [False, True]]

for i in range(1, n):
  for v in array:
    flag = not((s[i] == 'o') ^ v[i])
    v.append(not(flag ^ v[i-1]))
    
for v in array:
  flag1 = (array[1] == array[-2])
  flag2 = (s[0] == 'o') ^ v[0]
  flag = not(flag1 ^ flag2)
  
  if v[0] == v[-1] and flag:
    for c in v[0:len(v)-1]:
      if c:
        print('S', end='')
      else:
        print('W', end='')
        
    print()
    sys.exit(0)
    
print(-1)

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())
s = input()

array = [[True, True], [True, False], [False, False], [False, True]]

for i in range(1, n):
  for v in array:
    flag = not((s[i] == 'o') ^ v[i])
    v.append(not(flag ^ v[i-1]))
    
for v in array:
  if v[0] == v[-1]:
    for c in v[0:len(v)-1]:
      if c:
        print('S', end='')
      else:
        print('W', end='')
        
    print()
    sys.exit(0)
    
print(-1)

Submission status: Wrong Answer

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

def make_list(li):
    for i in range(2, n):
        if li[i-1] == 'S':
            if s[i-1] == 'o':
                li[i] = li[i-2]
            else:
                if li[i-2] == 'S':
                    li[i] = 'W'
                else:
                    li[i] = 'S'
        else:
            if s[i-1] == 'o':
                if li[i-2] == 'S':
                    li[i] = 'W'
                else:
                    li[i] = 'S'
            else:
                li[i] = li[i-2]

    hantei(li)

def hantei(li):
    if li[-1] == 'S':
        if s[-1] == 'o':
            if li[-2] == li[0]:
                print_list(li)
        else:
            if li[-2] != li[0]:
                print_list(li)
    else:
        if s[-1] == 'o':
            if li[-2] != li[0]:
                print_list(li)
        else:
            if li[-2] == li[0]:
                print_list(li)

def print_list(li):
    for i in li:
        print(i, end = '')
    exit()

li = [[''] * n for _ in range(4)]
li[0][0] = 'S'
li[0][1] = 'S'
li[1][0] = 'S'
li[1][1] = 'W'
li[2][0] = 'W'
li[2][1] = 'S'
li[3][0] = 'W'
li[3][1] = 'W'

for i in range(4):
    make_list(li[i])

print(-1)

Submission status: Wrong Answer

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

def make_list(li):
    for i in range(2, n):
        if li[i-1] == 'S':
            if s[i-1] == 'o':
                li[i] = li[i-2]
            else:
                if li[i-2] == 'S':
                    li[i] = 'W'
                else:
                    li[i] = 'S'
        else:
            if s[i-1] == 'o':
                if li[i-2] == 'S':
                    li[i] = 'W'
                else:
                    li[i] = 'S'
            else:
                li[i] = li[i-2]

    hantei(li)

def hantei(li):
    if li[-1] == 'S':
        if s[-1] == 'o':
            if li[-2] == li[0]:
                print_list(li)
        else:
            if li[-2] != li[0]:
                print_list(li)
    else:
        if s[-1] == 'o':
            if li[-2] != li[0]:
                print_list(li)
        else:
            if li[-2] == li[0]:
                print_list(li)

def print_list(li):
    for i in li:
        print(i, end = ' ')
    exit()

li = [[''] * n for _ in range(4)]
li[0][0] = 'S'
li[0][1] = 'S'
li[1][0] = 'S'
li[1][1] = 'W'
li[2][0] = 'W'
li[2][1] = 'S'
li[3][0] = 'W'
li[3][1] = 'W'

for i in range(4):
    make_list(li[i])

print(-1)

Submission status: Wrong Answer

Solution code:
import sys
INF = 10**10
MOD = 10**9 + 7
sys.setrecursionlimit(100000000)
from functools import lru_cache
from copy import deepcopy 

def main():
    n = int(input())
    s = input()
    flag = False  
    
    for bit in range(4):
        ani0 = (bit>>0)
        ani1 = (bit>>1)

        if ani0 == 0:
            ans = ['S']
        else:
            ans = ['W']

        if ani1 == 0:
            ans.append('S')
        else:
            ans.append('W')
        
        before2 = ani0
        before1 = ani1
        for i in range(n - 1):
            if s[i + 1] == 'o':
                tmp = before2^before1
            else:
                tmp = before2^before1^1
            
            before1,before2 = tmp,before1

            if i != n - 2:
                if before1:
                    ans.append('W')
                else:
                    ans.append('S')
        
        if s[0] == 'o':
            tmp = before2^before1
        else:
            tmp = before2^before1^1
        
        before1,before2 = tmp,before1

        if before1 == ani1 and before2 == ani0:
            flag = True
            break
    
    if flag:
        print(''.join(ans))
    else:
        print(-1)

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
s=input()
def f(A):
  for i in range(1,N-1):
    if s[i]=='o' and A[i-1:i+1]=='SS':
      A+=('S')
    elif s[i]=='x' and A[i-1:i+1]=='SS':
      A+=('W')
    elif s[i]=='o' and A[i-1:i+1]=='SW':
      A+=('W')
    elif s[i]=='x' and A[i-1:i+1]=='SW':
      A+=('S')
    elif s[i]=='o' and A[i-1:i+1]=='WS':
      A+=('W')
    elif s[i]=='x' and A[i-1:i+1]=='WS':
      A+=('S')
    elif s[i]=='o' and A[i-1:i+1]=='WW':
      A+=('S')
    else:
      A+=('W')
  return A
A='SS'
B='SW'
C='WW'
D='WS'
a=f(A)
b=f(B)
c=f(C)
d=f(D)
ans='-1'
t=a
if s[0]=='o':
  if (t[0]=='S' and t[1]==t[-1])or(t[0]=='W' and t[1]!=t[-1]):
    ans=t
else:
  if (t[0]=='S' and t[1]!=t[-1])or(t[0]=='W' and t[1]==t[-1]):
    ans=t
t=b
if s[0]=='o':
  if (t[0]=='S' and t[1]==t[-1])or(t[0]=='W' and t[1]!=t[-1]):
    ans=t
else:
  if (t[0]=='S' and t[1]!=t[-1])or(t[0]=='W' and t[1]==t[-1]):
    ans=t
t=c
if s[0]=='o':
  if (t[0]=='S' and t[1]==t[-1])or(t[0]=='W' and t[1]!=t[-1]):
    ans=t
else:
  if (t[0]=='S' and t[1]!=t[-1])or(t[0]=='W' and t[1]==t[-1]):
    ans=t
t=d
if s[0]=='o':
  if (t[0]=='S' and t[1]==t[-1])or(t[0]=='W' and t[1]!=t[-1]):
    ans=t
else:
  if (t[0]=='S' and t[1]!=t[-1])or(t[0]=='W' and t[1]==t[-1]):
    ans=t
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = input()
dic = ['SS','SW','WS','WW']
for i in range(4):
  ans = dic[i]
  state = ans[-1]
  for j in range(1,N):
    if state=='S':
      if s[j]=='o':
        ans += ans[j-1]
      else:
        if ans[j-1]=='S':
          ans += 'W'
        else:
          ans += 'S'
    else:
      if s[j]=='o':
        if ans[j-1]=='S':
          ans += 'W'
        else:
          ans += 'S'
      else:
        ans += ans[j-1]
    state = ans[-1]
  if ans[0]==ans[-1]:
    print(ans[:-1])
    break
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    s=input()
    s2=[0]*N
    for i in range(N):
        if s[i]=="x":
            s2[i]=1
        
    
    #0が羊，1が狼，最初の2匹を決めておく
    ans=-1
    for a in range(2):
        if ans==-1:
            for b in range(2):
                L=[0]*N
                L[0]=a
                L[1]=b
                for i in range(2,N):
                    L[i]=(L[i-2]+L[i-1]+s2[i-1])%2
                
                
                if L[0]==(L[-2]+L[-1]+s2[-1])%2:
                    ans=1
                    break
        
        
    if ans==-1:
        print(ans)
    else:
        ans=["S"]*N
        for i in range(N):
            if L[i]==1:
                ans[i]="W"
                
        print(''.join(map(str, ans)))
            
                
            

main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
s = input().rstrip()

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

for A in start:
    A = list(A)
    for i in range(1, N):
        if (A[i] == "W" and s[i] == "o") or (A[i] == "S" and s[i] == "x"):
            if A[i - 1] == "S":
                A.append("W")
            else:
                A.append("S")
        else:
            if A[i - 1] != A[i]:
                break
            else:
                A.append(A[i])
    if len(A) == N + 1 and A[0] == A[-1]:
        A = A[:N]
        if (A[0] == "W" and s[0] == "o") or (A[0] == "S" and s[0] == "x"):
            if A[1] != A[-1]:
                print("".join(A))
                sys.exit()
        else:
            if A[0] == A[1] and A[-1] == A[0]:
                print("".join(A))
                sys.exit()
print(-1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
s = input().rstrip()

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

for i in range(4):
    a = list(ans[i])
    # 最初の2つの仮定の時点で矛盾
    if s[0] == "o":
        if a[0] == "S" and a[1] == "W":
            continue
    else:
        if a[0] == "W" and a[1] == "S":
            continue
    for j in range(1, N):
        if s[j] == "o":
            if a[j] == "S" and a[j - 1] == "S":
                a.append("S")
            elif a[j] == "S" and a[j - 1] == "W":
                break
            elif a[j - 1] == "W":
                a.append("S")
            else:
                a.append("W")
        else:
            if a[j] == "W" and a[j - 1] == "W":
                a.append("W")
            elif a[j] == "W" and a[j - 1] == "S":
                break 
            elif a[j - 1] == "W":
                a.append("S")
            else:
                a.append("W")
    if len(a) == N + 1 and a[0] == a[-1]:
        a = a[:N]
        flag = 1
        if s[0] == "o":
            if a[0] == "S" and a[-1] == "W":
                flag = 0
            elif a[0] == "W" and a[1] == a[-1]:
                flag = 0
        else:
            if a[0] == "W" and a[-1] == "S":
                flag = 0
            elif a[0] == "S" and a[1] == a[-1]:
                flag = 0
        if flag: 
            print("".join(a))
            sys.exit()
print(-1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
s = input().rstrip()

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

for i in range(4):
    a = list(ans[i])
    # 最初の2つの仮定の時点で矛盾
    if s[0] == "o":
        if a[0] == "S" and a[1] == "W":
            continue
    else:
        if a[0] == "W" and a[1] == "S":
            continue
    for j in range(1, N):
        if s[j] == "o":
            if a[j] == "S" and a[j - 1] == "S":
                a.append("S")
            elif a[j] == "S" and a[j - 1] == "W":
                break
            elif a[j - 1] == "W":
                a.append("S")
            else:
                a.append("W")
        else:
            if a[j] == "W" and a[j - 1] == "W":
                a.append("W")
            elif a[j] == "W" and a[j - 1] == "S":
                break 
            elif a[j - 1] == "W":
                a.append("S")
            else:
                a.append("W")
    if len(a) == N + 1 and a[0] == a[-1]:
        a = a[:N]
        if s[0] == "o":
            if a[0] == "S" and a[-1] == "W":
                continue
            elif a[0] == "W" and a[1] == a[-1]:
                continue
        else:
            if a[0] == "W" and a[-1] == "S":
                continue
            elif a[0] == "S" and a[1] == a[-1]:
                continue 
        print("".join(a))
        sys.exit()
print(-1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
s = input().rstrip()

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

for i in range(4):
    a = list(ans[i])
    # 最初の2つの仮定の時点で矛盾
    if s[0] == "o":
        if a[0] == "S" and a[1] == "W":
            continue
    else:
        if a[0] == "W" and a[1] == "S":
            continue
    for j in range(1, N):
        if s[j] == "o":
            if a[j] == "S" and a[j - 1] == "S":
                a.append("S")
            elif a[j] == "S" and a[j - 1] == "W":
                break
            elif a[j - 1] == "W":
                a.append("S")
            else:
                a.append("W")
        else:
            if a[j] == "W" and a[j - 1] == "W":
                a.append("W")
            elif a[j] == "W" and a[j - 1] == "S":
                break 
            elif a[j - 1] == "W":
                a.append("S")
            else:
                a.append("W")
    if len(a) == N + 1 and a[0] == a[-1]:
        print("".join(a[:N]))
        sys.exit()
print(-1)

Submission status: Wrong Answer

Solution code:
from itertools import product

n = int(input())
s = input()

bl = False
for ef, el in product([1, -1], repeat=2):
    zoo = [0] * n
    zoo[0] = ef
    zoo[-1] = el
    for i, e in enumerate(s):
        if e == "o":
            if zoo[i] == 1:
                nxt = zoo[i-1]
            else:
                nxt = -zoo[i-1]

        else:
            if zoo[i] == 1:
                nxt = -zoo[i-1]
            else:
                nxt = zoo[i-1]

        if i < n - 1:
            zoo[i+1] = nxt

        elif nxt == zoo[0]:
            bl = True

    if bl:
        break

if bl:
    ans = ["S" if e == 1 else "W" for e in zoo]
    print(*ans, sep="")
    
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
from itertools import permutations

n = int(input())
s = input()

bl = False
for ef, el in permutations([1, -1], 2):
    zoo = [0] * n
    zoo[0] = ef
    zoo[-1] = el
    for i, e in enumerate(s):
        if e == "o":
            if zoo[i] == 1:
                nxt = zoo[i-1]
            else:
                nxt = -zoo[i-1]

        else:
            if zoo[i] == 1:
                nxt = -zoo[i-1]
            else:
                nxt = zoo[i-1]

        if i < n - 1:
            zoo[i+1] = nxt

        elif nxt == zoo[0]:
            bl = True
            break

    if bl:
        break

if bl:
    ans = ["S" if e == 1 else "W" for e in zoo]
    print(*ans, sep="")
    
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
s = input().rstrip()

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

for i in range(4):
    a = ans[i]
    # 最初の2つの仮定の時点で矛盾
    if s[0] == "o":
        if a[0] == "S" and a[1] == "W":
            continue
    else:
        if a[0] == "W" and a[1] == "S":
            continue
    for j in range(1, N):
        if s[j] == "o":
            if a[j] == "S":
                a += "S"
            elif a[j - 1] == "W":
                a += "S"
            else:
                a += "W"
        else:
            if a[j] == "W":
                a += "W"
            elif a[j - 1] == "W":
                a += "S"
            else:
                a += "W"
    if a[0] == a[-1]:
        print(a[:N])
        sys.exit()
print(-1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
s = input().rstrip()

ans = ["?"] * N
flag = 1

for i in range(N):
    if i == 0:
        if s[i] == "o":
            ans[i] = "s"
            ans[i + 1] = "s"
            ans[i - 1] = "s"
        else:
            ans[i] = "w"
            ans[i + 1] = "w"
            ans[i - 1] = "w"
        continue
    
    if i == N - 1:
        if s[i] == "o":
            if ans[i] == "s":
                if not (ans[i - 1] == "s" and ans[0] == "s"):
                    flag = 0
            else:
                if ans[i - 1] == "w" and ans[0] == "w":
                    flag = 0
        else:
            if ans[i] == "w":
                if not (ans[i - 1] == "w" and ans[0] == "w"):
                    flag = 0
            else:
                if ans[i - 1] == "s" and ans[0] == "s":
                    flag = 0
        break
    
    if s[i] == "o":
        if ans[i] == "s":
            ans[i + 1] = "s"
            if ans[i - 1] != "s":
                flag = 0
        else:
            if ans[i - 1] == "w":
                ans[i + 1] = "s"
            else:
                ans[i + 1] = "w"
    else:
        if ans[i] == "w":
            ans[i + 1] = "w"
            if ans[i - 1] != "w":
                flag = 0
        else:
            if ans[i - 1] == "s":
                ans[i + 1] = "w"
            else:
                ans[i + 1] = "s"
if flag:
    print("".join(ans).upper())
    sys.exit()
else:
    print(-1)
    sys.exit()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
s = input().rstrip()

ans = ["?"] * N
flag = 1

for i in range(N):
    if i == 0:
        if s[i] == "o":
            ans[i] = "s"
            ans[i + 1] = "s"
            ans[i - 1] = "s"
        else:
            ans[i] = "s"
        continue
    
    if i == N - 1:
        if s[i] == "o":
            if ans[i] == "s":
                if not (ans[i - 1] == "s" and ans[0] == "s"):
                    flag = 0
            else:
                if ans[i - 1] == "w" and ans[0] == "w":
                    flag = 0
        else:
            if ans[i] == "w":
                if not (ans[i - 1] == "w" and ans[0] == "w"):
                    flag = 0
            else:
                if ans[i - 1] == "s" and ans[0] == "s":
                    flag = 0
        break
    
    if s[i] == "o":
        if ans[i] == "s":
            ans[i + 1] = "s"
            if ans[i - 1] != "s":
                flag = 0
        else:
            if ans[i - 1] == "w":
                ans[i + 1] = "s"
            else:
                ans[i + 1] = "w"
    else:
        if ans[i] == "w":
            ans[i + 1] = "w"
            if ans[i - 1] != "w":
                flag = 0
        else:
            if ans[i - 1] == "s":
                ans[i + 1] = "w"
            else:
                ans[i + 1] = "s"
if "?" in ans:
    flag = 0
if flag:
    print("".join(ans).upper())
    sys.exit()

ans = ["?"] * N
flag = 1

for i in range(N):
    if i == 0:
        if s[i] == "o":
            ans[i] = "w"
        else:
            ans[i] = "w"
            ans[i + 1] = "w"
            ans[i - 1] = "w"
        continue
    
    if i == N - 1:
        if s[i] == "o":
            if ans[i] == "s":
                if not (ans[i - 1] == "s" and ans[0] == "s"):
                    flag = 0
            else:
                if ans[i - 1] == "w" and ans[0] == "w":
                    flag = 0
        else:
            if ans[i] == "w":
                if not (ans[i - 1] == "w" and ans[0] == "w"):
                    flag = 0
            else:
                if ans[i - 1] == "s" and ans[0] == "s":
                    flag = 0
        break
    
    if s[i] == "o":
        if ans[i] == "s":
            ans[i + 1] = "s"
            if ans[i - 1] != "s":
                flag = 0
        else:
            if ans[i - 1] == "w":
                ans[i + 1] = "s"
            else:
                ans[i + 1] = "w"
    else:
        if ans[i] == "w":
            ans[i + 1] = "w"
            if ans[i - 1] != "w":
                flag = 0
        else:
            if ans[i - 1] == "s":
                ans[i + 1] = "w"
            else:
                ans[i + 1] = "s"
if flag:
    print("".join(ans).upper())