Problem:


Score : 500 points

Problem Statement

Snuke, who loves animals, built a zoo.

There are N animals in this zoo. They are conveniently numbered 1 through N, and arranged in a circle.
The animal numbered i (2≤i≤N-1) is adjacent to the animals numbered i-1 and i+1. Also, the animal numbered 1 is adjacent to the animals numbered 2 and N, and the animal numbered N is adjacent to the animals numbered N-1 and 1.

There are two kinds of animals in this zoo: honest sheep that only speak the truth, and lying wolves that only tell lies.

Snuke cannot tell the difference between these two species, and asked each animal the following question: "Are your neighbors of the same species?" The animal numbered i answered s_i. Here, if s_i is o, the animal said that the two neighboring animals are of the same species, and if s_i is x, the animal said that the two neighboring animals are of different species.

More formally, a sheep answered o if the two neighboring animals are both sheep or both wolves, and answered x otherwise.
Similarly, a wolf answered x if the two neighboring animals are both sheep or both wolves, and answered o otherwise.

Snuke is wondering whether there is a valid assignment of species to the animals that is consistent with these responses. If there is such an assignment, show one such assignment. Otherwise, print -1.

Constraints

3 ≤ N ≤ 10^{5}

s is a string of length N consisting of o and x.

Input

The input is given from Standard Input in the following format:

N
s

Output

If there does not exist an valid assignment that is consistent with s, print -1.
Otherwise, print an string t in the following format. The output is considered correct if the assignment described by t is consistent with s.

t is a string of length N consisting of S and W.

If t_i is S, it indicates that the animal numbered i is a sheep. If t_i is W, it indicates that the animal numbered i is a wolf.

Sample Input 1

6
ooxoox

Sample Output 1

SSSWWS

For example, if the animals numbered 1, 2, 3, 4, 5 and 6 are respectively a sheep, sheep, sheep, wolf, wolf, and sheep, it is consistent with their responses. Besides, there is another valid assignment of species: a wolf, sheep, wolf, sheep, wolf and wolf.

Let us remind you: if the neiboring animals are of the same species, a sheep answers o and a wolf answers x. If the neiboring animals are of different species, a sheep answers x and a wolf answers o.

Sample Input 2

3
oox

Sample Output 2

-1

Print -1 if there is no valid assignment of species.

Sample Input 3

10
oxooxoxoox

Sample Output 3

SSWWSSSWWS



Sample input:
6
ooxoox


Sample output:
SSSWWS

Submission status: Accepted

Solution code:
N = int(input())
t = list(input())
Ans = ['n' for _ in t]
 
def judge(prevSW, SW, ox):
    if SW == 'S':
        if ox == 'o':
            return prevSW
        else:
            if prevSW == 'S':
                return 'W'
            else: return 'S'
    elif SW == 'W':
        if ox == 'x':
            return prevSW
        else:
            if prevSW == 'S':
                return 'W'
            else: return 'S'
 
def decideorder(startSW):
    Ans[1] = startSW
    for i in range(2,N):
        Ans[i] = judge(Ans[i-2], Ans[i-1], t[i-1])

def check():
    if judge(Ans[-2], Ans[-1], t[-1]) is Ans[0] and judge(Ans[-1], Ans[0], t[0]) is Ans[1]:
        return True
    else:
        return False

Ans[0] = 'S'
decideorder('S')
if check():
    print("".join(Ans))
