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: Runtime Error

Solution code:
n = int(input())
s = input()
animal = [0]*n #0が羊、1が狼
f = [False]
def dfs(v,animal):
    if v == 0:
        dfs(1,animal)
        animal[0]+=1
        dfs(1,animal)
        
        
    if f[0]== True:
        return animal
    if v == n-1:
        if s[v]=='o':
            if animal[v]==0:
                if ((animal[v-1]==0)and(animal[0]==0))or((animal[v-1]==1)and(animal[0]==1)):
                    # print("True!")
                    f[0]= True
                    return 
            else:
                if ((animal[v-1]==0)and(animal[0]==1))or((animal[v-1]==1)and(animal[0]==0)):
                    # print("True!")
                    f[0]= True
                    return 
        else:
            if animal[v]==0:
                if ((animal[v-1]==0)and(animal[0]==1))or((animal[v-1]==1)and(animal[0]==0)):
                    # print("True!")
                    f[0]= True
                    return 
                
            else:
                if ((animal[v-1]==0)and(animal[0]==0))or((animal[v-1]==1)and(animal[0]==1)):
                    # print("True!")
                    f[0]= True
                    return 
        if f[0]==False:
            # print("False!")
            return
        
        
    if s[v]=='o':
        if animal[v]==0:
            if animal[v-1]==0:
                animal[v+1]=0
                dfs(v+1,animal)
            else:
                animal[v+1]+=1
                dfs(v+1,animal)
            
        else:
            if animal[v-1]==0:
                animal[v+1]+=1
                dfs(v+1,animal)
            else:
                animal[v+1]=0
                dfs(v+1,animal)
            

    else:
        if animal[v]==0:
            if animal[v-1]==0:
                animal[v+1]+=1
                dfs(v+1,animal)
            else:
                animal[v+1]=0
                dfs(v+1,animal)
            
        else:
            if animal[v-1]==0:
                animal[v+1]=0
                dfs(v+1,animal)
            else:
                animal[v+1]+=1
                dfs(v+1,animal)
            

dfs(0,animal)
if f[0]==False:
    print(-1)
if f[0]==True:
    u = ''
    for i in range(0,n):
        if animal[i]==0:
            u = u+'S'
        else:
            u = u + 'W'
    print(u)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
animal = [0]*n #0が羊、1が狼
f = [False]
def dfs(v,animal):
    if v == 0:
        dfs(1,animal)
        animal[0]+=1
        dfs(1,animal)
        
        
    if f[0]== True:
        return animal
    if v == n-1:
        if s[v]=='o':
            if animal[v]==0:
                if ((animal[v-1]==0)and(animal[0]==0))or((animal[v-1]==1)and(animal[0]==1)):
                    # print("True!")
                    f[0]= True
                    return animal
            else:
                if ((animal[v-1]==0)and(animal[0]==1))or((animal[v-1]==1)and(animal[0]==0)):
                    # print("True!")
                    f[0]= True
                    return animal
        else:
            if animal[v]==0:
                if ((animal[v-1]==0)and(animal[0]==1))or((animal[v-1]==1)and(animal[0]==0)):
                    # print("True!")
                    f[0]= True
                    return animal
                
            else:
                if ((animal[v-1]==0)and(animal[0]==0))or((animal[v-1]==1)and(animal[0]==1)):
                    # print("True!")
                    f[0]= True
                    return animal
        if f[0]==False:
            # print("False!")
            return
        
        
    if s[v]=='o':
        if animal[v]==0:
            if animal[v-1]==0:
                animal[v+1]=0
                dfs(v+1,animal)
            else:
                animal[v+1]+=1
                dfs(v+1,animal)
            
        else:
            if animal[v-1]==0:
                animal[v+1]+=1
                dfs(v+1,animal)
            else:
                animal[v+1]=0
                dfs(v+1,animal)
            

    else:
        if animal[v]==0:
            if animal[v-1]==0:
                animal[v+1]+=1
                dfs(v+1,animal)
            else:
                animal[v+1]=0
                dfs(v+1,animal)
            
        else:
            if animal[v-1]==0:
                animal[v+1]=0
                dfs(v+1,animal)
            else:
                animal[v+1]+=1
                dfs(v+1,animal)
            

dfs(0,animal)
if f[0]==False:
    print(-1)
if f[0]==True:
    u = ''
    for i in range(0,n):
        if animal[i]==0:
            u = u+'S'
        else:
            u = u + 'W'
    print(u)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
