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

def che(k):
    if l[k] == "o":
        if ans[k] == 1:
            if ans[k-1]*ans[(k+1)%n] == 1:
                return True
            else:
                return False
        else:
            if ans[k-1]*ans[(k+1)%n] == -1:
                return True
            else:
                return False

    else:
        if ans[k] == 1:
            if ans[k-1]*ans[(k+1)%n] == -1:
                return True
            else:
                return False
        else:
            if ans[k-1]*ans[(k+1)%n] == 1:
                return True
            else:
                return False


check = [[1,1],[1,-1],[-1,1],[-1,-1]]
for j,k in check:
    ans = [0]*n
    ans[0] = j
    ans[1] = k
    
    for i in range(1,n):
        if ans[i] == 1:
            if l[i] == "o":
                ans[(i+1)%n] = ans[i-1]
            else:
                ans[(i+1)%n] = -ans[i-1]

        else:
            if l[i] == "o":
                ans[(i+1)%n] = -ans[i-1]
            else:
                ans[(i+1)%n] = ans[i-1]
    d = True
    
    for m in range(n):
        if che(m) == False:
            d = False
            
    if d:
        for i in ans:
            if i == 1:
                print("S",end="")
            else:
                print("W",end="")
        exit()  
            
print(-1)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n=II()
    s=SI()
    ans=[-1]*n
    for f in range(2):
        for e in range(2):
            ans[0]=f
            ans[-1]=e
            for i,c in enumerate(s):
                if c=="o":
                    if ans[i]:ans[(i+1)%n]=ans[i-1]
                    else:ans[(i+1)%n]=1-ans[i-1]
                else:
                    if ans[i]:ans[(i+1)%n]=1-ans[i-1]
                    else:ans[(i+1)%n]=ans[i-1]
            if ans[0]==f and ans[-1]==e:
                print("".join("S" if a else "W" for a in ans))
                exit()
    print(-1)

main()

Submission status: Accepted

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from functools import *
from itertools import permutations,combinations,groupby
import sys
import bisect
import string
import math
import time
import random
def Golf():
    *a,=map(int,open(0))
def S_():
    return input()
def IS():
    return input().split()
def LS():
    return [i for i in input().split()]
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def NI(n):
    return [int(input()) for i in range(n)]
def NI_(n):
    return [int(input())-1 for i in range(n)]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def LtoS(ls):
    return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[1]*(w+2)
    found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[1]+[mp_def[j] for j in s]+[1]
    mp+=[1]*(w+2)
    return h+2,w+2,mp,found
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['YES','NO']

mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()
 
def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

ans=0

n=I()
s=[1 if i=='o' else 0 for i in input()]

# 1 true, 0 lie
def check(y):
    t=[0]*n
    t[0],t[1]=y
    p=t[0]
    for i in range(2,n):
        t[i]=t[i-1]^t[i-2]^s[i-1]
    for i in range(n):
        if s[i]!=t[i-1]^t[i]^t[(i+1)%n]:
            return -1
    return t

for i in range(2):
    for j in range(2):
        ans=check((i,j))
        if ans!=-1:
            ans=''.join(['S' if i==1 else 'W' for i in ans])
            print(ans)
            exit()

print(ans)

Submission status: Accepted

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from functools import *
from itertools import permutations,combinations,groupby
import sys
import bisect
import string
import math
import time
import random
def Golf():
    *a,=map(int,open(0))
def S_():
    return input()
def IS():
    return input().split()
def LS():
    return [i for i in input().split()]
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def NI(n):
    return [int(input()) for i in range(n)]
def NI_(n):
    return [int(input())-1 for i in range(n)]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def LtoS(ls):
    return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[1]*(w+2)
    found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[1]+[mp_def[j] for j in s]+[1]
    mp+=[1]*(w+2)
    return h+2,w+2,mp,found
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['YES','NO']

mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()
 
def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

ans=0

n=I()
s=[1 if i=='o' else 0 for i in input()]

def f(a,b,c):
    if a==1: # sheep
        if c==1: # o
            r=b
        else:
            r=1-b
    if a==0: # wolf
        if c==1: # o
            r=1-b
        else:
            r=b
    return r

