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:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N = I()
    S=ST()
    ans = [0]*N #True -> W
    for mask in range(4):
        ans[0] = bool(mask&1)
        ans[1] = bool(mask&2)
        for i in range(2,N):
            if (S[i-1]=="o") ^ ans[i-1]:
                ans[i] = ans[i-2]
            else:
                ans[i] = ans[i-2] ^ True
        ok = True
        for i in range(N):
            if ((S[i] == "o") ^ ans[i]) ^ (ans[(i+1)%N] == ans[i-1]):
                ok = False
        if ok:
            print(*["W" if a else "S" for a in ans ],sep="")
            return 
    print(-1)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

def judge(a1,an):
    A=[-1]*N
    A[0]=a1
    A[N-1]=an
    A[1]=A[N-1]^A[0]^(1 if s[0]=='o' else 0)
    for i in range(1,N-2):
        A[i+1]=A[i-1]^A[i]^(1 if s[i]=='o' else 0)
    jn=A[N-3]^A[N-2]^(1 if s[N-2]=='o' else 0)
    j1=A[N-2]^jn^(1 if s[N-1]=='o' else 0)
    if j1==a1 and jn==an: return A
    return None

for a1 in range(2):
    for an in range(2):
        r = judge(a1,an)
        if r!=None:
            print(''.join([('S' if a==1 else 'W') for a in r]))
            exit()
print(-1)

Submission status: Accepted

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

for a,b in ['SS','SW','WS','WW']:
    tmp = a+b
    for c in S:
        if (c=='x') ^ (tmp[-1]=='W'):
            tmp += ('W' if tmp[-2]=='S' else 'S')
        else:
            tmp += tmp[-2]
    #print(tmp)
    if tmp[:2] == tmp[-2:]:
        print(tmp[1:-1])
        exit()
print(-1)

Submission status: Accepted

Solution code:

import numpy as np
import sys
sys.setrecursionlimit(100000)


def acinput():
    return list(map(int, input().split(" ")))


directions = np.array([[1, 0], [0, 1], [-1, 0], [0, -1]])
directions = list(map(np.array, directions))

mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact


def serch(x, count):
    #print("top", x, count)

    for d in directions:
        nx = d+x
        # print(nx)
        if np.all(0 <= nx) and np.all(nx < (H, W)):
            if field[nx[0]][nx[1]] == "E":
                count += 1
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)
                continue
            if field[nx[0]][nx[1]] == "#":
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)

    return count


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


def chk(init_tf):
    tf = [-1]*(N)

    tf[0:2] = init_tf
    for i in range(2, N):
        if tf[i-1] == 1:
            if s[i-1] == "o":
                # d[i]=d[i-2]
                tf[i] = tf[i-2]
            else:
                tf[i] = (tf[i-2]+1) % 2

        if tf[i-1] == 0:
            if s[i-1] == "o":
                tf[i] = (tf[i-2]+1) % 2
            else:
                tf[i] = tf[i-2]
    """ 
    print(tf)
    print(s)
     """

    for i in range(N):
        
        r=(i+1)%(N)
     #   print(i,r)
        if tf[i] == 1:
            if s[i]=="o":
                if tf[i-1] !=tf[r]:
                    return False       
            else:
                if tf[i-1] == tf[r]:
                    return False

        elif tf[i] == 0:
            if s[i] == "x":
                if tf[i-1] != tf[r]:
                    return False
            else:
                if tf[i-1] == tf[r]:
                    return False

    return ["S" if x == 1 else "W" for x in tf]


for i in range(2):
    for j in range(2):
        res=chk((i,j))
        if res:
            print("".join(res))
            sys.exit()

