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())
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])


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

Submission status: Wrong Answer

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'

Ans[0] = 'S'
for i in range(1,N):
    if i == 1:
        Ans[i] = 'S'
    else:
        Ans[i] = judge(Ans[i-2], Ans[i-1], t[i-1])

if judge(Ans[-1], Ans[0], t[-1]) is Ans[0]:
    print("".join(Ans))
else:
    for i in range(1,N):
        if i == 1:
            Ans[i] = 'W'
        else:
            Ans[i] = judge(Ans[i-2], Ans[i-1], t[i-1])
    if judge(Ans[-1], Ans[0], t[-1]) is Ans[0]:
        print("".join(Ans))
    else:
        Ans[0] = 'W'
        for i in range(1,N):
            if i == 1:
                Ans[i] = 'S'
            else:
                Ans[i] = judge(Ans[i-2], Ans[i-1], t[i-1])

        if judge(Ans[-1], Ans[0], t[-1]) is Ans[0]:
            print("".join(Ans))
        else:
            for i in range(1,N):
                if i == 1:
                    Ans[i] = 'W'
                else:
                    Ans[i] = judge(Ans[i-2], Ans[i-1], t[i-1])
            if judge(Ans[-1], Ans[0], t[-1]) is Ans[0]:
                print("".join(Ans))

Submission status: Wrong Answer

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'

Ans[0] = 'S'
for i in range(1,N):
    if i == 1:
        Ans[i] = 'S'
    else:
        Ans[i] = judge(Ans[i-2], Ans[i-1], t[i-1])

if judge(Ans[-1], Ans[0], t[-1]) is 'S':
    print("".join(Ans))
else:
    for i in range(1,N):
        if i == 1:
            Ans[i] = 'W'
        else:
            Ans[i] = judge(Ans[i-2], Ans[i-1], t[i-1])

    print("".join(Ans))

Submission status: Wrong Answer

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


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


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

    isCurrentSheep = firstIsSheep
    isPreviousSheep = lastIsSheep

    for i in range(len(responseArray) - 1):
        isNextSame = not (isCurrentSheep ^ responseArray[i])
        isNextSheep = not (isNextSame ^ isPreviousSheep)

        res_str += "S" if isNextSheep else "W"

        isCurrentSheep, isPreviousSheep = isNextSheep, isCurrentSheep

    isNextSame = not (isCurrentSheep ^ responseArray[-1])
    isNextSheep = not (isNextSame ^ isPreviousSheep)

    if isCurrentSheep != lastIsSheep or isNextSheep != 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(i)
            res = resolve_hypothesis(firstIsSheep, lastIsSheep, responseArray)

            if res == -1:
                continue

            print(res)
            return

    print(-1)
    return


main()

Submission status: Wrong Answer

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

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

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

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

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

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7


#############
# Main Code #
#############

def rand_letter(size):
    ascii_original='ox'
    digits_original='01'

    digits='0123456789'
    ascii_lowercase='abcdefghijklmnopqrstuvwxyz'
    ascii_uppercase='ABCDEFGHIJKLMNOPQRSTUVWXYZ'

    # 好きなものを使ってね
    psuedo = ascii_original

    return ''.join([random.choice(psuedo) for i in range(size)])

N = getN()
S = input()
lista = ['SS', 'SW', 'WS', 'WW']

def judge(string):
    alta = copy.deepcopy(string)
    for i in range(1, N - 1):
        if alta[-1] == "S" and alta[-2] == "S":
            if S[i] == "o":
                alta += 'S'
            else:
                alta += "W"
        elif alta[-1] == "S" and alta[-2] == "W":
            if S[i] == "o":
                alta += 'W'
            else:
                alta += "S"
        elif alta[-1] == "W" and alta[-2] == "S":
            if S[i] == "o":
                alta += 'W'
            else:
                alta += "S"
        else:
            if S[i] == "o":
                alta += 'S'
            else:
                alta += "W"

    return alta

def judge_2(before, now, after, j):
    if now == "S":
        if j == "o":
            if before == after:
                return True

        else:
            if before != after:
                return True

    else:
        if j == "o":
            if before != after:
                return True

        else:
            if before == after:
                return True
    return False

for i in lista:
    opt = judge(i)
    if judge_2(opt[-2], opt[-1], opt[0], S[-1]) and judge_2(opt[-1], opt[0], opt[1], S[0]):
        print(opt)
        exit()