def say(x,y,z):
    if y==1: # sheep
        if x==z: # o
            r=1
        else:
            r=0
    if y==0: # wolf
        if x==z: # o
            r=0
        else:
            r=1
    return r


# 1 true, 0 lie
def check(y):
    t=[0]*n
    t[0],t[1]=y
    for i in range(2,n):
        t[i]=f(t[i-1],t[i-2],s[i-1])
    for i in range(n):
        if s[i]!=say(t[i-1],t[i],t[(i+1)%n]):
            return -1
    return t

for i in range(2):
    for j in range(2):
        ans=check((i,1-j))
        if ans!=-1:
            ans=''.join(['S' if x==1 else 'W' for x in ans])
            print(ans)
            exit()

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(map(str, input()))
flg = 0

def f(a, b, c):
	if a == "S" and b == "S" and c == "o":
		return "S"
	elif a == "S" and b == "S" and c == "x":
		return "W"
	elif a == "S" and b == "W" and c == "o":
		return "W"
	elif a == "S" and b == "W" and c == "x":
		return "S"
	elif a == "W" and b == "S" and c == "o":
		return "W"
	elif a == "W" and b == "S" and c == "x":
		return "S"
	elif a == "W" and b == "W" and c == "o":
		return "S"
	else:
		return "W"

for first, second in [("S", "S"), ("S", "W"), ("W", "S"), ("W", "W"), ]:
	ans = [None] * N
	ans[0], ans[1] = first, second
	
	for i in range(1, N - 1):
		ans[i + 1] = f(ans[i - 1], ans[i], S[i])
		
	if f(ans[-2], ans[-1], S[-1]) == ans[0] and f(ans[-1], ans[0], S[0]) == ans[1]:
		flg = 1
		break
		
if flg:
	for a in ans:
		print(a, end = "")
else:
	print(-1)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
start_pattern = ['SS', 'SW', 'WS', 'WW']
s2 = s+s[0]
for start in start_pattern:
    ans = start
    before = s[0]

    for i, now in enumerate(s2[1:]):
        if i == N-1:
            break
        before_state = ans[i]
        now_state = ans[i+1]
        if now_state == 'S':
            yes = 'o'
            no = 'x'
        else:
            yes = 'x'
            no = 'o'

        if now == yes:
            ans += before_state
        else:
            if before_state == 'S':
                ans += 'W'
            else:
                ans += 'S'
    if ans[0] == ans[-1]:
        ok = False
        if ans[0] == 'S':
            yes = 'o'
        else:
            yes = 'x'
        if s[0] == yes:
            if ans[1] == ans[-2]:
                ok = True
        else:
            if ans[1] != ans[-2]:
                ok = True
        if ok:
            print(ans[:-1])
            exit()
print('-1')

Submission status: Accepted

Solution code:
n=int(input())
s=input()
l=s[n-1]+s+s[0]
assign=['S','W']
for i in assign:
  for j in assign:
    t=i+j
    for k in range(2,n):
      if s[k-1]=='o':
        if t[-1]=='S':
          t+=t[-2]
        else:
          if t[-2]=='S':
            t+='W'
          else:
            t+='S'
      else:
        if t[-1]=='S':
          if t[-2]=='S':
            t+='W'
          else:
            t+='S'
        else:
          t+=t[-2]
    t=t[n-1]+t+t[0]
    ok=True
    for u in range(1,n+1):
      if l[u]=='o':
        if t[u]=='S':
          if t[u-1]!=t[u+1]:
            ok=False
        else:
          if t[u-1]==t[u+1]:
            ok=False
      else:
        if t[u]=='S':
          if t[u-1]==t[u+1]:
            ok=False
        else:
          if t[u-1]!=t[u+1]:
            ok=False
    if ok:
      print(t[1:n+1])
      exit()
print(-1)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
S = S[1:]+S[0]
animal = ['WW', 'WS', 'SW', 'SS']