print(-1)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
ans=[[0]*n for i in range(4)]
ans[1][1]=1
ans[2][0]=1
ans[3][0]=ans[3][1]=1
idx=-1
for i in range(4):
  for j in range(1,n-1):
    if s[j]=='o' and ans[i][j]==1 and ans[i][j-1]==1:
      ans[i][j+1]=1
    elif s[j]=='o' and ans[i][j]==1 and ans[i][j-1]==0:
      ans[i][j+1]=0
    elif s[j]=='o' and ans[i][j]==0 and ans[i][j-1]==1:
      ans[i][j+1]=0
    elif s[j]=='o' and ans[i][j]==0 and ans[i][j-1]==0:
      ans[i][j+1]=1
    elif s[j]=='x' and ans[i][j]==1 and ans[i][j-1]==1:
      ans[i][j+1]=0
    elif s[j]=='x' and ans[i][j]==1 and ans[i][j-1]==0:
      ans[i][j+1]=1
    elif s[j]=='x' and ans[i][j]==0 and ans[i][j-1]==1:
      ans[i][j+1]=1
    else:
      ans[i][j+1]=0
  flag1=False
  flag2=False
  if ans[i][n-1]==1 and ans[i][0]==ans[i][n-2] and s[n-1]=='o':
    flag1=True
  elif ans[i][n-1]==1 and ans[i][0]!=ans[i][n-2] and s[n-1]=='x':
    flag1=True
  elif ans[i][n-1]==0 and ans[i][0]==ans[i][n-2] and s[n-1]=='x':
    flag1=True
  elif ans[i][n-1]==0 and ans[i][0]!=ans[i][n-2] and s[n-1]=='o':
    flag1=True
  if ans[i][0]==1 and ans[i][n-1]==ans[i][1] and s[0]=='o':
    flag2=True
  elif ans[i][0]==0 and ans[i][n-1]==ans[i][1] and s[0]=='x':
    flag2=True
  elif ans[i][0]==1 and ans[i][n-1]!=ans[i][1] and s[0]=='x':
    flag2=True
  elif ans[i][0]==0 and ans[i][n-1]!=ans[i][1] and s[0]=='o':
    flag2=True
  if flag1 and flag2:
    idx=i
    break
if idx==-1:
  print(-1)
  exit()
t=[]
for i in range(n):
  if ans[idx][i]==1:
    t.append('S')
  else:
    t.append('W')
print(''.join(t))

Submission status: Accepted

Solution code:
N = int(input())
S = input().strip()
#print(S)
for b, c in ["SS", "SW", "WS", "WW"]:
#for b, c in ["SW"]:
    fb, fc = b, c
    rs = [""]*N
    for i in range(N):
        cc = c
        if S[i] == 'o':
            if c == "S":
                c = "S" if b == "S" else "W"
            else:
                c = "W" if b == "S" else "S"
        else:
            if c == "S":
                c = "W" if b == "S" else "S"
            else:
                c = "S" if b == "S" else "W"
        b = cc
        if i != N-1:
            rs[i+1] = c
        else:
            rs[0] = c
    if rs[0] == fc and rs[-1] == fb:
        print("".join(rs))
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys
import itertools
input = sys.stdin.readline


def read_values():
    return map(int, input().split())


def read_list():
    return list(read_values())


def read_lists(N):
    return [read_list() for n in range(N)]