animal = [0]*n #0が羊、1が狼
f = [False]
def dfs(v,animal):
    if f[0]== True:
        return animal
    if v == n-1:
        if s[v]=='o':
            if animal[v]==0:
                if ((animal[v-1]==0)and(animal[0]==0))or((animal[v-1]==1)and(animal[0]==1)):
                    # print("True!")
                    f[0]= True
                    return animal
            else:
                if ((animal[v-1]==0)and(animal[0]==1))or((animal[v-1]==1)and(animal[0]==0)):
                    # print("True!")
                    f[0]= True
                    return animal
        else:
            if animal[v]==0:
                if ((animal[v-1]==0)and(animal[0]==1))or((animal[v-1]==1)and(animal[0]==0)):
                    # print("True!")
                    f[0]= True
                    return animal
                
            else:
                if ((animal[v-1]==0)and(animal[0]==0))or((animal[v-1]==1)and(animal[0]==1)):
                    # print("True!")
                    f[0]= True
                    return animal
        if f[0]==False:
            # print("False!")
            return
        
        
    if s[v]=='o':
        if animal[v]==0:
            if animal[v-1]==0:
                animal[v+1]=0
                dfs(v+1,animal)
            else:
                animal[v+1]+=1
                dfs(v+1,animal)
            
        else:
            if animal[v-1]==0:
                animal[v+1]+=1
                dfs(v+1,animal)
            else:
                animal[v+1]=0
                dfs(v+1,animal)
            

    else:
        if animal[v]==0:
            if animal[v-1]==0:
                animal[v+1]+=1
                dfs(v+1,animal)
            else:
                animal[v+1]=0
                dfs(v+1,animal)
            
        else:
            if animal[v-1]==0:
                animal[v+1]=0
                dfs(v+1,animal)
            else:
                animal[v+1]+=1
                dfs(v+1,animal)
            

dfs(0,animal)
if f[0]==False:
    print(-1)
if f[0]==True:
    u = ''
    for i in range(0,n):
        if animal[i]==0:
            u = u+'S'
        else:
            u = u + 'W'
    print(u)

Submission status: Runtime Error

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)
# -*- 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])

        if cnd[0] != cnd[-1]:
            return None

        if cnd[0] == 'S' and S[0] == 'o' and cnd[1] == cnd[-1]:
            return ''.join(cnd[:-1])
        elif cnd[0] == 'S' and S[0] == 'x' and cnd[1] != cnd[-1]:
            return ''.join(cnd[:-1])
        elif cnd[0] == 'W' and S[0] == 'o' and cnd[1] != cnd[-1]:
            return ''.join(cnd[:-1])
        elif cnd[0] == 'W' and S[0] == 'x' and cnd[1] == cnd[-1]:
            return ''.join(cnd[:-1])

        return None
    # 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()


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])
        if cnd[0] == cnd[-1]:
            return ''.join(cnd[:-1])

        return None

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

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

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

    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: Runtime Error

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

def DFS(ans):
        if len(ans) == N+2:
                return ans

        if (ans[-1] == "S") ^ (s[len(ans)-2] == "o") ^ (ans[-2] == "W"):
                ans += "W"
        else:
                ans += "S"

        return DFS(ans)

for initial in ["SS","SW","WS","WW"]:
        ans = DFS(initial)
        if ans[0] == ans[-2] and ans[1] == ans[-1]:
                print(ans[1:-1])
                exit()

print("-1")

Submission status: Runtime Error

Solution code:
def d_menagerie(N, S):
    s = S + S[0:2]  # 円環状に並んでいることを考慮
    for ans in ['SS', 'SW', 'WS', 'WW']:  # Sは羊、Wは狼。1番と2番の種類を仮定する
        for i in range(1, N + 1):
            if (ans[-1] == 'S' and s[i] == 'o') or (ans[-1] == 'W' and s[i] == 'x'):
                # この場合、今注目している動物の両隣は同じ種類。
                # よって、今注目している動物の右隣を左隣と同じにする。
                ans += ans[-2]
            else:
                # 両隣は違う種類。右隣を左隣と逆にする。
                ans += ('S' if ans[-2] == 'W' else 'W')
        if ans[:2] == ans[N:]:
            # sの最初の2文字(仮定)と最後の2文字が一致した文字列のみ受理する。
            # 1つ見つければ良いので、すぐにbreakする
            ans = ans[:N]
            break
    else:  # どの割り当て方も矛盾した
        ans = '-1'
    return ans