for pair in animal:
    a,b = pair[0],pair[1]
    pre, nex = a, b
    answer = [a,b]
    for i in range(N):
        if (nex == 'S' and S[i] == 'o') or (nex=='W'and S[i] == 'x'):
            tmp = nex
            nex = pre
            pre = tmp
            answer.append(nex)
        else:
            tmp = nex
            nex = 'W'if pre == 'S' else 'S'
            pre = tmp
            answer.append(nex)
    if nex == b and pre == a:
        print(''.join(answer[:-2]))
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
def check():
    N = int(input())
    S = input()
    S = S[1:]+S[0]
    animal = ['W', 'S']
    
    for a in animal:
        for b in animal:
            #print(a,b)
            pre, nex = a, b
            answer = [a,b]
            for i in range(N):
                if (nex == 'S' and S[i] == 'o') or (nex=='W'and S[i] == 'x'):
                    tmp = nex
                    nex = pre
                    pre = tmp
                    answer.append(nex)
                else:
                    tmp = nex
                    nex = 'W'if pre == 'S' else 'S'
                    pre = tmp
                    answer.append(nex)
            if nex == b and pre == a:
                return answer[:-2]
    else:
        return -1

ans = check()               
if type(ans) == int:
    print(ans)
else:
    print(''.join(ans))

Submission status: Accepted

Solution code:
def check():
    N = int(input())
    S = input()
    S = S[1:]+S[0]
    animal = ['W', 'S']
    
    for a in animal:
        for b in animal:
            #print(a,b)
            pre, nex = a, b
            answer = [a,b]
            for i in range(N):
                if nex == 'S':
                    if S[i] == 'o':
                        tmp = nex
                        nex = pre
                        pre = tmp
                        answer.append(nex)
                    elif S[i] == 'x':
                        tmp = nex
                        nex = 'W' if pre == 'S' else 'S'
                        pre = tmp
                        answer.append(nex)
                else:
                    if S[i] == 'o':
                        tmp = nex
                        nex = 'W'if pre == 'S' else 'S'
                        pre = tmp
                        answer.append(nex)
                    elif S[i] == 'x':
                        tmp = nex
                        nex = pre
                        pre = tmp
                        answer.append(nex)
                #print(nex)
            if nex == b and pre == a:
                return answer[:-2]
    else:
        return -1

ans = check()               
if type(ans) == int:
    print(ans)
else:
    print(''.join(ans))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
f = lambda x,y,z: "WS"[(x=="S")^(y=="S")^(z=="o")]
for T in ["SS", "SW", "WS", "WW"]:
    for s in S:
        T += f(T[-1], T[-2], s)
    if T[:2] == T[-2:]:
        print(T[1:-1])
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
pattern = [('S','S'), ('S','W'), ('W', 'S'), ('W','W')]
N = int(input())
S = input()

ret = '-1'

def check(s, animal, neighbor1, neighbor2):
    if animal == 'W': s = 'x' if s == 'o' else 'o'
    return (s == 'o' and neighbor1 == neighbor2) or (s == 'x' and neighbor1 != neighbor2)

for p in pattern:
    ret_temp = p[0]+p[1]
    first, second = p[0], p[1]
    for i in range(1, N-1):
        equalty = 0 if S[i] == 'o' else 1
        if second == 'W':
            equalty = equalty^1
        if equalty == 0:
            next_animal = first
        else:
            next_animal = 'W' if first == 'S' else 'S'
        ret_temp += next_animal

        first = second
        second = next_animal
        if not check(S[i], ret_temp[i], ret_temp[i-1], ret_temp[i+1]): break
            
    if check(S[0], ret_temp[0], ret_temp[1], ret_temp[-1]) and check(S[N-1], ret_temp[N-1], ret_temp[N-2], ret_temp[0]):
        # print('ok:', ret_temp)
        ret = ret_temp
        break
    # else: print('ng:', ret_temp)

print(ret)

Submission status: Accepted

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

    # print(N, S)

    return N, S


