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())
s = input()

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1]*(n+1)
        self.rnk = [0]*(n+1)
        
    def find_root(self, x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find_root(self.root[x])
            return self.root[x]
        
    def unite(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return
        elif self.rnk[x] > self.rnk[y]:
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if self.rnk[x] == self.rnk[y]:
                self.rnk[y] += 1
                
    def isSameGroup(self, x, y):
        return self.find_root(x) == self.find_root(y)
    
    def count(self, x):
        return -self.root[self.find_root(x)]
    
uf0 = UnionFind(N+1) #1が羊、2が羊だとした場合のグループ分け
uf1 = UnionFind(N+1) #1が羊、2が狼だとした場合のグループ分け
uf2 = UnionFind(N+1) #1が狼、2が羊だとした場合のグループ分け
uf3 = UnionFind(N+1) #1が狼、2が狼だとした場合のグループ分け

#0は羊、N+1は狼のグループ
uf0.unite(1, 0)
uf0.unite(2, 0)
uf1.unite(1, 0)
uf1.unite(2, N+1)
uf2.unite(1, N+1)
uf2.unite(2, 0)
uf3.unite(1, N+1)
uf3.unite(1, N+1)

if s[0] == "o":
    uf0.unite(N, 0)
    uf1.unite(N, N+1)
    uf2.unite(N, N+1)
    uf3.unite(N, 0)
else:
    uf0.unite(N, N+1)
    uf1.unite(N, 0)
    uf2.unite(N, 0)
    uf3.unite(N, N+1)
    
for i in range(2, N+1):
    if i == N:
        a = 1
    else:
        a = i+1
        
    if s[i-1] == "o":
        if uf0.isSameGroup(i, 0):
            uf0.unite(i-1, a)
        elif uf0.isSameGroup(i-1, 0):
            uf0.unite(a, N+1)
        else:
            uf0.unite(a, 0)
        
        if uf1.isSameGroup(i, 0):
            uf1.unite(i-1, a)
        elif uf1.isSameGroup(i-1, 0):
            uf1.unite(a, N+1)
        else:
            uf1.unite(a, 0)
            
        if uf2.isSameGroup(i, 0):
            uf2.unite(i-1, a)
        elif uf2.isSameGroup(i-1, 0):
            uf2.unite(a, N+1)
        else:
            uf2.unite(a, 0)
            
        if uf3.isSameGroup(i, 0):
            uf3.unite(i-1, a)
        elif uf3.isSameGroup(i-1, 0):
            uf3.unite(a, N+1)
        else:
            uf3.unite(a, 0)
            
    else:
        if not uf0.isSameGroup(i, 0):
            uf0.unite(i-1, a)
        elif uf0.isSameGroup(i-1, 0):
            uf0.unite(a, N+1)
        else:
            uf0.unite(a, 0)
            
        if not uf1.isSameGroup(i, 0):
            uf1.unite(i-1, a)
        elif uf1.isSameGroup(i-1, 0):
            uf1.unite(a, N+1)
        else:
            uf1.unite(a, 0)
            
        if not uf2.isSameGroup(i, 0):
            uf2.unite(i-1, a)
        elif uf2.isSameGroup(i-1, 0):
            uf2.unite(a, N+1)
        else:
            uf2.unite(a, 0)
            
        if not uf3.isSameGroup(i, 0):
            uf3.unite(i-1, a)
        elif uf3.isSameGroup(i-1, 0):
            uf3.unite(a, N+1)
        else:
            uf3.unite(a, 0)
            
ans = -1
if not uf0.isSameGroup(0, N+1):
    ans = uf0
elif not uf1.isSameGroup(0, N+1):
    ans = uf1
elif not uf2.isSameGroup(0, N+1):
    ans = uf2
elif not uf3.isSameGroup(0, N+1):
    ans = uf3
    
if ans == -1:
    print(ans)
else:
    t = []
    for i in range(1, N+1):
        if ans.isSameGroup(i, 0):
            t.append("S")
        else:
            t.append("W")
    print("".join(t))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n=int(input())
sw=[0 for i in range(n)]
ox=list(input())

def who(x):
    if sw[x-1]=="S":
        a=1
    else:
        a=-1
        
    if sw[x]=="S":
        b=1
    else:
        b=-1
    
    if ox[x]=="o":
        c=1
    else:
        c=-1
    
    if a*b*c==1:
        return "S"
    else:
        return "W"
   
        
start=[["S","S"],["S","W"],["W","S"],["W","W"]]
ans=-1

for i in range(4):
    sw[0],sw[-1]=start[i]
    tmp1=sw[-1]
    tmp2=sw[0]

    for j in range(n):
        sw[(j+1)%n]=who(j)

    if tmp1==sw[-1] and tmp2==sw[0]:
        ans="".join(sw)
    
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    S=list(map(lambda x:(x=='o')-0,input()))

    # 4パターン試す
    from itertools import product
    for a,b in product(range(2),repeat=2):
        ans=[None]*n
        ans[0]=a; ans[1]=b

        for i in range(2,n):
            if(S[i-1]==ans[i-1]): ans[i]=ans[i-2]
            else: ans[i]=1-ans[i-2]

        if((S[0]==ans[0])==(ans[1]==ans[-1]) and (S[-1]==ans[-1])==(ans[0]==ans[-2])):
            for s in ans: print('S' if(s) else 'W',end='')
            print()
            return
    print(-1)
resolve()

Submission status: Accepted

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

def sw(animal,maeanimal,flag):
    if animal == "S":
        if maeanimal == "S":
            if flag == "o":
                return "S"
            else:
                return "W"
        else:
            if flag == "o":
                return "W"
            else:
                return "S"
    else:
        if maeanimal == "S":
            if flag == "o":
                return "W"
            else:
                return "S"
        else:
            if flag == "o":
                return "S"
            else:
                return "W"
def main():
    r = ["" for _ in range(n)]
    r[0] = "S"
    if s[0] == "o":
        r[1] = "S"
        r[-1]= "S"
    else:
        r[1] = "S"
        r[-1] = "W"

    for i in range(1,n-2):
        r[i+1] = sw(r[i],r[i-1],s[i])
    if r[n-1] == sw(r[n-2],r[n-3],s[n-2]) and r[0] == sw(r[n-1],r[n-2],s[n-1]):
        return "".join(map(str, r))

    r = ["" for _ in range(n)]
    r[0] = "S"
    if s[0] == "o":
        r[1] = "W"
        r[-1]= "W"
    else:
        r[1] = "W"
        r[-1] = "S"

    for i in range(1,n-2):
        r[i+1] = sw(r[i],r[i-1],s[i])
    if r[n-1] == sw(r[n-2],r[n-3],s[n-2]) and r[0] == sw(r[n-1],r[n-2],s[n-1]):
        return "".join(map(str, r))

    r = ["" for _ in range(n)]
    r[0] = "W"
    if s[0] == "o":
        r[1] = "S"
        r[-1]= "W"
    else:
        r[1] = "S"
        r[-1] = "S"

    for i in range(1,n-2):
        r[i+1] = sw(r[i],r[i-1],s[i])
    if r[n-1] == sw(r[n-2],r[n-3],s[n-2]) and r[0] == sw(r[n-1],r[n-2],s[n-1]):
        return "".join(map(str, r))

    r = ["" for _ in range(n)]
    r[0] = "W"
    if s[0] == "o":
        r[1] = "W"
        r[-1]= "S"
    else:
        r[1] = "W"
        r[-1] = "W"

    for i in range(1,n-2):
        r[i+1] = sw(r[i],r[i-1],s[i])
    if r[n-1] == sw(r[n-2],r[n-3],s[n-2]) and r[0] == sw(r[n-1],r[n-2],s[n-1]):
        return "".join(map(str, r))

    return "-1"

print(main())

Submission status: Accepted

Solution code:
import sys


def main():
    N = int(input())
    s = list(input())
    s.append(s[0])
    s.insert(0, s[-2])

    init = ((0, 0), (0, 1), (1, 0), (1, 1))
    for i in init:
        p = [0] * (N+2)
        p[0] = i[0]
        p[1] = i[1]
        for j in range(1, N+1):
            if s[j] == "o":
                if p[j] == 0:
                    p[j+1] = p[j-1] ^ 0
                else:
                    p[j+1] = p[j-1] ^ 1
            else:
                if p[j] == 0:
                    p[j+1] = p[j-1] ^ 1
                else:
                    p[j+1] = p[j-1] ^ 0

        if p[0] == p[-2] and p[1] == p[-1]:
            ss = list(map(lambda x: "W" if x == 1 else "S", p[1:N+1]))
            print(''.join(ss))
            sys.exit()
    else:
        print(-1)


main()

Submission status: Accepted

Solution code:
n=int(input())
s=input()
animals=["#"]*len(s)

def oppositeAnimal(animal):
  if animal=="S":
    return "W"
  else:
    return "S"

def getNextAnimal(animal,answer,pastanimal):
  if animal=="S":
    if answer=="o":
      return pastanimal
    else:
      return oppositeAnimal(pastanimal)
  else:
    if answer=="o":
      return oppositeAnimal(pastanimal)
    else:
      return pastanimal

for a0 in ("S","W"):
  for a1 in ("S","W"):
    animals=["#"]*len(s)
    animals[0]=a0
    animals[1]=a1
    for i in range(2,len(s)):
      animals[i]=getNextAnimal(animals[i-1],s[i-1],animals[i-2])
    if getNextAnimal(animals[len(s)-1],s[len(s)-1],animals[len(s)-2])==animals[0]:
      if getNextAnimal(animals[0],s[0],animals[len(s)-1])==animals[1]:
        print("".join(animals))
        exit(0)
print(-1)

Submission status: Accepted

Solution code:
def match(index_num):
    if answer[index_num] == "S":
        if s[index_num] == "o":
            if answer[(index_num+1)%N] == answer[index_num-1]:
                return True
        else:
            if answer[(index_num+1)%N] != answer[index_num-1]:
                return True
    else:
        if s[index_num] == "o":
            if answer[(index_num+1)%N] != answer[index_num-1]:
                return True
        else:
            if answer[(index_num+1)%N] == answer[index_num-1]:
                return True
    return False
 
N = int(input())
s = list(input())
SorW = ["S","W"]
 
for i in SorW:
    for j in SorW:
        answer = []
        append = answer.append
        append(i)
        append(j)
 
        for k in range(2,N):
            if answer[k-1] == "S":
                if s[k-1] == "o":
                    append(answer[k-2])
                else:
                    if answer[k-2] == "S":
                        append("W")
                    else:
                        append("S")
            else:
                if s[k-1] == "o":
                    if answer[k-2] == "W":
                        append("S")
                    else:
                        append("W")
                else:
                    append(answer[k-2])
        
        for k in [0,N-1]:
            if not match(k):
                break
            if k == N-1:
                print("".join(answer))
                exit()
print(-1)

Submission status: Accepted

Solution code:
N = int(input())
s = list(input())
one_two_pair = (["S", "S"], ["S", "W"], ["W", "S"], ["W", "W"])
for one_two in one_two_pair:
    ans = one_two
    for i in range(1, N - 1):
        left = ans[i - 1]
        now = ans[i]
        ox = s[i]
        if now == "S":
            if ox == "o":
                if left == "S":
                    ans.append("S")
                else:
                    ans.append("W")
            else:
                if left == "S":
                    ans.append("W")
                else:
                    ans.append("S")
        elif now == "W":
            if ox == "o":
                if left == "S":
                    ans.append("W")
                else:
                    ans.append("S")
            else:
                if left == "S":
                    ans.append("S")
                else:
                    ans.append("W")
    first, last = False, False
    left = ans[-1]
    now = ans[0]
    right = ans[1]
    if now == "S":
        if s[0] == "o":
            first = (left == right)
        else:
            first = (left != right)
    else:
        if s[0] == "o":
            first = (left != right)
        else:
            first = (left == right)

    left = ans[-2]
    now = ans[-1]
    right = ans[0]
    if now == "S":
        if s[-1] == "o":
            last = (left == right)
        else:
            last = (left != right)
    else:
        if s[-1] == "o":
            last = (left != right)
        else:
            last = (left == right)

    if first and last:
        print("".join(ans))
        break

else:
    print(-1)

Submission status: Accepted

Solution code:
import copy

def sol(animals):
  for i, rep in enumerate(reps[:N-2]):
    if i == 0: continue
    if animals[i] == 1:animals[i+1] = rep*animals[i-1]
    else:animals[i+1] = -rep*animals[i-1]

  if animals[-2] == 1 and animals[-1] != reps[-2]*animals[-3]: return False
  elif  animals[-2] == -1 and animals[-1] != -reps[-2]*animals[-3]: return False
      
  if animals[-1] == 1 and animals[0] != reps[-1]*animals[-2]:return False
  elif animals[-1] == -1 and animals[0] != -reps[-1]*animals[-2]: return False

  return animals

N = int(input())
reps = [1 if s=='o' else -1 for s in input()]
animals = [0 for _ in range(N)]

if reps[0] == 1:
  cases = [[1,1,1], [-1,1,-1],[1,-1,-1],[-1,-1,1]]
else:
  cases = [[-1,1,1],[1,1,-1],[1,-1,1],[-1,-1,-1]]
  
for case in cases:
  animals1 = copy.deepcopy(animals)
  animals1[-1],animals1[0],animals1[1] = case #1,1,1
  res = sol(animals1)
  if res is not False:
    print(''.join(['S' if s==1 else 'W' for s in res]))
    exit()
print(-1)

Submission status: Accepted

Solution code:
import sys
n=int(input())
s=list(input())
for i in range(n):
    if s[i]=="o":
        s[i]=0
    else:
        s[i]=1
s.append(s[0])
#"1"は羊、"0"は狼
data=["11","10","01","00"]
anser=""
for ans in data:
    qqq=ans
        
    for i in range(1,n+1):
        if ans[i]=="1":
            ans+=str((int(ans[i-1])+s[i])%2)
        else:
            ans+=str((int(ans[i-1])+s[i]+1)%2)
    if ans[n:]==qqq:
        anser=ans[:n]
        break
else:
    print(-1)
    sys.exit()
anser=list(anser)
for i in range(n):
    if anser[i]=="1":
        anser[i]="S"
    else:
        anser[i]="W"
print("".join(anser))

Submission status: Accepted

Solution code:
def examD():
    N = I()
    s = S()
    ans = ""
    animal = [None, "S", "W"]
    ansC = ""
    for i in range(4):
        cur = ""
        if i==0:
            ansC = [1,1]
        elif i==1:
            ansC = [1,-1]
        elif i==2:
            ansC = [-1,1]
        elif i==3:
            ansC = [-1,-1]
        if (s[0]=="o" and ansC[0]==1) or (s[0]=="x" and ansC[0]==(-1)):
            cur = ansC[1]
        else:
            cur = ansC[1]*(-1)

        for j in range(1,N):
            if (s[j]=="o" and ansC[j]==1) or (s[j]=="x" and ansC[j]==(-1)):
                ansC.append(ansC[j-1])
            else:
                ansC.append(ansC[j-1]*(-1))
        if ansC[N-1]==cur and ansC[N]==ansC[0]:
            for i in ansC[:N]:
                ans = ans + animal[i]
            break
    if not ans:
        ans = int(-1)
    print(ans)


import sys
import copy
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 18
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007

n = I()
s = S()
L = [0] * n


# 0が狼、1が羊
def main():
    for i, j in [(1, 0), (0, 1), (0, 0), (1, 1)]:
        L[0] = i
        L[1] = j
        for k in range(1, n - 1):
            if (s[k] == 'o' and L[k]) or (s[k] == 'x' and not L[k]):
                L[k + 1] = L[k - 1]
            else:
                L[k + 1] = L[k - 1] ^ 1

        if (s[n - 1] == 'o' and L[n - 1]) or (s[n - 1] == 'x' and not L[n - 1]):
            first = L[n - 2]
        else:
            first = L[n - 2] ^ 1
        if (s[0] == 'o' and L[0]) or (s[0] == 'x' and not L[0]):
            second = L[n - 1]
        else:
            second = L[n - 1] ^ 1
        if first == L[0] and second == L[1]:
            return L


L = main()
if L:
    print(''.join(['S' if i else 'W' for i in L]))
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input().rstrip('\n'))
    s = [True if s == "o" else False for s in list(str(input().rstrip('\n')))]
    ls = [[0] * n for _ in range(4)]
    ls[0][0] = False
    ls[0][1] = False
    ls[1][0] = False
    ls[1][1] = True
    ls[2][0] = True
    ls[2][1] = False
    ls[3][0] = True
    ls[3][1] = True
    for i in range(1, n-1):
        ls[0][i+1] = (s[i] == ls[0][i]) == ls[0][i-1]
        ls[1][i+1] = (s[i] == ls[1][i]) == ls[1][i-1]
        ls[2][i+1] = (s[i] == ls[2][i]) == ls[2][i-1]
        ls[3][i+1] = (s[i] == ls[3][i]) == ls[3][i-1]
    for i in range(4):
        if (s[-1] == ls[i][-1] and ls[i][0] == ls[i][-2]) or (s[-1] != ls[i][-1] and ls[i][0] != ls[i][-2]):
            if (s[0] == ls[i][0] and ls[i][1] == ls[i][-1]) or (s[0] != ls[i][0] and ls[i][1] != ls[i][-1]):
                print("".join(["S" if ls[i][j] else "W" for j in range(n)]))
                exit()
    print(-1)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
def make(l,ai): # len(a)=2
    global N
    for i in range(2,N):
        ai.append(ai[i-2]==(ai[i-1]==l[i-1]))
    return ai

def check(l,ai):
    return ai[0]==(ai[-2]==(ai[-1]==l[-1])) and ai[1]==(ai[-1]==(ai[0]==l[0]))

N=int(input())
l=[c=='o' for c in input()]

f=False
a=[[True,True],[True,False],[False,True],[False,False]]
for ai in a:
    ai=make(l,ai)

f=False
for ai in a:
    if check(l,ai):
        for aij in ai:
            print('S' if aij else 'W',end='')
        f=True
        break
if not f:
    print(-1)

Submission status: Accepted

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

opposite = {"S": "W",
            "W": "S"}

for ans in ["SS","SW","WS","WW"]:
  for i in range(1,N-1):
    if (ans[i],S[i]) in [("S","o"),("W","x")]:
      ans += ans[i-1]
    elif (ans[i],S[i]) in [("S","x"),("W","o")]:
      ans += opposite[ans[i-1]]
  
  for i in [-1,0]:
    if ans[i] == "S":
      if (S[i] == "o" and ans[i-1] != ans[i+1]) or (S[i] == "x" and ans[i-1] == ans[i+1]):
        break
    else:
      if (S[i] == "o" and ans[i-1] == ans[i+1]) or (S[i] == "x" and ans[i-1] != ans[i+1]):
        break
  else:
    print(ans)
    break
else:
  print(-1)

Submission status: Accepted

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

opposite = {"S": "W",
            "W": "S"}

for ans in ["SS","SW","WS","WW"]:
  for i in range(1,N-1):
    if ans[i] == "S" and S[i] == "o":
      ans += ans[i-1]
    elif ans[i] == "S" and S[i] == "x":
      ans += opposite[ans[i-1]]
    elif ans[i] == "W" and S[i] == "o":
      ans += opposite[ans[i-1]]
    elif ans[i] == "W" and S[i] == "x":
      ans += ans[i-1]
  
  for i in [-1,0]:
    if ans[i] == "S":
      if (S[i] == "o" and ans[i-1] != ans[i+1]) or (S[i] == "x" and ans[i-1] == ans[i+1]):
        break
    else:
      if (S[i] == "o" and ans[i-1] == ans[i+1]) or (S[i] == "x" and ans[i-1] != ans[i+1]):
        break
  else:
    print(ans)
    break
else:
  print(-1)

Submission status: Accepted

Solution code:
global S,ans

def check_at(i):
  global S,ans

  if ans[i] == "S":
    if (S[i] == "o" and ans[i-1] != ans[i+1]) or (S[i] == "x" and ans[i-1] == ans[i+1]):
      return True
  else:
    if (S[i] == "o" and ans[i-1] == ans[i+1]) or (S[i] == "x" and ans[i-1] != ans[i+1]):
      return True

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

opposite = {"S": "W",
            "W": "S"}

for ans in ["SS","SW","WS","WW"]:
  for i in range(1,N-1):
    if ans[i] == "S" and S[i] == "o":
      ans += ans[i-1]
    elif ans[i] == "S" and S[i] == "x":
      ans += opposite[ans[i-1]]
    elif ans[i] == "W" and S[i] == "o":
      ans += opposite[ans[i-1]]
    elif ans[i] == "W" and S[i] == "x":
      ans += ans[i-1]
  
  if (not check_at(-1)) and (not check_at(0)):
    print(ans)
    break
else:
  print(-1)

Submission status: Accepted

Solution code:
def check_at(i,ans,s):
  if ans[i] == "S":
    if (s[i] == "o" and ans[i-1] != ans[i+1]) or (s[i] == "x" and ans[i-1] == ans[i+1]):
      return True
  else:
    if (s[i] == "o" and ans[i-1] == ans[i+1]) or (s[i] == "x" and ans[i-1] != ans[i+1]):
      return True

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

opposite = {"S": "W",
            "W": "S"}

for ans in ["SS","SW","WS","WW"]:
  for i in range(1,N-1):
    if ans[i] == "S" and S[i] == "o":
      ans += ans[i-1]
    elif ans[i] == "S" and S[i] == "x":
      ans += opposite[ans[i-1]]
    elif ans[i] == "W" and S[i] == "o":
      ans += opposite[ans[i-1]]
    elif ans[i] == "W" and S[i] == "x":
      ans += ans[i-1]
  
  if (not check_at(-1,ans,S)) and (not check_at(0,ans,S)):
    print(ans)
    break
else:
  print(-1)

Submission status: Accepted

Solution code:
global opposite
opposite = {"S": "W",
            "W": "S"}

def generate(ans,s,N):
  global opposite
  for i in range(1,N-1):
    if ans[i] == "S" and s[i] == "o":
      ans += ans[i-1]
    elif ans[i] == "S" and s[i] == "x":
      ans += opposite[ans[i-1]]
    elif ans[i] == "W" and s[i] == "o":
      ans += opposite[ans[i-1]]
    elif ans[i] == "W" and s[i] == "x":
      ans += ans[i-1]
  
  if ans[-1] == "S":
    if (s[-1] == "o" and ans[-2] != ans[0]) or (s[-1] == "x" and ans[-2] == ans[0]):
      return True
  if ans[-1] == "W":
    if (s[-1] == "o" and ans[-2] == ans[0]) or (s[-1] == "x" and ans[-2] != ans[0]):
      return True
  if ans[0] == "S":
    if (s[0] == "o" and ans[-1] != ans[1]) or (s[0] == "x" and ans[-1] == ans[1]):
      return True
  if ans[0] == "W":
    if (s[0] == "o" and ans[-1] == ans[1]) or (s[0] == "x" and ans[-1] != ans[1]):
      return True
  print(ans)

N = int(input())
S = input()
if generate("SS",S,N):
  if generate("SW",S,N):
    if generate("WS",S,N):
      if generate("WW",S,N):
        print(-1)

Submission status: Accepted

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

def loop():
    for i in range(1, n-1):
        if ans[i] == 'S':
            if s[i] == 'x':
                if ans[i-1] == 'S':         
                    ans[i+1] = 'W'
                else:
                    ans[i+1] = 'S'
            else:
                if ans[i-1] == 'W':
                    ans[i+1] = 'W'
                else:
                    ans[i+1] = 'S'
        else:
            if s[i] == 'o':
                if ans[i-1] == 'S':
                    ans[i+1] = 'W'
                else:
                    ans[i+1] = 'S'
            else:
                if ans[i-1] == 'W':
                    ans[i+1] = 'W'
                else:
                    ans[i+1] = 'S'

ans = ['S'] * n
if s[0]  == 'x':
    ans[1] = 'W'
else:
    ans[1] = 'S'
loop()
if ans[n-1] == 'S':
    if (s[n-1] == 'o' and ans[n-2] == 'S') or (s[n-1] == 'x' and ans[n-2] == 'W'):
        print(''.join(ans))
        exit()

ans = ['W'] * (n-1) + ['S']
if s[0]  == 'x':
    ans[1] = 'S'
else:
    ans[1] = 'W'
loop()
if ans[n-1] == 'S':
    if (s[n-1] == 'x' and ans[n-2] == 'S') or (s[n-1] == 'o' and ans[n-2] == 'W'):
        print(''.join(ans))
        exit()

ans = ['S'] * (n-1) + ['W']
if s[0]  == 'x':
    ans[1] = 'S'
else:
    ans[1] = 'W'
loop()
if ans[n-1] == 'W':
    if (s[n-1] == 'x' and ans[n-2] == 'S') or (s[n-1] == 'o' and ans[n-2] == 'W'):
        print(''.join(ans))
        exit()

ans = ['W'] * n
if s[0]  == 'x':
    ans[1] = 'W'
else:
    ans[1] = 'S'
loop()
if ans[n-1] == 'W':
    if (s[n-1] == 'x' and ans[n-2] == 'W') or (s[n-1] == 'o' and ans[n-2] == 'S'):
        print(''.join(ans))
        exit()        

print(-1)

Submission status: Accepted

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

def loop():
    for i in range(1, n-1):
        if ans[i] == 'S':
            if s[i] == 'x':
                if ans[i-1] == 'S':         
                    ans[i+1] = 'W'
                else:
                    ans[i+1] = 'S'
            else:
                if ans[i-1] == 'W':
                    ans[i+1] = 'W'
                else:
                    ans[i+1] = 'S'
        else:
            if s[i] == 'o':
                if ans[i-1] == 'S':
                    ans[i+1] = 'W'
                else:
                    ans[i+1] = 'S'
            else:
                if ans[i-1] == 'W':
                    ans[i+1] = 'W'
                else:
                    ans[i+1] = 'S'

ans = ['S'] * n
if s[0]  == 'x':
    ans[1] = 'W'
else:
    ans[1] = 'S'
loop()
if ans[n-1] == 'S':
    if (s[n-1] == 'o' and ans[n-2] == 'S') or (s[n-1] == 'x' and ans[n-2] == 'W'):
        print(''.join(ans))
        exit()

ans = ['W'] * n
if s[0]  == 'x':
    ans[1] = 'W'
else:
    ans[1] = 'S'
loop()
if ans[n-1] == 'W':
    if (s[n-1] == 'x' and ans[n-2] == 'W') or (s[n-1] == 'o' and ans[n-2] == 'S'):
        print(''.join(ans))
        exit()

ans = ['W'] * (n-1) + ['S']
if s[0]  == 'x':
    ans[1] = 'S'
else:
    ans[1] = 'W'
loop()
if ans[n-1] == 'S':
    if (s[n-1] == 'x' and ans[n-2] == 'S') or (s[n-1] == 'o' and ans[n-2] == 'W'):
        print(''.join(ans))
        exit()

ans = ['S'] * (n-1) + ['W']
if s[0]  == 'x':
    ans[1] = 'S'
else:
    ans[1] = 'W'
loop()
if ans[n-1] == 'W':
    if (s[n-1] == 'x' and ans[n-2] == 'S') or (s[n-1] == 'o' and ans[n-2] == 'W'):
        print(''.join(ans))
        exit()

print(-1)

Submission status: Accepted

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

def loop():
    for i in range(1, n-1):
        if ans[i] == 'S':
            if s[i] == 'x':
                if ans[i-1] == 'S':         
                    ans[i+1] = 'W'
                else:
                    ans[i+1] = 'S'
            else:
                if ans[i-1] == 'W':
                    ans[i+1] = 'W'
                else:
                    ans[i+1] = 'S'
        else:
            if s[i] == 'o':
                if ans[i-1] == 'S':
                    ans[i+1] = 'W'
                else:
                    ans[i+1] = 'S'
            else:
                if ans[i-1] == 'W':
                    ans[i+1] = 'W'
                else:
                    ans[i+1] = 'S'

ans = ['S'] + ['Unknown'] * (n-2) + ['S']
if s[0]  == 'x':
    ans[1] = 'W'
else:
    ans[1] = 'S'
loop()
if ans[n-1] == 'S':
    if (s[n-1] == 'o' and ans[n-2] == 'S') or (s[n-1] == 'x' and ans[n-2] == 'W'):
        print(''.join(ans))
        exit()

ans = ['W'] + ['Unknown'] * (n-2) + ['S']
if s[0]  == 'x':
    ans[1] = 'S'
else:
    ans[1] = 'W'
loop()
if ans[n-1] == 'S':
    if (s[n-1] == 'x' and ans[n-2] == 'S') or (s[n-1] == 'o' and ans[n-2] == 'W'):
        print(''.join(ans))
        exit()

ans = ['W'] + ['Unknown'] * (n-2) + ['W']
if s[0]  == 'x':
    ans[1] = 'W'
else:
    ans[1] = 'S'
loop()
if ans[n-1] == 'W':
    if (s[n-1] == 'x' and ans[n-2] == 'W') or (s[n-1] == 'o' and ans[n-2] == 'S'):
        print(''.join(ans))
        exit()

ans = ['S'] + ['Unknown'] * (n-2) + ['W']
if s[0]  == 'x':
    ans[1] = 'S'
else:
    ans[1] = 'W'
loop()
if ans[n-1] == 'W':
    if (s[n-1] == 'x' and ans[n-2] == 'S') or (s[n-1] == 'o' and ans[n-2] == 'W'):
        print(''.join(ans))
        exit()

print(-1)

Submission status: Accepted

Solution code:
#import numpy as np
#from numpy import*
#from scipy.sparse.csgraph import shortest_path #shortest_path(csgraph=graph) # dijkstra# floyd_warshall
#from scipy.sparse import csr_matrix
 
from collections import* #defaultdict Counter deque appendleft
from fractions import gcd
from functools import* #reduce
from itertools import* #permutations("AB",repeat=2) combinations("AB",2) product("AB",2) groupby accumulate
from operator import mul,itemgetter
from bisect import* #bisect_left bisect_right
from heapq import* #heapify heappop heappushpop
from math import factorial,pi
from copy import deepcopy
import sys
sys.setrecursionlimit(10**8)
#input=sys.stdin.readline  #危険！基本オフにしろ！
 
def main():
    n=int(input())
    s=input()
    
    #print(s)
    doubutu1=["W","W"]
    doubutu2=["S","W"]
    doubutu3=["W","S"]
    doubutu4=["S","S"]
    def doubutuappender(doubutu):
        for i in range(1,n):
            if doubutu[i]=="S":
                doubutu.append( ["SW"["S"==doubutu[i-1]] ,doubutu[i-1]][s[i]=="o"])
            else:
                doubutu.append( [doubutu[i-1] ,"SW"["S"==doubutu[i-1]]][s[i]=="o"])
        return doubutu
    for i in [doubutuappender(doubutu1),doubutuappender(doubutu2),doubutuappender(doubutu3),doubutuappender(doubutu4)]:
        if i[0]==i[-1]:
            if s[0]=="o" and i[0]=="S" and i[1]==i[-2]:
                print("".join(i[:-1]))
                exit()
            elif s[0]=="x" and i[0]=="S" and i[1]!=i[-2]:
                print("".join(i[:-1]))
                exit()
            elif s[0]=="o" and i[0]=="W" and i[1]!=i[-2]:
                print("".join(i[:-1]))
                exit()
            elif s[0]=="x" and i[0]=="W" and i[1]==i[-2]:
                print("".join(i[:-1]))
                exit()
    print(-1)
if __name__ == '__main__':
    main()