print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
s += s[0]
d = {'S':'W', 'W':'S'}
flag = True
for sw in ['SS', 'WW', 'SW', 'WS']:
    l = sw
    for i in range(1, n+1):
        if l[i] == 'S' and s[i] == 'o' or l[i] == 'W' and s[i] == 'x':
            l += l[i-1]
        else:
            l += d[l[i-1]]
    if l[0] == l[-2] and l[1] == l[-1] :
        print(l[:-2])
        flag = False
        break
if flag:
    print(-1)

Submission status: Time Limit Exceeded

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):
        s+=s[0]
        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: Time Limit Exceeded

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()
    q = queue.Queue()
    for a in ["SW", "SS", "WS", "WW"]:
        q.put(a)
    while not q.empty():
        L = q.get()
        l = len(L)
        if l == N:
            if s[-1] + s[0] == "xo":
                for a in ["WWWS", "WSSS", "SWSW", "SSWW"]:
                    if L[-2:] + L[:2] == a:
                        print(L)
                        return
                else:
                    continue

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

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
s = input()
t = [1 if i == 'o' else 0 for i in s]
def generate(res):
    for i in range(1,n):
        if res[-1]:
            if t[i]:
                if res[-2]:
                    res.append(1)
                else:
                    res.append(0)
            else:
                if res[-2]:
                    res.append(0)
                else:
                    res.append(1)
        else:
            if t[i]:
                if res[-2]:
                    res.append(0)
                else:
                    res.append(1)
            else:
                if res[-2]:
                    res.append(1)
                else:
                    res.append(0)
    return res

def ret(ans):
    res = ''
    for i in ans:
        if i:
            res += 'S'
        else:
            res += 'W'
    return res

ans1 = [1]
ans2 = [1]
ans3 = [1]
ans4 = [1]
if t[0]:
    ans1.append(1)
    ans2.append(0)
    res1 = generate(ans1)
    res2 = generate(ans2)
    if res1[-1] == ans1[0] and ans1[1] == res1[-2]:
        print(ret(res1[:-1]))
        quit()
    elif res2[-1] == ans2[0] and ans2[1] == res2[-2]:
        print(ret(res2[:-1]))
        quit()
else:
    ans3.append(1)
    ans4.append(0)
    res3 = generate(ans3)
    res4 = generate(ans4)
    if res3[-1] == ans3[0] and ans3[1] != res3[-2]:
        print(ret(res3[:-1]))
        quit()
    elif res4[-1] == ans4[0] and ans4[1] != res4[-2]:
        print(ret(res4[:-1])) 
        quit()


ans5 = [0]
ans6 = [0]
ans7 = [0]
ans8 = [0]
if t[0]:
    ans5.append(1)
    ans6.append(0)
    res5 = generate(ans5)
    res6 = generate(ans6)
    if res5[-1] == ans5[0] and ans5[1] != res5[-2]:
        print(ret(res5[:-1]))
        quit()
    elif res6[-1] == ans6[0] and ans6[1] != res6[-2]:
        print(ret(res6[:-1]))
        quit()

else:
    ans7.append(1)
    ans8.append(0)
    res7 = generate(ans7)
    res8 = generate(ans8)
    if res7[-1] == ans7[0] and ans7[1] == res7[-2]:
        print(ret(res7[:-1]))
        quit()
    elif res8[-1] == ans8[0] and ans8[1] == res8[-2]:
        print(ret(res8[:-1]))
        quit()

print(-1)

Submission status: Time Limit Exceeded

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