def solve(N, S):
    def inv_animal(animal):
        return "S" if animal == "W" else "W"

    def is_correct(st):
        for i in range(0, N):
            pre, nxt = i - 1, i + 1
            if i == 0:
                pre = -1
            elif i == N - 1:
                nxt = 0

            say = S[i]
            animal = st[i]

            if animal == "S":
                if say == "o":
                    if not st[pre] == st[nxt]:
                        return False
                else:
                    if st[pre] == st[nxt]:
                        return False
            else:
                if say == "o":
                    if st[pre] == st[nxt]:
                        return False
                else:
                    if not st[pre] == st[nxt]:
                        return False
        return True

    # print(S)

    lis = []
    def recur(i, st):
            if not i < 2:
                lis.append(st)
                return

            recur(i + 1, st + "S")
            recur(i + 1, st + "W")
    recur(0, "")

    # print(lis)

    if len(lis) == 0:
        return -1

    def fill(p, n):
        for i in range(1, n):
            pre = i - 1
            say = S[i]
            animal = p[i]

            if animal == "S":
                if say == "o":
                    p += p[pre]
                else:
                    p += inv_animal(p[pre])
            else:
                if say == "o":
                    p += inv_animal(p[pre])
                else:
                    p += p[pre]
        return p

    for p in lis:
        filled = fill(p, N - 1)
        if is_correct(filled):
            return filled
    return -1


if __name__ == '__main__':
    res = solve(*args())
    print(res)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, atan, degrees
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect
import numpy as np

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
s = input()

search = [
["S"]+["_"]*(N-1), 
["S"]+["_"]*(N-1), 
["W"]+["_"]*(N-1),
["W"]+["_"]*(N-1)
]

if s[0] == "o":
	search[0][1] = "S"
	search[0][-1] = "S"
	search[1][1] = "W"
	search[1][-1] = "W"
	search[2][1] = "W"
	search[2][-1] = "S"
	search[3][1] = "S"
	search[3][-1] = "W"
else:
	search[0][1] = "S"
	search[0][-1] = "W"
	search[1][1] = "W"
	search[1][-1] = "S"
	search[2][1] = "W"
	search[2][-1] = "W"
	search[3][1] = "S"
	search[3][1] = "S"

for lis in search:
	for i in range(N):
		# print(i, lis)
		if s[i] == "o" and lis[i] == "S":
			if lis[(i+1)%N] != "_" and lis[i-1] != lis[(i+1)%N]:
				break
			if lis[(i+1)%N] == "_":
				lis[i+1] = lis[i-1]
		elif s[i] == "x" and lis[i] == "S":
			if lis[(i+1)%N] != "_" and lis[i-1] == lis[(i+1)%N]:
				break
			if lis[(i+1)%N] == "_":
				if lis[i-1] == "W":
					lis[i+1] = "S"
				else:
					lis[i+1] = "W"

		elif s[i] == "o" and lis[i] == "W":
			if lis[(i+1)%N] != "_" and lis[i-1] == lis[(i+1)%N]:
				break
			if lis[(i+1)%N] == "_":
				if lis[i-1] == "W":
					lis[i+1] = "S"
				else:
					lis[i+1] = "W"
			
		elif s[i] == "x" and lis[i] == "W":
			if lis[(i+1)%N] != "_" and lis[i-1] != lis[(i+1)%N]:
				break
			if lis[(i+1)%N] == "_":
				lis[i+1] = lis[i-1]

	else:
		print(*lis, sep="")
		break
else:
	print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

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

def main():
    N = int(input())
    S = str(input())
    
    def animal(x,s):
        if x[-1] == 1:
            if s == "o":
                return x[-2]
            else:
                return -1*x[-2]
        else:
            if s == "o":
                return -1*x[-2]
            else:
                return x[-2]
                
    poss = [[1,1],[1,-1],[-1,1],[-1,-1]]
    for i in range(4):
        col = poss[i]
        for j in range(N-1):
            put = animal(col,S[j+1])
            col.append(put)
        put = animal(col,S[0])
        col.append(put)
        if (col[0] == col[-2] and col[1] == col[-1]):
            l = len(col)-2
            ans = []
            for k in range(l):
                if col[k] == 1:
                    ans.append("S")
                else:
                    ans.append("W")
            print("".join(ans))
            exit()
            
    print(-1)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