N = int(input())
S = input().strip()
print(d_Menagerie(N, S))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100001)

N=int(input())
S=input()


def solve(n,str):
  if n==N:
    if str[-2]==str[0] and str[-1]==str[1]:
      str=str[1:-1]
    else:
      str=""
    return str
  else:
    if (S[n]=="o" and str[-1]=="S") or (S[n]=="x" and str[-1]=="W"):
      str=str+str[-2]
    else:
      if str[-2]=="W":
        str=str+"S"
      else:
        str=str+"W"
    ans=solve(n+1,str)
    return ans

flgT=["SSS","SWW","WWS","WSW"]
flgF=["SSW","WSS","SWS","WWW"]
imp=True
if S[0]=='o':
  for i in range(4):
    ans=solve(1,flgT[i])
    if len(ans)>0:
      print(ans)
      imp=False
      break
else:
  for i in range(4):
    ans=solve(1,flgF[i])
    if len(ans)>0:
      print(ans)
      imp=False
      break
if imp:
  print(-1)

Submission status: Runtime Error

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

def solve(n,str):
  if n==N:
    if str[-2]==str[0] and str[-1]==str[1]:
      str=str[1:-1]
    else:
      str=""
    return str
  else:
    if (S[n]=="o" and str[-1]=="S") or (S[n]=="x" and str[-1]=="W"):
      str=str+str[-2]
    else:
      if str[-2]=="W":
        str=str+"S"
      else:
        str=str+"W"
    ans=solve(n+1,str)
    return ans

flgT=["SSS","SWW","WWS","WSW"]
flgF=["SSW","WSS","SWS","WWW"]
imp=True
if S[0]=='o':
  for i in range(4):
    ans=solve(1,flgT[i])
    if len(ans)>0:
      print(ans)
      imp=False
      break
else:
  for i in range(4):
    ans=solve(1,flgF[i])
    if len(ans)>0:
      print(ans)
      imp=False
      break
if imp:
  print(-1)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

n = int(input())
s = input()
t1 = ["S","S"]
l1 = ["W","W"]
t2 = ["S","W"]
l2 = ["W","S"]
for i in range(n-2):
    t1.append("S")
for i in range(n):
    if i+1 <= n-1 and i != 0:
        if (s[i] == "x" and t1[i] == "S") or (s[i] == "o" and t1[i] == "W"):
            if t1[i-1] == "S":
                t1[i+1] = "W"

            else:
                t1[i+1] = "S"

        else:
            t1[i+1] = t1[i]
    elif i == 0:
        pass
    else:
        if ((s[i] == "x" and t1[i] == "S") or (s[i] == "o" and t1[i] == "W")) and t1[0] == t1[i-1]: t1 = "-1"
        elif ((s[i] == "x" and t1[i] == "W") or (s[i] == "o" and t1[i] == "S")) and t1[0] != t1[i-1]: t1 = "-1"
for i in range(n-2):
    t2.append("S")

for i in range(n):
    if i+1 <= n-1 and i != 0:
        if (s[i] == "x" and t2[i] == "S") or (s[i] == "o" and t2[i] == "W"):
            if t2[i-1] == "S":
                t2[i+1] = "W"

            else:
                t2[i+1] = "S"

        else:
            t2[i+1] = t2[i]
    elif i == 0:
        pass
    else:
        if ((s[i] == "x" and t2[i] == "S") or (s[i] == "o" and t2[i] == "W")) and t2[0] == t2[i-1]: t = "-1"
        elif ((s[i] == "x" and t2[i] == "W") or (s[i] == "o" and t2[i] == "S")) and t2[0] != t2[i-1]: t = "-1"

for i in range(n-2):
    l1.append("W")

for i in range(n):
    if i+1 <= n-1 and i != 0:
        if (s[i] == "x" and l1[i] == "S") or (s[i] == "o" and l1[i] == "W"):
            if l1[i-1] == "S":
                l1[i+1] = "W"

            else:
                l1[i+1] = "S"

        else:
            l1[i+1] = l1[i]
            
    elif i == 0:
        pass
    else:
        if(s[i] == "x" and l1[i] == "S") or (s[i] == "o" and l1[i] == "W") and l1[0] == l1[i-1]: l1 = "-1"
        elif (s[i] == "x" and l1[i] == "W") or (s[i] == "o" and l1[i] == "S") and l[0] != l1[i-1]: l1 = "-1"
for i in range(n-2):
    l2.append("S")