def main():
    import itertools
    def check(c):
        for i in range(n):
            if s[i] == 'o':
                if c[i]==0:
                    if i==0:
                        if c[1]!=c[-1]:
                            return False
                    elif i == n-1:
                        if c[-2]!=c[0]:
                            return False
                    else:
                        if c[i-1]!=c[i+1]:
                            return False
                else:
                    if i==0:
                        if c[1]==c[-1]:
                            return False
                    elif i == n-1:
                        if c[-2]==c[0]:
                            return False
                    else:
                        if c[i-1]==c[i+1]:
                            return False

            else:
                if c[i]==0:
                    if i==0:
                        if c[1]==c[-1]:
                            return False
                    elif i == n-1:
                        if c[-2]==c[0]:
                            return False
                    else:
                        if c[i-1]==c[i+1]:
                            return False
                else:
                    if i==0:
                        if c[1]!=c[-1]:
                            return False
                    elif i == n-1:
                        if c[-2]!=c[0]:
                            return False
                    else:
                        if c[i-1]!=c[i+1]:
                            return False
        return True


    status = [[0,1] for _ in range(n)]
    for c in itertools.product(*status):
        if check(c)==True:
            c = list(map(lambda x: 'S' if x==0 else 'W',c))
            print(''.join(c))
            return
    print(-1)
    return

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
a = "SS"
ct=1
while ct<7:    
    for i in range(n-1):
        p=a[i]
        q="A"
        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]:
        if a[0]=="S":
            if s[0]=="o":
                if a[n-1]==a[1]:
                    print(a[0:n])
                    break
                else:
                    pass
            else:
                if a[n-1]==a[1]:
                    pass
                else:
                    print(a[0:n])
                    break
        else:
            if s[0]=="o":
                if a[n-1]==a[1]:
                    pass
                else:
                    print(a[0:n])
                    break
            else:
                if a[n-1]==a[1]:
                    print(a[0:n])
                    break
                else:
                    pass
    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: Time Limit Exceeded

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

for bit in range(4):
    animal = [-1] * N
    animal[0] = bit % 2
    animal[1] = bit // 2
    for i in range(1, N-1):
        if (animal[i] == 0) ^ (s[i] == 'o'):
            animal[i+1] = 1 - animal[i-1]
        else:
            animal[i+1] = animal[i-1]
    ok = True
    if animal[-1] == 0:
        if (s[-1] == 'o') ^ (animal[-2] == animal[0]):
            ok = False
    if animal[-1] == 1:
        if (s[-1] == 'o') ^ (animal[-2] != animal[0]):
            ok = False
    if animal[0] == 0:
        if (s[0] == 'o') ^ (animal[-1] == animal[1]):
            ok = False
    if animal[0] == 1:
        if (s[0] == 'o') ^ (animal[-1] != animal[1]):
            ok = False
    if ok:
        ans = ''
        for i in range(N):
            ans += 'S' if animal[i] == 0 else 'W'
        print(ans)
        exit()
print(-1)

Submission status: Time Limit Exceeded

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

    def make(S0, S1) -> str:
        rec = S0+S1
        for i in range(1, N-1):
            if S[i] == "o" and rec[i] == "S":
                rec = rec + rec[i-1]
            elif S[i] == "o" and rec[i] == "W":
                if rec[i-1] == "S":
                    rec = rec + "W"
                else:
                    rec = rec + "S"
            elif S[i] == "x" and rec[i] == "S":
                if rec[i-1] == "S":
                    rec = rec + "W"
                else:
                    rec = rec + "S"
            elif S[i] == "x" and rec[i] == "W":
                rec = rec + rec[i-1]

        return rec

    def checker(rec) -> bool:
        for i in range(N-1):
            if S[i] == "o" and rec[i] == "S":
                if rec[i-1] != rec[i+1]:
                    return False
            elif S[i] == "o" and rec[i] == "W":
                if rec[i-1] == rec[i+1]:
                    return False
            elif S[i] == "x" and rec[i] == "S":
                if rec[i-1] == rec[i+1]:
                    return False
            elif S[i] == "x" and rec[i] == "W":
                if rec[i-1] != rec[i+1]:
                    return False
        if S[N-1] == "o" and rec[N-1] == "S":
            return rec[N-2] == rec[0]
        elif S[N-1] == "o" and rec[N-1] == "W":
            return rec[N-2] != rec[0]
        elif S[N-1] == "x" and rec[N-1] == "S":
            return rec[N-2] != rec[0]
        elif S[N-1] == "x" and rec[N-1] == "W":
            return rec[N-2] == rec[0]

    for S0, S1 in (("S", "S"), ("S", "W"), ("W", "S"), ("W", "W")):
        rec = make(S0, S1)
        if checker(rec):
            print(rec)
            break
    else:
        print(-1)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
S += S[0]
for key in ['SS', 'SW', 'WS', 'WW']:
    animals = key
    for i in range(N + 1):
        if i < 1:
            continue
        if animals[i] == 'S':
            if S[i] == 'o':
                animals += animals[-2]
            else:
                animals += 'S' if animals[-2] == 'W' else 'W'
        else:
            if S[i] == 'x':
                animals += animals[-2]
            else:
                animals += 'S' if animals[-2] == 'W' else 'W'
    if animals[0] == animals[-2] and animals[1] == animals[-1]:
        print(animals[:-2])
        exit(0)