def f(n):
    if n == 'S':
        return 'W'
    if n == 'W':
        return 'S'
A = ['S','S']
fa = 1
B = ['S','W']
fb = 1
C = ['W','W']
fc = 1
D = ['W','S']
fd = 1
for i in range(1,N-1):
    x = S[i]
    if x == 'o':
        if A[i] == 'S':
            A.append(A[i-1])
        else:
            A.append(f(A[i-1]))
        if B[i] == 'S':
            B.append(B[i-1])
        else:
            B.append(f(B[i-1]))
        if C[i] == 'S':
            C.append(C[i-1])
        else:
            C.append(f(C[i-1]))
        if D[i] == 'S':
            D.append(D[i-1])
        else:
            D.append(f(D[i-1]))
    else:
        if A[i] == 'S':
            A.append(f(A[i-1]))
        else:
            A.append(A[i-1])
        if B[i] == 'S':
            B.append(f(B[i-1]))
        else:
            B.append(B[i-1])
        if C[i] == 'S':
            C.append(f(C[i-1]))
        else:
            C.append(C[i-1])
        if D[i] == 'S':
            D.append(f(D[i-1]))
        else:
            D.append(D[i-1])
if S[0] == 'o':
    if A[0] == 'S':
        if A[1] != A[-1]:
            fa = 0
    else:
        if A[1] == A[-1]:
            fa = 0
else:
    if A[0] == 'S':
        if A[1] == A[-1]:
            fa = 0
    else:
        if A[1] != A[-1]:
            fa = 0
if S[-1] == 'o':
    if A[-1] == 'S':
        if A[-2] != A[0]:
            fa = 0
    else:
        if A[-2] == A[0]:
            fa = 0
else:
    if A[-1] == 'S':
        if A[-2] == A[0]:
            fa = 0
    else:
        if A[-2] != A[0]:
            fa = 0
if fa == 1:
    x = A[0]
    for i in range(1,N):
        x += A[i]
    print(x)
    quit()
if S[0] == 'o':
    if B[0] == 'S':
        if B[1] != B[-1]:
            fb = 0
    else:
        if B[1] == B[-1]:
            fb = 0
else:
    if B[0] == 'S':
        if B[1] == B[-1]:
            fb = 0
    else:
        if B[1] != B[-1]:
            fb = 0
if S[-1] == 'o':
    if B[-1] == 'S':
        if B[-2] != B[0]:
            fb = 0
    else:
        if B[-2] == B[0]:
            fb = 0
else:
    if B[-1] == 'S':
        if B[-2] == B[0]:
            fb = 0
    else:
        if B[-2] != B[0]:
            fb = 0
if fb == 1:
    x = B[0]
    for i in range(1,N):
        x += B[i]
    print(x)
    quit()
if S[0] == 'o':
    if C[0] == 'S':
        if C[1] != C[-1]:
            fc = 0
    else:
        if C[1] == C[-1]:
            fc = 0
else:
    if C[0] == 'S':
        if C[1] == C[-1]:
            fc = 0
    else:
        if C[1] != C[-1]:
            fc = 0
if S[-1] == 'o':
    if C[-1] == 'S':
        if C[-2] != C[0]:
            fc = 0
    else:
        if C[-2] == C[0]:
            fc = 0
else:
    if C[-1] == 'S':
        if C[-2] == C[0]:
            fc = 0
    else:
        if C[-2] != C[0]:
            fc = 0
if fc == 1:
    x = C[0]
    for i in range(1,N):
        x += C[i]
    print(x)
    quit()
if S[0] == 'o':
    if D[0] == 'S':
        if D[1] != D[-1]:
            fd = 0
    else:
        if D[1] == D[-1]:
            fd = 0
else:
    if D[0] == 'S':
        if D[1] == D[-1]:
            fd = 0
    else:
        if D[1] != D[-1]:
            fd = 0
if S[-1] == 'o':
    if D[-1] == 'S':
        if D[-2] != D[0]:
            fd = 0
    else:
        if D[-2] == D[0]:
            fd = 0