def f(S, init):
    F = [init[0], init[1]]
    for i, s in enumerate(S):
        if i == 0:
            continue
        d = 1 if s == "o" else -1
        F.append(F[i - 1] * F[i] * d)
    
    if F[0] != F[-1]:
        return None
    
    d = 1 if S[0] == "o" else -1
    d *= F[0]
    N = len(S)
    if F[-2] * F[1] == d:
        # print(F)
        return F[:N]
    else:
        return None


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

    for init in [(1, 1), (-1, 1), (1, -1), (-1, -1)]:
        F = f(S, init)
        if F is not None:
            F = map(lambda v: "S" if v == 1 else "W", F)
            print("".join(F))
            return 
    
    print(-1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import *
from itertools import *
import copy
from heapq import *

N=int(input())
S=input()
def check(data):
    for i in range(N):
        if data[i]=="S" and S[i]=="o" or data[i]=="W" and S[i]=="x":
            if data[i-1]==data[(i+1) if i+1<N else (i+1-N)]:
                pass
            else:
                return(0)
        else:
            if data[i-1]!=data[(i+1) if i+1<N else (i+1-N)]:
                pass
            else:
                return(0)
    return(1)


for s1 in ["S","W"]:
    for s2 in ["S","W"]:
        data=[0 for i in range(N)]
        data[0],data[1]=s1,s2

        for i in range(2,N):
            if data[i-1]=="S" and S[i-1]=="o" or data[i-1]=="W" and S[i-1]=="x":
                data[i]=data[i-2]
            else:
                data[i]="S" if data[i-2]=="W" else "W"

        if check(data):
            print("".join(list(map(str,data))))
            exit()

print(-1)

Submission status: Accepted

Solution code:
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def right(aim,ai,si):
    if ai==0:
        return aim if si=='o' else (1-aim)
    else:
        return (1-aim) if si=='o' else aim

n = inn()
s = ins()
for a0 in [0,1]:
    for a1 in [0,1]:
        a = [-1]*n
        a[0] = a0
        a[1] = a1
        for i in range(1,n-1):
            a[i+1] = right(a[i-1],a[i],s[i])
        if right(a[n-2],a[n-1],s[n-1])==a[0] and \
           right(a[n-1],a[0],s[0])==a[1] :
           for j in range(n):
               printn('S' if a[j]==0 else 'W')
           print('')
           exit()
print(-1)

Submission status: Accepted

Solution code:
def examC():
    N, M = LI()
    if N-M//2>=0:
        ans = M//2
    else:
        ans = N + (M-N*2)//4
    print(ans)
    return

def examD():
    N = I()
    S = SI()
    for i1,i2 in [[0,0],[0,1],[1,0],[1,1]]:
        flag = True
        sheep = [1]*N
        sheep[0] = i1
        sheep[1] = i2
        for j in range(1,N-1):
            if S[j]=="o":
                cur = 1
            else:
                cur = 0
            sheep[j+1] = (sheep[j]^cur)^sheep[j-1]
        if S[-1] == "o":
            cur = 1
        else:
            cur = 0
        if sheep[0]!=(sheep[-1] ^ cur) ^ sheep[-2]:
            flag = False
        if S[0] == "o":
            cur = 1
        else:
            cur = 0
        if sheep[1]!=(sheep[0] ^ cur) ^ sheep[-1]:
            flag = False
        if flag:
            ans = ""
            for i in sheep:
                if i==1:
                    ans += "S"
                else:
                    ans += "W"
            print(ans)
            return
    print(-1)
    return

def examE():
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examD()

"""

"""

Submission status: Accepted

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

# sheep:1, wolf:0
isOK = False
for x, y in [(1, 1), (1, 0), (0, 1), (0, 0)]:
    A = [-1] * len(S)
    A[0] = x
    A[1] = y
    for i in range(1, len(S)-1):
        if A[i]:  # 羊の場合
            A[i + 1] = A[i - 1] if S[i] == "o" else 1 - A[i - 1]
        else:  # 狼の場合
            A[i + 1] = A[i - 1] if S[i] == "x" else 1 - A[i - 1]

    if A[0] == A[-2] and A[1] == A[-1]:
        isOK = True
        break

if isOK:
    ans = ["S" if i else "W" for i in A[1:-1]]
    ans = "".join(ans)
else:
    ans = -1

print(ans)

Submission status: Accepted

Solution code:
def f(x,y,z):
    if x=="S" and y=="o":
        return z
    if x=="S" and y=="x":
        return g(z)
    if x=="W" and y=="o":
        return g(z)
    if x=="W" and y=="x":
        return z
def g(x):
    if x=="S":
        return "W"
    if x=="W":
        return "S"
def check(x,y):
    t[0]=x
    t[1]=y
    for j in range(1,N-1):
        t[j+1] = f(t[j],s[j],t[j-1])
    x1 = f(t[N-1],s[N-1],t[N-2])
    y1 = f(x1,s[0],t[N-1])
    if x==x1 and y==y1:
        return True
    else:
        return False
N = int(input())
s = input().strip()
t = [0 for _ in range(N)]
if check("S","S"):
    print("".join(t))
elif check("S","W"):
    print("".join(t))
elif check("W","S"):
    print("".join(t))
elif check("W","W"):
    print("".join(t))
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
s = [0 if i=="o" else 1 for i in input()]
for i in range(4):
  dp = [0]*n
  dp[0], dp[1] = divmod(i, 2)
  for i in range(2, n):
    dp[i] = dp[i-2]^dp[i-1]^s[i-1]
  if dp[-2]^dp[-1]^s[-1] == dp[0] and dp[-1]^dp[0]^s[0] == dp[1]:
    A = ["S" if i==0 else "W" for i in dp]
    print(*A, sep="")
    break
else:
  print(-1)

Submission status: Accepted

Solution code:
#お手上げ、挙動追えない
import sys
n = int(input())
s = input()

L = ["SS","SW","WS","WW"]
l= len(s)
##0,1に置き換えたりしない
##条件分岐注意する
##ずらす必要はない?Lが固定するのはindexで-1,0?
for t in L:
    tmp = t###
    for i in range(l):
        if (s[i]=="x") ^ (tmp[-1] == "W"):
            if tmp[-2]=="S":
                tmp+= "W"
            else:
                tmp += "S"
            
        else:
            tmp += tmp[-2]
    if tmp[:2]==tmp[-2:]:
        print(tmp[1:-1])###
        sys.exit()
print(-1)

Submission status: Accepted

Solution code:
def check(b0, b1):
    is_sheep[0] = b0
    is_sheep[1] = b1
    
    for i in range(1, N):
        if s[i]=='o':
            if is_sheep[i]:
                is_sheep[(i+1)%N] = is_sheep[i-1]
            else:
                is_sheep[(i+1)%N] = not is_sheep[i-1]
        else:
            if is_sheep[i]:
                is_sheep[(i+1)%N] = not is_sheep[i-1]
            else:
                is_sheep[(i+1)%N] = is_sheep[i-1]
    
    if b0==is_sheep[0]:
        if s[0]=='o':
            if b0:
                return is_sheep[N-1]==is_sheep[1]
            else:
                return is_sheep[N-1]!=is_sheep[1]
        else:
            if b0:
                return is_sheep[N-1]!=is_sheep[1]
            else:
                return is_sheep[N-1]==is_sheep[1]
    else:
        return False

N = int(input())
s = input()
is_sheep = [False]*N

for b0, b1 in [(False, False), (False, True), (True, False), (True, True)]:
    if check(b0, b1):
        ans = ['S' if is_sheep[i] else 'W' for i in range(N)]
        print(''.join(ans))
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
st = [["S","S"],["S","W"],["W","W"],["W","S"]]
key = "F"
for i in st:
    ans = i
    a0,a1 = i
    for j in range(1,n):
        #print(a0,a1,s[j])
        if s[j] == "o":
            if a1 == "S":
                if a0 == "S":
                    ans.append("S")
                    a0 = "S"
                    a1 = "S"
                else:
                    ans.append("W")
                    a0 = "S"
                    a1 = "W"
            else:
                if a0 == "S":
                    ans.append("W")
                    a0 = "W"
                    a1 = "W"
                else:
                    ans.append("S")
                    a0 = "W"
                    a1 = "S"
        else:
            if a1 == "W":
                if a0 == "S":
                    ans.append("S")
                    a0 = "W"
                    a1 = "S"
                else:
                    ans.append("W")
                    a0 = "W"
                    a1 = "W"
            else:
                if a0 == "S":
                    ans.append("W")
                    a0 = "S"
                    a1 = "W"
                else:
                    ans.append("S")
                    a0 = "S"
                    a1 = "S"
    if ans[0] == ans[n]:
        a0 = ans[n-1]
        a1 = ans[n]
        if s[0] == "o":
            if a1 == "S":
                if a0 == "S":
                    ans.append("S")
                    a0 = "S"
                    a1 = "S"
                else:
                    ans.append("W")
                    a0 = "S"
                    a1 = "W"
            else:
                if a0 == "S":
                    ans.append("W")
                    a0 = "W"
                    a1 = "W"
                else:
                    ans.append("S")
                    a0 = "W"
                    a1 = "S"
        else:
            if a1 == "W":
                if a0 == "S":
                    ans.append("S")
                    a0 = "W"
                    a1 = "S"
                else:
                    ans.append("W")
                    a0 = "W"
                    a1 = "W"
            else:
                if a0 == "S":
                    ans.append("W")
                    a0 = "S"
                    a1 = "W"
                else:
                    ans.append("S")
                    a0 = "S"
                    a1 = "S"
        if ans[1] == ans[n+1]:
            key = "T"
            break
if key == "T":
    for i in range(n):
        print(ans[i],end="")
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return "".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = onem()

a = list(input())[:-1]

pata = ["S","W"]

for fi in pata:
    for se in pata:
        aaa = [fi,se]
        for i in range(1,n-1):
            if aaa[-1] == "S":
                if a[i] == "o":
                    aaa.append(aaa[-2])
                else:
                    if aaa[-2] == "S":
                        aaa.append("W")
                    else:
                        aaa.append("S")
            else:
                if a[i] == "o":
                    if aaa[-2] == "S":
                        aaa.append("W")
                    else:
                        aaa.append("S")
                    
                else:
                    aaa.append(aaa[-2])
        flag = True

        if aaa[-1] == "S":
            if a[-1] == "o":
                if aaa[-2] != aaa[0]:
                    flag = False
            else:
                if aaa[-2] == aaa[0]:
                    flag = False
        else:
            if a[-1] == "x":
                if aaa[-2] != aaa[0]:
                    flag = False
            else:
                if aaa[-2] == aaa[0]:
                    flag = False
        if aaa[0] == "S":
            if a[0] == "o":
                if aaa[-1] != aaa[1]:
                    flag = False
            else:
                if aaa[-1] == aaa[1]:
                    flag = False
        else:
            if a[0] == "x":
                if aaa[-1] != aaa[1]:
                    flag = False
            else:
                if aaa[-1] == aaa[1]:
                    flag = False
        if flag:
            print(jo(aaa))
            exit()
print(-1)
exit()

Submission status: Accepted

Solution code:
N=int(input())
s=list(input())
for i in range(2**2):
  sw=[1,1]
  ans=[1]*(N+1)
  for j in range(2):
    if (i>>j)%2==1:
      sw[j]=0
      ans[j]=0
  for j in range(1,N):
    if sw[1]== 1:
      if s[j]=="o":
        sw[0],sw[1]=sw[1],sw[0]
      else:
        sw[0],sw[1]=sw[1],(sw[0])^1
    else:
      if s[j]=="o":
        sw[0],sw[1]=sw[1],(sw[0])^1
      else:
        sw[0],sw[1]=sw[1],sw[0]
    ans[j+1]=sw[1]
  
  if ans[0]==ans[N]:
    if ans[0]==1:
      if s[0]=="o":
        if ans[N-1]!=ans[1]:
          continue
      else:
        if ans[N-1]==ans[1]:
          continue
    else:
      if s[0]=="o":
        if ans[N-1]==ans[1]:
          continue
      else:
        if ans[N-1]!=ans[1]:
          continue
    for i in range(N):
      if ans[i]==1:
        ans[i]="S"
      else:
        ans[i]="W"
    print("".join(ans[:N]))
    exit(0)
  else:
    continue
print(-1)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin
 
ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N = ri()
S = rs()
d = {'S': 'W', 'W': 'S'}
for x in ['SS', 'SW', 'WS', 'WW']:
    animals = x
    for i in range(N):
        if S[i] == 'o':
            if animals[i+1] == 'S':
                animals += animals[i]
            else:
                animals += d[animals[i]]
        else:
            if animals[i+1] == 'S':
                animals += d[animals[i]]
            else:
                animals += animals[i]
    if animals[0:2] == animals[-2:]:
        print(animals[1:-1])
        exit()

print(-1)

#08

Submission status: Accepted

Solution code:
def main():
  N = int(input())
  s = input()
  L = [["S","S"],["S","W"],["W","W"],["W","S"]]
  for i in range(1,N):
    for j in range(4):
      if s[i] == "o":
        if L[j][-1] == "S":
          L[j].append(L[j][-2])
        else:
          if L[j][-2] == "S":
            L[j].append("W")
          else:
            L[j].append("S")
      else:
        if L[j][-1] == "S":
          if L[j][-2] == "S":
            L[j].append("W")
          else:
            L[j].append("S")
        else:
          L[j].append(L[j][-2])
  for i in range(4):
    flg = False
    if L[i][0] != L[i][-1]:
      continue
    if s[0] == "o":
      if L[i][-1] == "S" and L[i][1] == L[i][-2]:
        flg = True
      elif L[i][-1] == "W" and L[i][1] != L[i][-2]:
        flg = True
    else:
      if L[i][-1] == "W" and L[i][1] == L[i][-2]:
        flg = True
      elif L[i][-1] == "S" and L[i][1] != L[i][-2]:
        flg = True
    if flg:
      print(*L[i][:-1],sep="")
      exit()
  print(-1)
main()

Submission status: Accepted

Solution code:
n=int(input())
w1=list(input())
s="S"
w="W"
o="o"
x="x"
l=[[s,s],[s,w],[w,w],[w,s]]
for j in range(4):
    for i in range(1,n):
        if w1[i]==o:
            if l[j][-1]==s:
                if l[j][-2]==s:
                    l[j].append(s)
                else:
                    l[j].append(w)
            else:
                if l[j][-2]==s:
                    l[j].append(w)
                else:
                    l[j].append(s)
        else:
            if l[j][-1]==s:
                if l[j][-2]==s:
                    l[j].append(w)
                else:
                    l[j].append(s)
            else:
                if l[j][-2]==s:
                    l[j].append(s)
                else:
                    l[j].append(w)
b=False
for i in range(4):
  if l[i][0]==l[i][-1] and not b:
    del l[i][-1]
    if l[i][0]==s:
      if (l[i][1]==l[i][-1] and w1[0]==o) or (l[i][1]!=l[i][-1] and w1[0]==x):
        answer=l[i]
        b=True
        break
    else:
      if (l[i][1]!=l[i][-1] and w1[0]==o) or (l[i][1]==l[i][-1] and w1[0]==x):
        answer=l[i]
        b=True
        break
if not b:
    print(-1)
else:
    print("".join(answer))

Submission status: Accepted

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

for a0,a1 in [[1,1],[1,-1],[-1,1],[-1,-1]]:
  ans=[0]*N
  ans[0],ans[1]=a0,a1
  for i in range(2,N):
    if S[i-1]=="o":
      ans[i]=ans[i-2]*ans[i-1]
    else:
      ans[i]=-ans[i-2]*ans[i-1]

  if S[N-1]=="o":
    if ans[0]==ans[N-2]*ans[N-1]:
      if S[0]=="o":
        if ans[1]==ans[N-1]*ans[0]:
          print("".join(map(lambda x:"S" if x==1 else "W",ans)))
          exit()
      else:
        if ans[1]==-ans[N-1]*ans[0]:
          print("".join(map(lambda x:"S" if x==1 else "W",ans)))
          exit()
  else:
    if ans[0]==-ans[N-2]*ans[N-1]:
      if S[0]=="o":
        if ans[1]==ans[N-1]*ans[0]:
          print("".join(map(lambda x:"S" if x==1 else "W",ans)))
          exit()
      else:
        if ans[1]==-ans[N-1]*ans[0]:
          print("".join(map(lambda x:"S" if x==1 else "W",ans)))
          exit()

print(-1)

Submission status: Accepted

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

isF = [False for i in range(N)]
isT = [False for i in range(N)]

def isSatisfied():
    for i in range(2, N+2):
        different = (S[(i - 1) % N] == "x" and isT[(i - 1) % N]) or (S[(i - 1) % N] == "o" and isF[(i - 1) % N])
        if different:
            isF[i % N] |= not isF[(i-2) % N]
            isT[i % N] |= not isT[(i-2) % N]
        else:
            isF[i % N] |= isF[(i-2) % N]
            isT[i % N] |= isT[(i-2) % N]
    for j in range(2, N+2):
        i = 1-i
        different = (S[(i + 1) % N] == "x" and isT[(i + 1) % N]) or (S[(i + 1) % N] == "o" and isF[(i + 1) % N])
        if different:
            isF[i % N] |= not isF[(i+2) % N]
            isT[i % N] |= not isT[(i+2) % N]
        else:
            isF[i % N] |= isF[(i+2) % N]
            isT[ i% N] |= isT[(i+2) % N]
    for i in range(N):
        if isF[i] and isT[i]: return False
    return True

for a in [False, True]:
    for b in [False, True]:
        isF = [False for i in range(N)]
        isT = [False for i in range(N)]
        isF[0] = a
        isT[0] = not a
        isF[1] = b
        isT[1] =  not b
        if isSatisfied():
            print("".join(["S" if isT[i] else "W" for i in range(N)]))
            quit()
print(-1)

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    s=input()
    s=s[1:]+s[0]
    def check(i):
        if ans[-1]=="S":
            if s[i]=="o":
                ans.append(ans[-2])
            else:
                if ans[-2]=="S":
                    ans.append("W")
                else:
                    ans.append("S")
        else:
            if s[i]=="x":
                ans.append(ans[-2])
            else:
                if ans[-2]=="S":
                    ans.append("W")
                else:
                    ans.append("S")
    ans=["S","S"]
    for i in range(n):
        check(i)
    if ans[0]==ans[-2] and ans[1]==ans[-1]:
        print("".join(ans[:-2]))
        return 0
    ans=["S","W"]
    for i in range(n):
        check(i)
    if ans[0]==ans[-2] and ans[1]==ans[-1]:
        print("".join(ans[:-2]))
        return 0
    ans=["W","S"]
    for i in range(n):
        check(i)
    if ans[0]==ans[-2] and ans[1]==ans[-1]:
        print("".join(ans[:-2]))
        return 0
    ans=["W","W"]
    for i in range(n):
        check(i)
    if ans[0]==ans[-2] and ans[1]==ans[-1]:
        print("".join(ans[:-2]))
        return 0
    print(-1)
main()

Submission status: Accepted

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    s = [ True if sv == "o" else False for sv in list(str(readline().rstrip().decode('utf-8')))]
    dp = [[True] * n for _ in range(4)]
    dp[0][0] = True
    dp[0][1] = True
    dp[1][0] = False
    dp[1][1] = False
    dp[2][0] = True
    dp[2][1] = False
    dp[3][0] = False
    dp[3][1] = True
    for i in range(1, n-1):
        for j in range(4):
            if dp[j][i] == s[i]:
                dp[j][i+1] = dp[j][i-1]
            else:
                dp[j][i + 1] = not dp[j][i - 1]
    for i in range(4):
        b = True
        if dp[i][0] == s[0]:
            if dp[i][1] != dp[i][-1]:
                b = False
        else:
            if dp[i][1] == dp[i][-1]:
                b = False

        if dp[i][-1] == s[-1]:
            if dp[i][0] != dp[i][-2]:
                b = False
        else:
            if dp[i][0] == dp[i][-2]:
                b = False
        if b:
            ans = []
            for j in range(len(dp[i])):
                ans.append("S" if dp[i][j] else "W")
            print("".join(ans))
            exit()
    print(-1)


if __name__ == '__main__':
    solve()