for i in range(n):
    if i+1 <= n-1 and i != 0:
        if (s[i] == "x" and l2[i] == "S") or (s[i] == "o" and l2[i] == "W"):
            if l2[i-1] == "S":
                l2[i+1] = "W"

            else:
                l2[i+1] = "S"

        else:
            l2[i+1] = l2[i]
            
    elif i == 0:
        pass
    else:
        if(s[i] == "x" and l2[i] == "S") or (s[i] == "o" and l2[i] == "W") and l2[0] == l2[i-1]: l2 = "-1"
        elif (s[i] == "x" and l2[i] == "W") or (s[i] == "o" and l2[i] == "S") and l2[0] != l2[i-1]: l2 = "-1"
if t1 != "-1":
    for i in t1:
        print(i,end="")
elif t2 != "-1":
    for i in t2:
        print(i,end="")
elif l1 != "-1":
    for i in l1:
        print(i,end="")
elif l2 != "-1":
    for i in l2:
        print(i,end="")
else:print("-1")

Submission status: Runtime Error

Solution code:

def check(s, ans, i) :
    return s[i-1] ^ ans[i-2] ^ ans[i]

def solve(first, second, s) :
    n = len(s)
    ans = [0] * (n)
    ans[-2] = first;
    ans[-1] = second;



    for i in range(0, n-2) :
        nsame = 0
        nsame ^= s[i-1]
        nsame ^= ans[i-1]
        ans[i] = ans[i-2] ^ nsame
    if check( s, ans, -2 ) == 0 and check(s, ans, -1) == 0 :
        ans += [1]
#    print(ans)
    return ans

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

data = [0] * N
for i in range(N) :
    data[i] = 1 if s[i] == 'o' else 0
ok = 0

for i in range(2) :
    if ok == 1 :
        break
    for j in range(2) :
        if ok == 1 :
            break
        ans = solve(i, j, data)
        ok = len(ans) - N
if ok == 0 :
    print("-1")
else :
    for i in range(N) :
        print('S' if ans[i] == 1 else 'W', end = "")
    print()

Submission status: Runtime Error

Solution code:
io = STDIN
$n=io.gets.chomp.to_i
$s=io.gets.chomp

$ans=' '*$n

def check2(i)
  return true if $ans[i]==' '
  nxt = i==$n-1 ? 0 : i+1
  return true if $ans[i-1]==' ' || $ans[nxt]==' '
  rslt=case $s[i]
  when "o"
    if $ans[i]=="S"
      $ans[i-1]==$ans[nxt]
    else
      $ans[i-1]!=$ans[nxt]
    end
  else
    if $ans[i]=="S"
      $ans[i-1]!=$ans[nxt]
    else
      $ans[i-1]==$ans[nxt]
    end
  end
  # p ["check2",i,rslt,$s[i],$ans[i-1],$ans[i],$ans[nxt]]
  rslt
end
def check1(i)
  if i==$n
    if check2(i-1)
      return $ans
    else
      return false
    end
  else
    ["S","W"].each do |a|
      $ans[i]=a
      if check2(i-1)
        return true if check1(i+1)
      end
    end
    return false
  end
end
puts check1(0) ? $ans : -1

Submission status: Runtime Error

Solution code:
# coding: utf-8


def main():
    n = int(input())
    data = list(input())

    result = []
    for i in range(n):
        if len(result) == i-1:
            if data[i] == 'o' and data[i+1] == 'o':
                result.append('W')
                result.append('W')
            else:
                result.append('S')

    if len(result) < 3 or 'W' not in result:
        print(-1)
    else:
        print(result)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
def check(s, ans):
    if len(s) != len(ans):
        return False;
    for i in range(0, len(s)):
        pre = i - 1
        next = i + 1
        if i == 0:
            pre = len(s) - 1
        if i == len(s) - 1:
            next = 0
                
        if ans[i] == "S":
            if s[i] == "o":
                if ans[pre] != ans[next]:
                    return False
            else:
                if ans[pre] == ans[next]:
                    return False
        else:
            if s[i] == "x":
                if ans[pre] != ans[next]:
                    return False
            else:
                if ans[pre] == ans[next]:
                    return False
    return True
                                