else:
    if D[-1] == 'S':
        if D[-2] == D[0]:
            fd = 0
    else:
        if D[-2] != D[0]:
            fd = 0
if fd == 1:
    x = D[0]
    for i in range(1,N):
        x += D[i]
    print(x)
    quit()
if fa + fb + fc + fd == 0:
    print(-1)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
s=input()
def solve():
    for i in range(1,n):
        if (ans[i]=="S" and s[i]=="o") or (ans[i]=="W" and s[i]=="x"):
            ans[i+1]=ans[i-1]
        else:
            if ans[i-1]=="S":
                ans[i+1]="W"
            else:
                ans[i+1]="S"

        if (ans[0]=="S" and s[0]=="o") or (ans[0]=="W" and s[0]=="x"):
            sl=ans[1]
        else:
            if ans[1]=="S":
                sl="W"
            else:
                sl="S"
    if ans[0]==ans[-1] and sl==ans[n-1]:
        return True
    else:
        return False

ans=[0]*(n+1)

for i in ["S","W"]:
    for j in ["S","W"]:
        ans[0]=i
        ans[1]=j
        if solve():
            print("".join(ans[:-1]))
            exit()
print(-1)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
x=["SS","SW","WS","WW"]

def check(i,j):
    if x[i][j]=="S":
        if s[j]=="o":
            if x[i][j-1]=="S":
                return x[i][j+1]=="S"
            else:
                return x[i][j+1]=="W"
        else:
            if x[i][j-1]=="S":
                return x[i][j+1]=="W"
            else:
                return x[i][j+1]=="S"
    else:
        if s[j]=="o":
            if x[i][j-1]=="S":
                return x[i][j+1]=="W"
            else:
                return x[i][j+1]=="S"
        else:
            if x[i][j-1]=="S":
                return x[i][j+1]=="S"
            else:
                return x[i][j+1]=="W"

a=["S","W"]
for i in range(4):
    for j in range(n-2):
        if x[i][j+1]=="S":
            if s[j+1]=="o":
                x[i]+=a[x[i][j]!="S"]
            else:
                x[i]+=a[x[i][j]=="S"]
        else:
            if s[j+1]=="o":
                x[i]+=a[x[i][j]=="S"]
            else:
                x[i]+=a[x[i][j]!="S"]
    if check(i,0) and check(i,-1):
        print(x[i])
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
x=["SS","SW","WS","WW"]

def check0(i):
    if x[i][0]=="S":
        if s[0]=="o":
            if x[i][-1]=="S":
                return x[i][1]=="S"
            else:
                return x[i][1]=="W"
        else:
            if x[i][-1]=="S":
                return x[i][1]=="W"
            else:
                return x[i][1]=="S"
    else:
        if s[0]=="o":
            if x[i][-1]=="S":
                return x[i][1]=="W"
            else:
                return x[i][1]=="S"
        else:
            if x[i][-1]=="S":
                return x[i][1]=="S"
            else:
                return x[i][1]=="W"
def check1(i):
    if x[i][n-1]=="S":
        if s[n-1]=="o":
            if x[i][n-2]=="S":
                return x[i][0]=="S"
            else:
                return x[i][0]=="W"
        else:
            if x[i][n-2]=="S":
                return x[i][0]=="W"
            else:
                return x[i][0]=="S"
    else:
        if s[n-1]=="o":
            if x[i][n-2]=="S":
                return x[i][0]=="W"
            else:
                return x[i][0]=="S"
        else:
            if x[i][n-2]=="S":
                return x[i][0]=="S"
            else:
                return x[i][0]=="W"

a=["S","W"]
for i in range(4):
    for j in range(n-2):
        if x[i][j+1]=="S":
            if s[j+1]=="o":
                x[i]+=a[x[i][j]!="S"]
            else:
                x[i]+=a[x[i][j]=="S"]
        else:
            if s[j+1]=="o":
                x[i]+=a[x[i][j]=="S"]
            else:
                x[i]+=a[x[i][j]!="S"]
    if check0(i) and check1(i):
        print(x[i])
        break
else:
    print(-1)