else:
    decideorder('W')
    if check():
        print("".join(Ans))
    else:
        Ans[0] = 'W'
        decideorder('S')
        if check():
            print("".join(Ans))
        else:
            decideorder('W')
            if check():
                print("".join(Ans))
            else:
                print(-1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# TODO はじめの二匹について組み合わせの仮説を作り、検証する
# 計算時間はO(N)になるはず (4通り×N匹)

from itertools import product

N = int(input())
s = list(input())
assert N == len(s)

def solve(type_b, type_n, ox_n, s=s):
    pass
# input: type_before + type_now + ox_now
# output: type_next

solve = {"WSo": "W",
         "SSx": "W",
         "WSx": "S",
         "SSo": "S",
         "WWx": "W",
         "SWo": "W",
         "WWo": "S",
         "SWx": "S"}

answer = -1

for n0, n1 in product("SW", "SW"):
    animals = [0 for n in range(N)]
    animals[0] = n0
    animals[1] = n1
    for i in range(2, N):
        animals[i] = solve["{}{}{}".format(animals[i-2], animals[i-1], s[i-1])]
    if solve["{}{}{}".format(animals[-2], animals[-1], s[-1])] == n0 and solve["{}{}{}".format(animals[-1], animals[0], s[0])] == n1:
        answer = ""
        for animal in animals:
            answer += animal

print(answer)

Submission status: Accepted

Solution code:
N = int(raw_input())
s = raw_input()
def opp(a):
	return 'S' if a == 'W' else 'W'
def det(a0, a1):
	ap = a0
	ac = a1
	ans = a0

	for c in s[1:]+s[:2]:
		ans += ac
		if (c == 'o' and ac == 'S') or (c == 'x' and ac == 'W'):
			an = ap
		else:
			an = opp(ap)
		ap = ac
		ac = an
	if ans[-2:] == ans[:2]:
		return ans[:-2]
	else:
		return None
for pref in ["SS", "SW", "WW", "WS"]:
	ans = det(pref[0], pref[1])
	if ans:
		break
print -1 if not ans else ans

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(int(2E5))

SHEEP = True
WOLF = False


def print_tree(tree):
    print(''.join(['S' if e else 'W' for e in tree]))


def calc_next_animal(tree, cur_pos):
    if answer[cur_pos]:
        if tree[cur_pos] == SHEEP:
            return tree[cur_pos - 1]
        else:
            return not tree[cur_pos - 1]
    else:
        if tree[cur_pos] == SHEEP:
            return not tree[cur_pos - 1]
        else:
            return tree[cur_pos - 1]


def calc_prev_animal(tree, cur_pos):
    if answer[cur_pos]:
        if tree[cur_pos] == SHEEP:
            return tree[cur_pos + 1]
        else:
            return not tree[cur_pos + 1]
    else:
        if tree[cur_pos] == SHEEP:
            return not tree[cur_pos + 1]
        else:
            return tree[cur_pos + 1]


def wfs(tree, cur_pos, N):
    tree[cur_pos + 1] = calc_next_animal(tree, cur_pos)
    cur_pos += 1
    if cur_pos == N - 1:
        return tree[0] == calc_next_animal(tree, cur_pos) and tree[cur_pos] == calc_prev_animal(tree, 0)
    else:
        return wfs(tree, cur_pos, N)


if __name__ == '__main__':
    N = int(input())
    answer = [True if i == 'o' else False for i in input().rstrip()]
    aa = [[WOLF, WOLF]
        , [WOLF, SHEEP]
        , [SHEEP, SHEEP]
        , [SHEEP, WOLF]]
    super_list = [0] * int(2E5)
    for a in aa:
        super_list[:2] = a
        if wfs(super_list, 1, N):
            print_tree(super_list[:N])
            break
    else:
        print(-1)

Submission status: Accepted

Solution code:
N = int(raw_input())
s = raw_input()
assert(3 <= N <= 1e5)
assert(len(s) == N)

res = []
sheep = 'S'
wolf = 'W'
container = [val for val in s]
o = 'o'
x = 'x'
passed = False
res0 = [sheep, sheep]
res1 = [sheep, wolf]
res2 = [wolf, sheep]
res3 = [wolf, wolf]
RES = [res0, res1, res2, res3]
for res in RES:
    passed1 = False
    passed2 = False
    for i in xrange(1, len(container) - 1):
        val = container[i]
        if res[i] == sheep:
            if val == o:
                res.append(res[i - 1])
            else:
                if res[i - 1] == sheep:
                    res.append(wolf)
                else:
                    res.append(sheep)
        else:# wolf
            if val == o:
                if res[i - 1] == sheep:
                    res.append(wolf)
                else:
                    res.append(sheep)
            else:
                res.append(res[i - 1])
    if (res[0] == sheep):
        if (container[0] == o) and (res[-1] == res[1]):
            passed1 = True
        elif (container[0] == x) and (not (res[-1] == res[1])):
            passed1 = True
    else:
        if (container[0] == o) and (not (res[-1] == res[1])):
            passed1 = True
        elif (container[0] == x) and (res[-1] == res[1]):
            passed1 = True

    if (res[-1] == sheep):
        if (container[-1] == o) and (res[-2] == res[0]):
            passed2 = True
        elif (container[-1] == x) and (not (res[-2] == res[0])):
            passed2 = True
    else:
        if (container[-1] == o) and (not (res[-2] == res[0])):
            passed2 = True
        elif (container[-1] == x) and (res[-2] == res[0]):
            passed2 = True
    if passed1 and passed2:
        passed = True
        break

if passed:
    print ''.join(res)
else:
    print -1

Submission status: Accepted

Solution code:
from itertools import product as prod

n = int(input())
ss = list(input())

def next_anim(a_0, a_1, s):
    if s == "o":
        if a_1 == "S":
            return a_0
        else:
            return "W" if a_0 == "S" else "S"
    else:
        if a_1 == "S":
            return "W" if a_0 == "S" else "S"
        else:
            return a_0

ans = -1
anims = ["" for _ in range(n)]
for fst, snd in prod(["W", "S"], repeat=2):
    anims[:2] = [fst, snd]
    for i in range(2, n):
        anims[i] = next_anim(anims[i-2], anims[i-1], ss[i-1])
    tmp_anim = next_anim(snd, fst, ss[0])
    tmp_anim2 = next_anim(anims[-2], anims[-1], ss[-1])
    if tmp_anim == anims[-1] and tmp_anim2 == anims[0]:
        ans = "".join(anims)
        break

print(ans)

Submission status: Accepted

Solution code:
import itertools
N = int(input())
s = list(input())

def hantei(N,s):
    for onetwo in itertools.product((0, 1), (0, 1)):
        res = {}
        for n in range(N):
            if n == 0:
                res[n] = onetwo[0]
            elif n == 1:
                res[n] = onetwo[1]
            else:
                if res[n-1] == 0 and s[n-1] == "o":
                    res[n] = res[n-2]
                elif res[n-1] == 0 and s[n-1] == "x":
                    res[n] = 1 if res[n-2] == 0 else 0
                elif res[n-1] == 1 and s[n-1] == "o":
                    res[n] = 1 if res[n-2] == 0 else 0
                elif res[n-1] == 1 and s[n-1] == "x":
                    res[n] = res[n-2]
        # check s[0]
        if (res[1] == res[N-1]) and res[0] == 0 and s[0] == "x":
            continue
        if (res[1] == res[N-1]) and res[0] == 1 and s[0] == "o":
            continue
        if (res[1] != res[N-1]) and res[0] == 0 and s[0] == "o":
            continue
        if (res[1] != res[N-1]) and res[0] == 1 and s[0] == "x":
            continue
        # check s[n-1]
        if (res[0] == res[N-2]) and res[N-1] == 0 and s[N-1] == "x":
            continue
        if (res[0] == res[N-2]) and res[N-1] == 1 and s[N-1] == "o":
            continue
        if (res[0] != res[N-2]) and res[N-1] == 0 and s[N-1] == "o":
            continue
        if (res[0] != res[N-2]) and res[N-1] == 1 and s[N-1] == "x":
            continue

        print(''.join(map(lambda x: "S" if x == 0 else "W", res.values())))
        return True

    return False

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

Submission status: Accepted

Solution code:
# coding: utf-8
def getLnInputs():
    return input().split()


def getLnIntInputs():
    return list(map(int, getLnInputs()))


def is_next_sheep(isCurrentSheep, response, isPreviousSheep):
    isNextSame = not (isCurrentSheep ^ response)
    return not (isNextSame ^ isPreviousSheep)


def resolve_hypothesis(firstIsSheep, lastIsSheep, responseArray):
    res_str = "S" if firstIsSheep else "W"

    isCurrentSheep = firstIsSheep
    isPreviousSheep = lastIsSheep

    for i in range(len(responseArray) - 1):
        isNextSheep = is_next_sheep(isCurrentSheep, responseArray[i], isPreviousSheep)
        res_str += "S" if isNextSheep else "W"
        isCurrentSheep, isPreviousSheep = isNextSheep, isCurrentSheep

    if isCurrentSheep != lastIsSheep or is_next_sheep(isCurrentSheep, responseArray[-1], isPreviousSheep) != firstIsSheep:
        return -1

    return res_str


def main():
    input()
    s = input()

    responseArray = list(map(lambda c: c == "o", list(s)))

    for i in range(2):
        for j in range(2):
            firstIsSheep = bool(i)
            lastIsSheep = bool(j)
            res = resolve_hypothesis(firstIsSheep, lastIsSheep, responseArray)

            if res == -1:
                continue

            print(res)
            return

    print(-1)
    return


main()

Submission status: Accepted

Solution code:
import sys

N = int(input())
stemp = input()
s = [True if stemp[i]== 'o' else False for i in range(N)]

T = [True, True] #Sheap, Sheap

for i in range(2,N+1):
    if s[i-1]^T[i-1]==True:
        T.append( not T[i-2])
    else:
        T.append( T[i-2])

if T[N] == T[0] and ((T[1]==T[N-1]) != (T[0] != s[0])):
    output = ['S' if T[i] == True else 'W' for i in range(N)]
    for i in range(N):
        print(output[i], end='')
    print()
    sys.exit()

T = [True, False] #Sheap, Wolf

for i in range(2,N+1):
    if s[i-1]^T[i-1]==True:
        T.append( not T[i-2])
    else:
        T.append( T[i-2])

if T[N] == T[0] and ((T[1]==T[N-1]) != (T[0] != s[0])):
    output = ['S' if T[i] == True else 'W' for i in range(N)]
    for i in range(N):
        print(output[i], end='')
    print()
    sys.exit()

T = [False, True] #Wolf, Sheap

for i in range(2,N+1):
    if s[i-1]^T[i-1]==True:
        T.append( not T[i-2])
    else:
        T.append( T[i-2])

if T[N] == T[0] and ((T[1]==T[N-1]) != (T[0] != s[0])):
    output = ['S' if T[i] == True else 'W' for i in range(N)]
    for i in range(N):
        print(output[i], end='')
    print()
    sys.exit()

T = [False, False]

for i in range(2,N+1):
    if s[i-1]^T[i-1]==True:
        T.append( not T[i-2])
    else:
        T.append( T[i-2])

if T[N] == T[0] and ((T[1]==T[N-1]) != (T[0] != s[0])):
    output = ['S' if T[i] == True else 'W' for i in range(N)]
    for i in range(N):
        print(output[i], end='')
    print()
    sys.exit()

print(-1)

Submission status: Accepted

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

sw = [1,0]
ss = [1,1]
ws = [0,1]
ww = [0,0]

def nxt(tf, last):
    if tf=='x' and last=='S':
        return ''

for idx,c in enumerate(s):
    if idx==0:
        continue
    if c == 'x':
        sw.append(sw[-1]^sw[-2])
        ss.append(ss[-1]^ss[-2])
        ws.append(ws[-1]^ws[-2])
        ww.append(ww[-1]^ww[-2])
    else:
        sw.append(1*(not(sw[-1]^sw[-2])))
        ss.append(1*(not(ss[-1]^ss[-2])))
        ws.append(1*(not(ws[-1]^ws[-2])))
        ww.append(1*(not(ww[-1]^ww[-2])))

c=s[0]
if c == 'x':
    sw.append(sw[-1]^sw[-2])
    ss.append(ss[-1]^ss[-2])
    ws.append(ws[-1]^ws[-2])
    ww.append(ww[-1]^ww[-2])
else:
    sw.append(1*(not(sw[-1]^sw[-2])))
    ss.append(1*(not(ss[-1]^ss[-2])))
    ws.append(1*(not(ws[-1]^ws[-2])))
    ww.append(1*(not(ww[-1]^ww[-2])))


def f(seq):
    # print(seq[-1],seq[0])
    return seq[-2:]==seq[:2]

def pp(seq):
    print(''.join(map(lambda x:'S' if x else 'W', seq[:-2])))

if f(sw):
    pp(sw)
elif f(ss):
    pp(ss)
elif f(ws):
    pp(ws)
elif f(ww):
    pp(ww)
else:
    print(-1)

Submission status: Accepted

Solution code:
from collections import defaultdict


def solve(s, l):

    for i in range(1, len(s) - 1):
        # ひつじ
        if l[i] == 1:
            if s[i] == "o":
                l[i + 1] = l[i - 1]
            else:
                l[i + 1] = -1 * l[i - 1]
        # おおかみ
        else:
            if s[i] == "o":
                l[i + 1] = -1 * l[i - 1]
            else:
                l[i + 1] = l[i - 1]

    if l[0] == 1:
        if s[0] == "o":
            ok1 = l[1] == l[-1]
        else:
            ok1 = l[1] != l[-1]
    # おおかみ
    else:
        if s[0] == "o":
            ok1 = l[1] != l[-1]
        else:
            ok1 = l[1] == l[-1]

    if l[-1] == 1:
        if s[-1] == "o":
            ok2 = l[0] == l[-2]
        else:
            ok2 = l[0] != l[-2]
    # おおかみ
    else:
        if s[-1] == "o":
            ok2 = l[0] != l[-2]
        else:
            ok2 = l[0] == l[-2]

    return ok1 and ok2

def main():
    N = int(input())
    S = input()
    ans = [None] * N
    ans[0] = 1
    ans[1] = 1
    if solve(S, ans):
        print(*["S" if x == 1 else "W" for x in ans], sep="")
        return

    ans = [None] * N
    ans[0] = 1
    ans[1] = -1
    if solve(S, ans):
        print(*["S" if x == 1 else "W" for x in ans], sep="")
        return

    ans = [None] * N
    ans[0] = -1
    ans[1] = 1
    if solve(S, ans):
        print(*["S" if x == 1 else "W" for x in ans], sep="")
        return

    ans = [None] * N
    ans[0] = -1
    ans[1] = -1
    if solve(S, ans):
        print(*["S" if x == 1 else "W" for x in ans], sep="")
        return

    print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

D = ["SS", "SW", "WS", "WW"]
A = "SW"

s += s[0]
for d in D:
    a = d
    for i in range(1, N+1):
        k = 0
        if a[i] == "W":
            k += 1
        if s[i] == "x":
            k += 1
        if a[i-1] == "W":
            k += 1
        a += A[k % 2]
    if a[:2] == a[-2:]:
        print a[:-2]
        exit()
print -1

Submission status: Runtime Error

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

init = ["SS", "SW","WS","WW"]
next_animal = { ("S","o","S"):"S",
                ("S","o","W"):"W",
                ("S","x","S"):"W",
                ("S","x","W"):"S",
                ("W","o","S"):"W",
                ("W","o","W"):"S",
                ("W","x","S"):"S",
                ("W","x","W"):"W "}
for X in init:
    SW = X
    for i in range(1,N):
        SW += next_animal[SW[i],s[i],SW[i-1]]
    if SW[0] == SW[len(SW)-1]:
        print(SW[:len(SW)-1])
        break
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

dic={'o':lambda s:1-(s[1]^s[0]), 'x':lambda s:s[1]^s[0]}
def mknseq(ox,ld):
    for x in ld:
        x.append(dic[ox]((x[-2],x[-1])))
def main():
    N=int(sys.stdin.readline().strip())
    s=list(sys.stdin.readline().strip())
    ans=[deque([x,y]) for x in range(2) for y in range(2)]
    for x in range(N):
        mknseq(s[x],ans)
    # print(ans)
    for x in ans:
        if x[:2]==x[-2:]:
            return print(''.join([['W','S'][i] for i in x])[1:-1])
    print(-1)

if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
# import sys;input = lambda : sys.stdin.readline()
n = int(input())
s = input()
x=[]
ans=["s"]*n
for i in range(n):
    if s[i]=='x':
        x.append(i)
if len(x)>1:
    for i in range(0,len(x),2):
        ans[x[i]+1:x[i+1]]=["w"]*(x[i+1]-x[i]-1)
    print(("".join(ans)).upper())
elif len(x)==1:
    print(-1)
elif len(x)==0:
    print(("s"*n).upper())

Submission status: Runtime Error

Solution code:
import sys

input = sys.stdin.readline


def main():
    N = int(input())
    S = input().strip()

    def solve(current_index, ans):
        if current_index <= 0:
            solve(current_index + 1, ans + "S")
            solve(current_index + 1, ans + "W")

        elif current_index == N - 1:
            if ans[-1] == "S":
                if S[current_index] == 'o':
                    if ans[-2] == ans[0]:
                        print(ans)
                        exit()
                else:
                    if ans[-2] != ans[0]:
                        print(ans)
                        exit()
            else:
                if S[current_index] == 'x':
                    if ans[-2] == ans[0]:
                        print(ans)
                        exit()
                else:
                    if ans[-2] != ans[0]:
                        print(ans)
                        exit()
        else:
            if ans[-1] == "S":
                if S[current_index] == 'o':
                    solve(current_index + 1, ans + ans[-2])
                else:
                    if ans[-2] == "S":
                        solve(current_index + 1, ans + "W")
                    else:
                        solve(current_index + 1, ans + "S")
            else:
                if S[current_index] == 'x':
                    solve(current_index + 1, ans + ans[-2])
                else:
                    if ans[-2] == "S":
                        solve(current_index + 1, ans + "W")
                    else:
                        solve(current_index + 1, ans + "S")

    solve(-1, "")
    print(-1)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys

input = sys.stdin.readline


def main():
    N = int(input())
    S = input().strip()

    def solve(current_index, ans):
        if current_index == 0:
            solve(1, ans + "S")
            solve(1, ans + "W")

        elif current_index == N - 1:
            if ans[-1] == "S":
                if S[current_index] == 'o':
                    if ans[-2] == ans[0]:
                        print(ans)
                        exit()
                else:
                    if ans[-2] != ans[0]:
                        print(ans)
                        exit()
            else:
                if S[current_index] == 'x':
                    if ans[-2] == ans[0]:
                        print(ans)
                        exit()
                else:
                    if ans[-2] != ans[0]:
                        print(ans)
                        exit()
        else:
            if ans[-1] == "S":
                if S[current_index] == 'o':
                    solve(current_index + 1, ans + ans[-2])
                else:
                    if ans[-2] == "S":
                        solve(current_index + 1, ans + "W")
                    else:
                        solve(current_index + 1, ans + "S")
            else:
                if S[current_index] == 'x':
                    solve(current_index + 1, ans + ans[-2])
                else:
                    if ans[-2] == "S":
                        solve(current_index + 1, ans + "W")
                    else:
                        solve(current_index + 1, ans + "S")

    solve(0, "S")
    solve(0, "W")

    print(-1)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

def rev(x):
    if x=="S":
        return "W"
    else:
        return "S"

def wolfsheep(xy):
    global n,s
    result=[xy[0],xy[1]]

    for i in range(1,n+1):
        if result[i]=="S":
            s+=s[0]
            if s[i]=="o":
                result.append(result[i-1])
            else:
                result.append(rev(result[i-1]))
        else:
            if s[i]=="o":
                result.append(rev(result[i-1]))
            else:
                result.append(result[i-1])

    if result[0]==result[n] and result[1]==result[n+1]:
        return result
    else:
        return 0

#SWパターン
tmp= wolfsheep("SW")
if tmp!=0:
    print("".join(tmp[:-2]))
    exit()

#SSパターン
tmp= wolfsheep("SS")
if tmp!=0:
    print("".join(tmp[:-2]))
    exit()
#WWパターン
tmp= wolfsheep("WW")
if tmp!=0:
    print("".join(tmp[:-2]))
    exit()
#WSパターン
tmp= wolfsheep("WS")
if tmp!=0:
    print("".join(tmp[:-2]))
    exit()

print(-1)

Submission status: Runtime Error

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

def rev(x):
    if x=="S":
        return "W"
    else:
        return "S"

def wolfsheep(xy):
    global n,s
    result=[xy[0],xy[1]]

    for i in range(1,n+1):
        if result[i]=="S":
            if s[i]=="o":
                result.append(result[i-1])
            else:
                result.append(rev(result[i-1]))
        else:
            if s[i]=="o":
                result.append(rev(result[i-1]))
            else:
                result.append(result[i-1])

    if result[0]==result[n] and result[1]==result[n+1]:
        return result
    else:
        return 0

#SWパターン
tmp= wolfsheep("SW")
if tmp!=0:
    print("".join(tmp[:-2]))
    exit()

#SSパターン
tmp= wolfsheep("SS")
if tmp!=0:
    print("".join(tmp[:-2]))
    exit()
#WWパターン
tmp= wolfsheep("WW")
if tmp!=0:
    print("".join(tmp[:-2]))
    exit()
#WSパターン
tmp= wolfsheep("WS")
if tmp!=0:
    print("".join(tmp[:-2]))
    exit()

print(-1)

Submission status: Runtime Error

Solution code:
import sys
N=int(input())
slist=[x=="o" for x in input()]
#print(slist)

answer_list=[0]*(N+1)

if N==3:
  if slist.count(True)==3:
    print("SSS")
  elif slist.count(False)==3:
    print("WWW")
  else:
    print(-1)
  sys.exit(0)

ok=False
for p1,p2 in [(1,1),(1,-1),(-1,1),(-1,-1)]:
  answer_list[1]=p1
  answer_list[2]=p2
  if slist[1]:
    answer_list[3]=answer_list[1]
  else:
    answer_list[3]=-answer_list[1]
  for i in range(3,N+1):
    l=i-1
    r=i%N+1
    si=slist[i-1]

    if answer_list[i]==1:
      if si:
        if answer_list[l]==1:
          answer_list[r]=1
        else:
          answer_list[r]=-1
      else:
        if answer_list[l]==1:
          answer_list[r]=-1
        else:
          answer_list[r]=1
    elif answer_list[i]==-1:
      if si:
        if answer_list[l]==1:
          answer_list[r]=-1
        else:
          answer_list[r]=1
      else:
        if answer_list[l]==-1:
          answer_list[r]=1
        else:
          answer_list[r]=-1
    
  contra=False
  if slist[0]:
    if answer_list[1]==1 and answer_list[2]!=answer_list[N]:
      contra=True
    if answer_list[1]==-1 and answer_list[2]==answer_list[N]:
      contra=True
  elif not slist[0]:
    if anser_list[1]==1 and answer_list[2]==answer_list[N]:
      contra=True
    if anser_list[1]==-1 and answer_list[2]!=answer_list[N]:
      contra=True

  if not contra:
    ok=True
    break
      
if ok:
  #print(answer_list[1:])
  sw_list=[]
  for i in range(1,N+1):
    if answer_list[i]==1:
      sw_list.append("S")
    else:
      sw_list.append("W")
  print("".join(sw_list))  
else:
  print(-1)

Submission status: Runtime Error

Solution code:
import sys
import queue


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7


def main():
    N = int(input())
    s = input()
    Animal = ["WW", "WS", "SS", "SW"]
    for i in range(1, N - 1):
        if s[i] == "o":
            for j in range(4):
                if Animal[j][-1] == "S":
                    Animal[j] = Animal[j] + Animal[j][-2]
                else:
                    if Animal[j][-2] == "S":
                        Animal[j] = Animal[j] + "W"
                    else:
                        Animal[j] = Animal[j] + "S"
        else:
            for j in range(4):
                if Animal[j][-1] == "S":
                    if Animal[j][-2] == "S":
                        Animal[j] = Animal[j] + "W"
                    else:
                        Animal[j] = Animal[j] + "S"
                else:
                    Animal[j] = Animal[j] + Animal[j][-2]
    for i in range(4):
        animal = Animal[i]
        if s[-1] + s[0] == "xo":
            for a in ["WWWS", "WSSS", "SWSW", "SSWW"]:
                if animal[-2:] + animal[:2] == a:
                    print(animal)
                    return
            else:
                continue

        elif s[-1] + s[0] == "xx":
            for a in ["WWWW", "WSSW", "SWSS", "SSWS"]:
                if animal[-2:] + animal[:2] == a:
                    print(animal)
                    return
                else:
                    continue

        elif s[-1] + s[0] == "ox":
            for a in ["WWSS", "WSWS", "SSSW", "SWWW"]:
                if animal[-2:] + animal[:2] == a:
                    print(L)
                    return
        elif s[-1] + s[0] == "oo":
            for a in ["WWSW", "WSWW", "SSSS", "SWWS"]:
                if animal[-2:] + animal[:2] == a:
                    print(animal)
                    return
                else:
                    continue
    print(-1)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
s = [s[i] for i in range(n)]

answer = ''
current = 'S' # 1: sheep, -1: wolf
first_flag = False

for i,l in enumerate(s):
    if current == 'S':
        if l == 'x':
            if first_flag == False:
                answer += current
                current = 'W'
                first_flag = True
            else:
                answer += current
                first_flag = False
        else:
            answer += current
    elif current == 'W':
        if l == 'x':
            answer += current
        else:
            if first_flag == False:
                answer += current
                current = 'S'
                first_flag = True
            else:
                answer += current
                first_flag = False

if answer[0] == 'S':
    if s[0] == 'o':
        if answer[1] != answer[-1]:
            answer = -1
    elif s[0] == 'x':
        if answer[1] == answer[-1]:
            answer = -1
elif answer[0] == 'W':
    if s[0] == 'x':
        if answer[1] != answer[-1]:
            answer = -1
    elif s[0] == 'o':
        if answer[1] == answer[-1]:
            answer = -1

if answer[-1] == 'S':
    if s[-1] == 'o':
        if answer[1] != answer[-2]:
            answer = -1
    elif s[-1] == 'x':
        if answer[1] == answer[-2]:
            answer = -1
elif answer[-1] == 'W':
    if s[-1] == 'x':
        if answer[1] != answer[-2]:
            answer = -1
    elif s[-1] == 'o':
        if answer[1] == answer[-2]:
            answer = -1

print(answer)

Submission status: Runtime Error

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