def getAns(s, ans, idx, n):
    if idx == n - 1:
        if check(a, ans):
            return ans
        else:
            return []
    if idx == 0:
        if ans[idx] == "S":
            if s[idx] == "o":
                ans[n - 1] = ans[1]
            else:
                if ans[1] == "S":
                    ans[n - 1] = "W"
                else:
                    ans[n - 1] = "S"
        else:
            if s[idx] == "x":
                ans[n - 1] = ans[1]
            else:
                if ans[1] == "S":
                    ans[n - 1] = "W"
                else:
                    ans[n - 1] = "S"
        return getAns(s, ans, idx + 1, n)
    pre  = idx - 1
    next = idx + 1
    # 羊
    if ans[idx] == "S":
        if s[idx] == "o":
            ans[next] = ans[pre]
        else:
            if ans[pre] == "S":
                ans[next] = "W"
            else:
                ans[next] = "S"
    # 狼
    else:
        if s[idx] == "x":
            ans[next] = ans[pre]
        else:
            if ans[pre] == "S":
                ans[next] = "W"
            else:
                ans[next] = "S"
    return getAns(s, ans, idx + 1, n)

def arrayToString(arry):
    s = ""
    for i in range(0, len(arry)):
        s+= arry[i]
    return s

n = int(input())
a = input()
s = [a[i] for i in range(0, len(a))]
ans = ["S" for i in range(0, len(a))]
ans1 = ans[:]
ans1 = getAns(s, ans1, 0, n)
if len(ans1) != 0:
    print(arrayToString(ans1))
    quit()
ans2 = ans[:]
ans2[0] = "W"
ans2 = getAns(s, ans2, 0, n)
if len(ans2) != 0:
    print(arrayToString(ans2))
    quit()
ans3 = ans[:]
ans3[1] = "W"
ans3 = getAns(s, ans3, 0, n)
if len(ans3) != 0:
    print(arrayToString(ans3))
    quit()
ans4 = ans[:]
ans4[0] = "W"
ans4[1] = "W"
ans4 = getAns(s, ans4, 0, n)
if len(ans4) != 0:
    print(arrayToString(ans4))
    quit()
print("-1")

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
def check(s, ans):
    for i in range(0, len(s)):
        pre = i - 1
        next = i + 1
        if i == 0:
            pre = len(s) - 1
        if i == len(s) - 1:
            next = 0
                
        if ans[i] == "S":
            if s[i] == "o":
                if ans[pre] != ans[next]:
                    return False
            else:
                if ans[pre] == ans[next]:
                    return False
        else:
            if s[i] == "x":
                if ans[pre] != ans[next]:
                    return False
            else:
                if ans[pre] == ans[next]:
                    return False
    return True
                                
def getAns(s, ans, idx, n):
    if idx == n - 1:
        if check(a, ans):
            return ans
        else:
            return []
    if idx == 0:
        if ans[idx] == "S":
            if s[idx] == "o":
                ans[n - 1] = ans[1]
            else:
                if ans[1] == "S":
                    ans[n - 1] = "W"
                else:
                    ans[n - 1] = "S"
        else:
            if s[idx] == "x":
                ans[n - 1] = ans[1]
            else:
                if ans[1] == "S":
                    ans[n - 1] = "W"
                else:
                    ans[n - 1] = "S"
        return getAns(s, ans, idx + 1, n)
    pre  = idx - 1
    next = idx + 1
    # 羊
    if ans[idx] == "S":
        if s[idx] == "o":
            ans[next] = ans[pre]
        else:
            if ans[pre] == "S":
                ans[next] = "W"
            else:
                ans[next] = "S"
    # 狼
    else:
        if s[idx] == "x":
            ans[next] = ans[pre]
        else:
            if ans[pre] == "S":
                ans[next] = "W"
            else:
                ans[next] = "S"
    return getAns(s, ans, idx + 1, n)

def arrayToString(arry):
    s = ""
    for i in range(0, len(arry)):
        s+= arry[i]
    return s

n = int(input())
a = input()
s = [a[i] for i in range(0, len(a))]
ans = ["S" for i in range(0, len(a))]
ans1 = ans[:]
ans1 = getAns(s, ans1, 0, n)
if len(ans1) != 0:
    print(arrayToString(ans1))
    quit()
ans2 = ans[:]
ans2[0] = "W"
ans2 = getAns(s, ans2, 0, n)
if len(ans2) != 0:
    print(arrayToString(ans2))
    quit()
ans3 = ans[:]
ans3[1] = "W"
ans3 = getAns(s, ans3, 0, n)
if len(ans3) != 0:
    print(arrayToString(ans3))
    quit()
ans4 = ans[:]
ans4[0] = "W"
ans4[1] = "W"
ans4 = getAns(s, ans4, 0, n)
if len(ans4) != 0:
    print(arrayToString(ans4))
    quit()