print(-1)

Submission status: Time Limit Exceeded

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

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

for t in T:
  for i in range(1, N-1):
    if (t[i] == "S" and S[i] == "o") or (t[i] == "W" and S[i] == "x"):
        t += t[i-1]
    else:
        t += "S" if t[i-1] == "W" else "W"
  #print(t)
  
  flag = 1
  if (t[0] == "S" and S[0] == "o") or (t[0] == "W" and S[0] == "x"):
    if t[-1] != t[1]: flag = 0
  else:
    if t[-1] == t[1]: flag = 0
  if (t[-1] == "S" and S[-1] == "o") or (t[-1] == "W" and S[-1] == "x"):
    if t[-2] != t[0]: flag = 0
  else:
    if t[-2] == t[0]: flag = 0
  if flag:
    print(t)
    exit()
print(-1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
a=["SS","SW","WS","WW"]
for i in range(n):
    for j in range(4):
        a[j]+="SW"[::-1 if s[i]=='o' else 1][a[j][i]==a[j][i+1]]
for i in range(4):
    if a[i][1]==a[i][n+1] and a[i][0] == a[i][n]:
        print(a[i][1:n+1])
        exit()
print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
S += S[0:2]

for ans in ['SS','SW','WS','WW']:
    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:]:
        print(ans[:N])
        exit()
print(-1)

Submission status: Time Limit Exceeded

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

l = ['SS', 'SW', 'WS', 'WW']

for i in range(4):
  ans = l[i]
  for j in range(1, n):
    if ans[j] == 'S' and s[j] == 'o':
      if ans[j-1] == 'S':
        ans += 'S'
      else:
        ans += 'W'
    elif ans[j] == 'S' and s[j] == 'x':
      if ans[j-1] == 'S':
        ans += 'W'
      else:
        ans += 'S'
    elif ans[j] == 'W' and s[j] == 'o':
      if ans[j-1] == 'S':
        ans += 'W'
      else:
        ans += 'S'
    else:
      if ans[j-1] == 'S':
        ans += 'S'
      else:
        ans += 'W' 
  flag = 0
  if ans[-1] == ans[0]:
    if ans[0] == 'S' and s[0] == 'o':
      if ans[-2] == ans[1]:
        flag = 1
    elif ans[0] == 'W' and s[0] == 'o':
      if ans[-2] != ans[1]:
        flag = 1
    elif ans[0] == 'S' and s[0] == 'x':
      if ans[-2] != ans[1]:
        flag = 1
    else:
      if ans[-2] == ans[1]:
        flag = 1
  if flag == 1:
    print(ans[:-1])
    exit()
else:
  print('-1')

Submission status: Time Limit Exceeded

Solution code:
def tami(list):
  ans = ""
  for i in range(N):
    if list[i] == 1:
      ans += "S"
    else:
      ans += "W"
  else:
    print(ans)

N = int(input())
s = input()
s += s[:2]

dp = [1,1] # 1:羊, -1:狼
for i in range(1,N+1):
  if dp[i] == 1:
    if s[i] == "o":
      dp.append(dp[i - 1])
    else:
      dp.append(dp[i - 1]*(-1))
  else:
    if s[i] == "o":
      dp.append(dp[i - 1]*(-1))
    else:
      dp.append(dp[i - 1])
else:
  if dp[0] == dp[-2] and dp[1] == dp[-1]:
    tami(dp)
    exit()

dp = [1,-1] # 1:羊, -1:狼
for i in range(1,N+1):
  if dp[i] == 1:
    if s[i] == "o":
      dp.append(dp[i - 1])
    else:
      dp.append(dp[i - 1]*(-1))
  else:
    if s[i] == "o":
      dp.append(dp[i - 1]*(-1))
    else:
      dp.append(dp[i - 1])
else:
  if dp[0] == dp[-2] and dp[1] == dp[-1]:
    tami(dp)
    exit()

dp = [-1,1] # 1:羊, -1:狼
for i in range(1,N+1):
  if dp[i] == 1:
    if s[i] == "o":
      dp.append(dp[i - 1])
    else:
      dp.append(dp[i - 1]*(-1))
  else:
    if s[i] == "o":
      dp.append(dp[i - 1]*(-1))
    else:
      dp.append(dp[i - 1])