print("-1")

Submission status: Runtime Error

Solution code:
import sys
def point(main_animal,main_animal_answer,previous_animal):
    next_animal=''
    if main_animal=='S':
        if main_animal_answer=='o':
            next_animal=previous_animal
        else:
            if previous_animal=='W':
                next_animal='S'
            else:
                next_animal='W'
    else:
        if main_animal_answer=='x':
            next_animal=previous_animal
        else:
            if previous_animal=='W':
                next_animal='S'
            else:
                next_animal='W'
        

    
    
    return next_animal

def cheacker(main_animal,main_animal_answer,previous_animal,next_animal):
    if (main_animal=="S" and main_animal_answer=="o")or(main_animal=='W' and main_animal_answer=='x' ):
        if(previous_animal==next_animal):
            return True
        else:
            return False
    elif (main_animal=="S"and main_animal_answer=="x")or(main_animal=="W" and main_animal_answer=="o"):
        if(previous_animal!=next_animal):
            return True
        else:
            return False
    else:
        return False

def cheack_list(animal_list,answer_list):
    final_ans=True
    for i in range(1,len(animal_list)-2):
        if i==1:
            ans=cheacker(animal_list[i],answer_list[i],animal_list[len(animal_list)-1],animal_list[i+1])
        else:
            ans=cheacker(animal_list[i],answer_list[i],animal_list[i-1],animal_list[i+1])
        if ans==False:
            final_ans=False
        else:
            pass
    return final_ans

N=input()
str=input()
answer_list=[]
animal_list=[]
for i in str:
    answer_list.append(i)

animal_list.append("S")
animal_list.append("S")

for i in range(1,len(answer_list)-1):
    animal_list.append(point(animal_list[i],answer_list[i],animal_list[i-1]))

ans=cheack_list(animal_list,answer_list)
if ans:
    print(animal_list)
    sys.exit
animal_list.clear


animal_list.append("S")
animal_list.append("W")

for i in range(1,len(answer_list)-1):
    animal_list.append(point(animal_list[i],answer_list[i],animal_list[i-1]))

ans=cheack_list(animal_list,answer_list)
if ans:
    print(animal_list)
    sys.exit

animal_list.clear
animal_list.append("W")
animal_list.append("S")

for i in range(1,len(answer_list)-1):
    animal_list.append(point(animal_list[i],answer_list[i],animal_list[i-1]))

ans=cheack_list(animal_list,answer_list)
if ans:
    print(animal_list)
    sys.exit
    animal_list.clear
animal_list.append("W")
animal_list.append("W")

for i in range(1,len(answer_list)-1):
    animal_list.append(point(animal_list[i],answer_list[i],animal_list[i-1]))

ans=cheack_list(animal_list,answer_list)
if ans:
    print(animal_list)
    sys.exit

print("-1")

Submission status: Runtime Error

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

r = {'S' : 'W', 'W': 'S'}

a = 'SS'
ans = -1
for a in ('SS', 'SW', 'WS', 'WW'):
    for i in range(1,N):
        print(a)
        if s[i] == 'o':
            if a[i] == 'S':
                a += a[i-1]
            else:
                a += r[a[i-1]]
        else:
            if a[i] == 'S':
                a += r[a[i-1]]
            else:
                a += a[i-1]
    if a[N] == a[0]:
        if (s[0] == 'o') and (a[N-1] == a[1]):
            ans = a[:-1]
        elif (s[0] == 'x') and (a[N-1] == r[a[1]]):
            ans = a[:-1]
print(ans)

Submission status: Runtime Error

Solution code:
from test import set_stdin_sandbox
set_stdin_sandbox()

import itertools
N = int(input())
s = list(map(lambda x: True if x == "o" else False, list(input())))

f = lambda x,y: not(x^y)

def hantei(N,s):
    for onetwo in itertools.product((True, False), (True, False)):
        res = {}
        for n in range(N):
            if n == 0:
                res[n] = onetwo[0]
            elif n == 1:
                res[n] = onetwo[1]
            else:
                res[n] = f(res[n-2], f(res[n-1], s[n-1]))
        # check s[0]
        if not ((res[1] == res[N-1]) ^ res[0]) == s[0]:
            if not ((res[0] == res[N-2]) ^ res[N-1]) == s[N-1]:
                print(''.join(map(lambda x: "S" if x == True else "W", res.values())))
                return True

    return False

if not hantei(N, s):
    print(-1)