else:
  if dp[0] == dp[-2] and dp[1] == dp[-1]:
    tami(dp)
    exit()

dp = [-1,-1] # 1:羊, -1:狼
for i in range(1,N+1):
  if dp[i] == 1:
    if s[i] == "o":
      dp.append(dp[i - 1])
    else:
      dp.append(dp[i - 1]*(-1))
  else:
    if s[i] == "o":
      dp.append(dp[i - 1]*(-1))
    else:
      dp.append(dp[i - 1])
else:
  if dp[0] == dp[-2] and dp[1] == dp[-1]:
    tami(dp)
    exit()

print(-1)

Submission status: Time Limit Exceeded

Solution code:
def tami(list):
  ans = ""
  for i in range(N):
    if list[i] == 1:
      ans += "S"
    else:
      ans += "W"
  else:
    print(ans)

N = int(input())
s = input()
s += s[:2]

dp = [0]*(N+2) # 1:羊, -1:狼
dp[0] = 1
dp[1] = 1
for i in range(1,N+1):
  if dp[i] == 1:
    if s[i] == "o":
      dp[i + 1] = dp[i - 1]
    else:
      dp[i + 1] = dp[i - 1]*(-1)
  else:
    if s[i] == "o":
      dp[i + 1] = dp[i - 1]*(-1)
    else:
      dp[i + 1] = dp[i - 1]
else:
  if dp[0] == dp[-2] and dp[1] == dp[-1]:
    tami(dp)
    exit()

dp = [0]*(N+2) # 1:羊, -1:狼
dp[0] = 1
dp[1] = -1
for i in range(1,N+1):
  if dp[i] == 1:
    if s[i] == "o":
      dp[i + 1] = dp[i - 1]
    else:
      dp[i + 1] = dp[i - 1]*(-1)
  else:
    if s[i] == "o":
      dp[i + 1] = dp[i - 1]*(-1)
    else:
      dp[i + 1] = dp[i - 1]
else:
  if dp[0] == dp[-2] and dp[1] == dp[-1]:
    tami(dp)
    exit()

dp = [0]*(N+2) # 1:羊, -1:狼
dp[0] = -1
dp[1] = 1
for i in range(1,N+1):
  if dp[i] == 1:
    if s[i] == "o":
      dp[i + 1] = dp[i - 1]
    else:
      dp[i + 1] = dp[i - 1]*(-1)
  else:
    if s[i] == "o":
      dp[i + 1] = dp[i - 1]*(-1)
    else:
      dp[i + 1] = dp[i - 1]
else:
  if dp[0] == dp[-2] and dp[1] == dp[-1]:
    tami(dp)
    exit()

dp = [0]*(N+2) # 1:羊, -1:狼
dp[0] = -1
dp[1] = -1
for i in range(1,N+1):
  if dp[i] == 1:
    if s[i] == "o":
      dp[i + 1] = dp[i - 1]
    else:
      dp[i + 1] = dp[i - 1]*(-1)
  else:
    if s[i] == "o":
      dp[i + 1] = dp[i - 1]*(-1)
    else:
      dp[i + 1] = dp[i - 1]
else:
  if dp[0] == dp[-2] and dp[1] == dp[-1]:
    tami(dp)
    exit()

print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
s=input().rstrip()
start=['SS','SW','WS','WW']
import sys
for k in range(4):
    ans=start[k]
    for i in range(2,n):
        if (ans[i-2:i]=='SS' and s[i-1]=='o') or (ans[i-2:i]=='SW' and s[i-1]=='x') or (ans[i-2:i]=='WS' and s[i-1]=='x') or (ans[i-2:i]=='WW' and s[i-1]=='o'):
            ans=ans+'S'
        else:
            ans=ans+'W'
    if (s[0]=='o' and ans[0]=='S') or (s[0]=='x' and ans[0]=='W'):
        if ans[1]==ans[n-1]:
            pass
        else:
            continue
    else:
        if ans[1]==ans[n-1]:
            continue
    if (s[n-1]=='o' and ans[n-1]=='S') or (s[n-1]=='x' and ans[n-1]=='W'):
        if ans[0]==ans[n-2]:
            pass
        else:
            continue
    else:
        if ans[0]==ans[n-2]:
            continue
    print(ans)
    sys.exit()
print(-1)