Problem:


Score : 500 points

Problem Statement

Snuke, who loves animals, built a zoo.

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

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

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

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

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

Constraints

3 ≤ N ≤ 10^{5}

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

Input

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

N
s

Output

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

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

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

Sample Input 1

6
ooxoox

Sample Output 1

SSSWWS

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

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

Sample Input 2

3
oox

Sample Output 2

-1

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

Sample Input 3

10
oxooxoxoox

Sample Output 3

SSWWSSSWWS



Sample input:
6
ooxoox


Sample output:
SSSWWS

Submission status: Wrong Answer

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

l1 = ['S']
p1 = 'S'

l2 = ['S']
p2 = 'W'

l3 = ['W']
p3 = 'S'

l4 = ['W']
p4 = 'W'


def when_o_next(p, c):
  if c == 'S':
    if p == 'S':
      return 'S'
    if p == 'W':
      return 'W'
  if c == 'W':
    if p == 'S':
      return 'W'
    if p == 'W':
      return 'S'

def when_x_next(p, c):
  if c == 'S':
    if p == 'S':
      return 'W'
    if p == 'W':
      return 'S'
  if c == 'W':
    if p == 'S':
      return 'S'
    if p == 'W':
      return 'W'

def next(f, p, c):
  if f == 'o':
    return when_o_next(p, c)
  return when_x_next(p, c)

for i in range(N):
  s = SS[i]

  c1 = l1[i]
  n1 = next(s, p1, c1)
  l1.append(n1)
  p1 = c1

  c2 = l2[i]
  n2 = next(s, p2, c2)
  l2.append(n2)
  p2 = c2

  c3 = l3[i]
  n3 = next(s, p3, c3)
  l3.append(n3)
  p3 = c3

  c4 = l4[i]
  n4 = next(s, p4, c4)
  l4.append(n4)
  p4 = c4

import sys
if l1[0] == l1[N]:
  print(''.join(l1[:-1]))
  sys.exit()
if l2[0] == l2[N]:
  print(''.join(l2[:-1]))
  sys.exit()
if l3[0] == l3[N]:
  print(''.join(l3[:-1]))
  sys.exit()
if l4[0] == l4[N]:
  print(''.join(l4[:-1]))
  sys.exit()

print(-1)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/arc069/tasks/arc069_b

from collections import deque

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

# 両方試すかな？ (1が羊(0)のとき，1が狼(1)のとき)
def valid(ans):
    # print(ans)
    for i in range(1, N - 1):
        if ans[i] == 0:
            if S[i] == 'o':
                ans[i + 1] = ans[i - 1]
            else:
                ans[i + 1] = 1 - ans[i - 1]
        else:
            if S[i] == 'o':
                ans[i + 1] = 1 - ans[i - 1]
            else:
                ans[i + 1] = ans[i - 1]
    if ans[-1] == 0:
        if S[-1] == 'o':
            return ans[-2] == ans[0]
        else:
            return ans[-2] != ans[0]
    else:
        if S[-1] == 'o':
            return ans[-2] != ans[0]
        else:
            return ans[-2] == ans[0]

for (v0, v1) in [(0, 0), (0, 1), (1, 0), (1, 1)]:
    ans = [-1] * N
    ans[0] = v0
    ans[1] = v1
    flag = valid(ans)
    # print(ans, flag)
    if flag:
        for ch in ans:
            print('S' if ch == 0 else 'W', end='')
        print()
        exit()

print("-1")

Submission status: Wrong Answer

Solution code:
def solve(bfo2, bfo1):
    ans = [bfo2, bfo1]
    for i in s[1:]:
        if ((bfo1 == 'S' and i == 'o')
                or (bfo1 == 'W' and i == 'x')):
            ans.append(bfo2)
        else:
            ans.append('S' if bfo2 == 'W' else 'W')
        bfo1, bfo2 = ans[-1], bfo1
    if ans[0] == ans[-1]:
        print(*ans[:n], sep='')
        exit()


n, s = int(input()), input()

for i in ['S', 'W']:
    for j in ['S', 'W']:
        solve(i, j)
print(-1)

Submission status: Wrong Answer

Solution code:
def solve(bfo2, bfo1):
    ans = [bfo2, bfo1]
    for i in s[1:]:
        if ((bfo1 == 'S' and i == 'o')
                or (bfo1 == 'W' and i == 'x')):
            ans.append(bfo2)
        else:
            ans.append('S' if bfo2 == 'W' else 'W')
        bfo1, bfo2 = ans[-1], bfo1
    if ans[0] == ans[1]:
        print(*ans[:n], sep='')
        exit()


n, s = int(input()), input()

for i in ['S', 'W']:
    for j in ['S', 'W']:
        solve(i, j)
print(-1)

Submission status: Wrong Answer

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

ans = ['S', 'S']
bfo1, bfo2 = ans[-1], ans[-2]
for i in s[1:]:
    if ((bfo1 == 'S' and i == 'o')
            or (bfo1 == 'W' and i == 'x')):
        ans.append(bfo2)
    else:
        ans.append('S' if bfo2 == 'W' else 'W')
    bfo1, bfo2 = ans[-1], bfo1
print(*(ans[:n] if ans[0] == ans[-1] else[-1]), sep='')

Submission status: Wrong Answer

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

ans = ['S', 'S']
bfo1, bfo2 = ans[-1], ans[-2]
for i in s:
    if ((bfo1 == 'S' and i == 'o')
            or (bfo1 == 'W' and i == 'x')):
        ans.append(bfo2)
    else:
        ans.append('S' if bfo2 == 'W' else 'W')
    bfo1, bfo2 = ans[-1], bfo1
print(*(ans[1:n + 1] if ans[0] == ans[-1] else[-1]), sep='')

Submission status: Wrong Answer

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

ans = ['S', 'S']
bfo1, bfo2 = ans[-1], ans[-2]
for i in s:
    if ((bfo1 == 'S' and i == 'o')
            or (bfo1 == 'W' and i == 'x')):
        ans.append(bfo2)
    else:
        ans.append('S' if bfo2 == 'W' else 'W')
    bfo1, bfo2 = ans[-1], bfo1
print(*(ans[1:n + 1] if ans[0:2] == ans[n:n + 2] else[-1]), sep='')

Submission status: Wrong Answer

Solution code:
import sys,math,collections,itertools
input = sys.stdin.readline

def solve(n0,n1):
    #wolf = 1,sheep=0
    ws = [-1]*len(s)
    ws[0]=n0
    ws[1]=n1
    for i in range(1,len(s)-1):
        if s[i]=='o' and ws[i]==0:#羊で、両隣が同じと証言
            ws[i+1]=ws[i-1]
        elif  s[i]=='x' and ws[i]==0:#羊で、両隣が違うと証言
            ws[i+1]=(ws[i-1]+1)%2
        elif s[i]=='o' and ws[i]==1:#狼で、両隣が違う証言
            ws[i+1]=(ws[i-1]+1)%2
        elif s[i]=='x' and ws[i]==1:#狼で、両隣が同じと証言
            ws[i+1]=ws[i-1]
    if ws[0] == ws[-2] and ws[1]==ws[-1]:
        return ws[1:-1]
    else:
        return 0

N=int(input())
s=input().rstrip()
s=s[-1]+s+s[0]

for i in range(2):
    for j in range(2):
        ws = solve(i,j)
        if ws:
            for w in ws:
                if w==0:
                    print('s',end='')
                else:
                    print('w',end='')
            exit()
print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
ans = ""
if s.count("x") % 2 == 1:
    print(-1)
else:
    b = 0
    for i in s:
        if b == 1 and i == "o":
            ans += "W"
        else:
            ans += "S"
        b ^= (i=="x")
    print(ans)

Submission status: Wrong Answer

Solution code:
def check(i,j):
  if s[j-1]=='o':
    if a[j-1]=='S':return a[i]==a[j]
    else:return a[i]!=a[j]
  else:
    if a[j-1]=='S':return a[i]!=a[j]
    else:return a[i]==a[j]
n=int(input())
s=input()
sw='SW'
for i in 'SW':
  a='S'
  a+=i
  for i,j in enumerate(s[1:n-1],1):
    if j=='o':
      if a[-1]=='S':a+=a[-2]
      else:a+=sw[a[-2]=='S']
    else:
      if a[-1]=='S':a+=sw[a[-2]=='S']
      else:a+=a[-2]
  if check(-1,1) and check(-2,0):
    flag=1
    break
  else:flag=0
print([-1,a][flag])

Submission status: Wrong Answer

Solution code:
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n = int(input())
s = input()
for S in ["SS","SW","WS","WW"]:
  st = S
  for i in range(1,n):
    if s[i] == "o":
      if st[i] == "S":
        st += st[i-1]
      else:
        st += ("W" if st[i-1] == "S" else "S")
    else:
      if st[i] == "S":
        st += ("W" if st[i-1] == "S" else "S")
      else:
        st += st[i-1]
  # print(st,"debug")
  if st[0] == st[-1]:
    if st[0] == "S":
      if st[1] == st[-2]:
        print(*st[:n],sep = "")
        exit()
    else:
      if st[1] != st[-2]:
        print(*st[:n],sep = "")
        exit()
print(-1)

Submission status: Wrong Answer

Solution code:
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n = int(input())
s = input()
for S in ["SS","SW","WS","WW"]:
  st = S
  for i in range(1,n):
    if s[i] == "o":
      if st[i] == "S":
        st += st[i-1]
      else:
        st += ("W" if st[i-1] == "S" else "S")
    else:
      if st[i] == "S":
        st += ("W" if st[i-1] == "S" else "S")
      else:
        st += st[i-1]
  # print(st,"debug")
  if st[0] == st[-1]:
    print(*st[:n],sep = "")
    exit()
print(-1)

Submission status: Wrong Answer

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

def calc():
  global ans
  for i in range(1, n-1):
    temp = s[i]
    if ans[i]=="S":
      if temp=="o":
        ans[i+1]=ans[i-1]
      else:
        if ans[i-1]=="S":
          ans[i+1]="W"
        else:
          ans[i+1]="S"
    else:
      if temp=="o":
        if ans[i-1]=="S":
          ans[i+1]="W"
        else:
          ans[i+1]="S"
      else:
        ans[i+1]=ans[i-1]

def judge():
  global ans
  global last
  if ans[n-1]!=last:
    return False
  else:
    if ans[n-1]=="S":
      if s[n-1]=="o":
        if ans[n-2]==ans[0]:
          return True
        else:
          return False
      else:
        if ans[n-2]==ans[0]:
          return False
        else:
          return True
    else:
      if s[n-1]=="o":
        if ans[n-2]==ans[0]:
          return False
        else:
          return True
      else:
        if ans[n-2]==ans[0]:
          return True
        else:
          return False
#1 i=0 が　羊だとする。
##1-1 i=1が羊の場合
ans = ["a"]*n
ans[0]="S"
ans[1]="S"
if s[0]=="o":
  last = "S"
else:
  last = "W"

calc()
#print(ans)
if judge():
  print("".join(ans))
  quit()
  
##1-2 i=1が狼 
ans = ["a"]*n
ans[0]="S"
ans[1]="W"
if s[0]=="o":
  last = "W"
else:
  last = "S"
  
calc()
#print(ans)
if judge():
  print("".join(ans))
  quit()

print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = input()
#print(s)
d = ['S','W']
for i in d:
    for j in d:
        ans = [i,j]
        for k in range(2,N):
            if s[k-1] == 'o':
                if ans[-1] == 'S':
                    ans.append(ans[-2])
                else:
                    if ans[-2] == 'S':
                        ans.append('W')
                    else:
                        ans.append('S')
            else:
                if ans[-1] == 'S':
                    if ans[-2] == 'S':
                        ans.append('W')
                    else:
                        ans.append('S')
                else:
                    ans.append(ans[-2])
        #print(ans)
        flg = True
        if ans[-1] == 'S':
            if s[-1] == 'o':
                if ans[-2] != ans[0]:
                    flg = False
            else:
                if ans[-2] == ans[0]:
                    flg = False
        else:
            if s[-1] == 'x':
                if ans[-2] != ans[0]:
                    flg = False
            else:
                if ans[-2] == ans[0]:
                    flg = False
        if ans[0] == 'S':
            if s[0] == 'o':
                if ans[-1] != ans[1]:
                    flg = False
            else:
                if ans[-1] == ans[1]:
                    flg = False
        else:
            if s[0] == 'x':
                if ans[-1] != ans[1]:
                    flg = False
            else:
                if ans[-1] == ans[1]:
                    flg = False
        if flg == True:
            print(''.join(ans))
            exit()

            print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = input()
#print(s)
d = ['S','W']
for i in d:
    for j in d:
        ans = [i,j]
        for k in range(2,N):
            if s[k-1] == 'o':
                if ans[-1] == 'S':
                    ans.append(ans[-2])
                else:
                    if ans[-2] == 'S':
                        ans.append('W')
                    else:
                        ans.append('S')
            else:
                if ans[-1] == 'S':
                    if ans[-2] == 'S':
                        ans.append('W')
                    else:
                        ans.append('S')
                else:
                    ans.append(ans[-2])
        #print(ans)
        flg = True
        if ans[-1] == 'S':
            if s[-1] == 'o':
                if ans[-2] != ans[0]:
                    flg = False
            else:
                if ans[-2] == ans[0]:
                    flg = False
        else:
            if s[-1] == 'x':
                if ans[-2] != ans[0]:
                    flg = False
                else:
                    if ans[-2] == ans[0]:
                        flg = False
        if ans[0] == 'S':
            if s[0] == 'o':
                if ans[-1] != ans[1]:
                    flg = False
            else:
                if ans[-1] == ans[1]:
                    flg = False
        else:
            if s[0] == 'x':
                if ans[-1] != ans[1]:
                    flg = False
            else:
                if ans[-1] == ans[1]:
                    flg = False
        if flg == True:
            print(''.join(ans))
            exit()
print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = input()
d = ['S','W']
for i in d:
    for j in d:
        ans = [i,j]
        for k in range(2,N):
            if s[k-1] == 'o':
                if ans[-1] == 'S':
                    ans.append(ans[-2])
                else:
                    if ans[-2] == 'S':
                        ans.append('W')
                    else:
                        ans.append('S')
            else:
                if ans[-1] == 'S':
                    if ans[-2] == 'S':
                        ans.append('W')
                    else:
                        ans.append('S')
                else:
                    ans.append(ans[-2])
        flg = True
        if ans[-1] == 'S':
            if s[-1] == 'o':
                if ans[-2] != ans[0]:
                    flg = False
            else:
                if ans[-2] == ans[0]:
                    flg = False
        else:
            if s[-1] == 'x':
                if ans[-2] != ans[0]:
                    flg = False
                else:
                    if ans[-2] == ans[0]:
                        flg = False
        if ans[0] == 'S':
            if s[0] == 'o':
                if ans[-1] != ans[1]:
                    flg = False
            else:
                if ans[-1] == ans[1]:
                    flg = False
        else:
            if s[0] == 'x':
                if ans[-1] != ans[1]:
                    flg = False
            else:
                if ans[-1] == ans[1]:
                    flg = False
        if flg == True:
            print(''.join(ans))
            exit()
print(-1)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    s = list(input())
    for t0,t1 in [('S','S'),('S','W'),('W','S'),('W','W')]:
        T = ['S'] * (N + 1)

        T[0] = t0
        T[1] = t1

        for i in range(1,N):
            if s[i] == 'o':
                if T[i] == 'S':
                    T[i+1] = T[i-1]
                else:
                    if T[i-1] == 'W':
                        T[i+1] = 'S'
                    else:
                        T[i+1] = 'W'
            else:
                if T[i] == 'W':
                    T[i+1] = T[i-1]
                else:
                    if T[i-1] == 'W':
                        T[i+1] = 'S'
                    else:
                        T[i+1] = 'W'

        if T[-1] == T[0]:
            if T[0] == 'S':
                if s[0] == 'o' and T[-2] == T[1]:
                    T.pop(-1)
                    print(''.join(T))
                elif s[0] == 'x' and T[-2] != T[1]:
                    T.pop(-1)
                    print(''.join(T))
            else:
                if s[0] == 'x' and T[-2] == T[1]:
                    T.pop(-1)
                    print(''.join(T))
                elif s[0] == 'o' and T[-2] != T[1]:
                    T.pop(-1)
                    print(''.join(T))


    print(-1)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    s = list(input())
    for t0,t1 in [('S','S'),('S','W'),('W','S'),('W','W')]:
        T = ['S'] * (N + 1)

        T[0] = t0
        T[1] = t1

        for i in range(1,N):
            if s[i] == 'o':
                if T[i] == 'S':
                    T[i+1] = T[i-1]
                else:
                    if T[i-1] == 'W':
                        T[i+1] = 'S'
                    else:
                        T[i+1] = 'W'
            else:
                if T[i] == 'W':
                    T[i+1] = T[i-1]
                else:
                    if T[i-1] == 'W':
                        T[i+1] = 'S'
                    else:
                        T[i+1] = 'W'

        if T[N] == T[0]:
            T.pop(-1)
            print(''.join(T))
            exit()

    print(-1)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import product

n = int(input())
s = [c == "o" for c in input()]


def is_consistent(is_sheep_0, is_sheep_1):
    list_is_sheep = [None] * (n + 1)
    list_is_sheep[0] = is_sheep_0
    list_is_sheep[1] = is_sheep_1
    for i in range(2, n + 1):
        is_sheep = list_is_sheep[i - 2]
        if not s[i - 1]:
            is_sheep = not is_sheep
        if not list_is_sheep[i - 1]:
            is_sheep = not is_sheep
        list_is_sheep[i] = is_sheep
    return (list_is_sheep[0] == list_is_sheep[-1]), list_is_sheep


def answer():
    for is_sheep_0, is_sheep_1 in product([True, False], repeat=2):
        c, list_is_sheep = is_consistent(is_sheep_0, is_sheep_1)
        if c:
            return list_is_sheep
    return -1



answer = answer()

if answer == -1:
    print(answer)
else:
    print("".join(["S" if is_sheep else "W" for is_sheep in answer[:-1]]))

Submission status: Wrong Answer

Solution code:
# 決め打ちすることを考える。i=0をS/Wとしても、結局i=1が分からないので先に進めない。なのでi=1も決め打ちする。
# すべてのiを決め打ちする必要はなく、これだけ決めればあとは決まっていく。
# したがって初期状態は4通り

n = int(input())
s = list(input())


def f(pattern):
    for i in range(1, n):
        if pattern[-1] == "S":
            if s[i] == "o":
                # ひとつ前と一緒
                pattern.append(pattern[-2])
            else:
                pattern.append("S" if pattern[-2] == "W" else "W")
        else:
            if s[i] == "o":
                # ひとつ前と別
                pattern.append("S" if pattern[-2] == "W" else "W")
            else:
                pattern.append(pattern[-2])
    return pattern


for p in [["S", "S"], ["S", "W"], ["W", "W"], ["W", "S"]]:
    ret = f(p[:])
    if ret[-1] == ret[0]:
        print("".join(ret[:n]))
        exit()
print(-1)

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('o') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def solve(N, S, ans):

    for i in range(N - 2):
        if S[i]:
            if ans[i] == 0 and ans[i - 1] == 0:
                ans[i + 1] = 0
            elif ans[i] == 0 and ans[i - 1] == 1:
                ans[i + 1] = 1
            elif ans[i] == 1 and ans[i - 1] == 0:
                ans[i + 1] = 1
            elif ans[i] == 1 and ans[i - 1] == 1:
                ans[i + 1] = 0
        else:
            if ans[i] == 0 and ans[i - 1] == 0:
                ans[i + 1] = 1
            elif ans[i] == 0 and ans[i - 1] == 1:
                ans[i + 1] = 0
            elif ans[i] == 1 and ans[i - 1] == 0:
                ans[i + 1] = 0
            elif ans[i] == 1 and ans[i - 1] == 1:
                ans[i + 1] = 1

    ret = True
    for i in range(N - 1, N):

        if S[i]:
            if ans[i] == 0 and ans[i - 1] == 0:
                ret &= ans[(i + 1) % N] == 0
            elif ans[i] == 0 and ans[i - 1] == 1:
                ret &= ans[(i + 1) % N] == 1
            elif ans[i] == 1 and ans[i - 1] == 0:
                ret &= ans[(i + 1) % N] == 1
            elif ans[i] == 1 and ans[i - 1] == 1:
                ret &= ans[(i + 1) % N] == 0
        else:
            if ans[i] == 0 and ans[i - 1] == 0:
                ret &= ans[(i + 1) % N] == 1
            elif ans[i] == 0 and ans[i - 1] == 1:
                ret &= ans[(i + 1) % N] == 0
            elif ans[i] == 1 and ans[i - 1] == 0:
                ret &= ans[(i + 1) % N] == 0
            elif ans[i] == 1 and ans[i - 1] == 1:
                ret &= ans[(i + 1) % N] == 1

    return ret


def main():

    N = in_n()
    S = in_map()

    pattern = [(0, 0), (0, 1), (1, 0), (1, 1)]

    for p in pattern:
        ans = [0] * N
        ans[0], ans[-1] = p[0], p[1]
        if(solve(N, S, ans)):
            p = ''
            for i in range(N):
                if ans[i] == 0:
                    p += 'S'
                else:
                    p += 'W'
            print(p)
            exit()
    else:
        print(-1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('o') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def solve(N, S, ans):

    for i in range(N - 1):
        if S[i]:
            if ans[i] == 0 and ans[i - 1] == 0:
                ans[i + 1] = 0
            elif ans[i] == 0 and ans[i - 1] == 1:
                ans[i + 1] = 1
            elif ans[i] == 1 and ans[i - 1] == 0:
                ans[i + 1] = 1
            elif ans[i] == 1 and ans[i - 1] == 1:
                ans[i + 1] = 0
        else:
            if ans[i] == 0 and ans[i - 1] == 0:
                ans[i + 1] = 1
            elif ans[i] == 0 and ans[i - 1] == 1:
                ans[i + 1] = 0
            elif ans[i] == 1 and ans[i - 1] == 0:
                ans[i + 1] = 0
            elif ans[i] == 1 and ans[i - 1] == 1:
                ans[i + 1] = 1

    i = N - 1
    if S[i]:
        if ans[i] == 0 and ans[i - 1] == 0:
            return ans[(i + 1) % N] == 0
        elif ans[i] == 0 and ans[i - 1] == 1:
            return ans[(i + 1) % N] == 1
        elif ans[i] == 1 and ans[i - 1] == 0:
            return ans[(i + 1) % N] == 1
        elif ans[i] == 1 and ans[i - 1] == 1:
            return ans[(i + 1) % N] == 0
    else:
        if ans[i] == 0 and ans[i - 1] == 0:
            return ans[(i + 1) % N] == 1
        elif ans[i] == 0 and ans[i - 1] == 1:
            return ans[(i + 1) % N] == 0
        elif ans[i] == 1 and ans[i - 1] == 0:
            return ans[(i + 1) % N] == 0
        elif ans[i] == 1 and ans[i - 1] == 1:
            return ans[(i + 1) % N] == 1


def main():

    N = in_n()
    S = in_map()

    pattern = [(0, 0), (0, 1), (1, 0), (1, 1)]

    for p in pattern:
        ans = [0] * N
        ans[0], ans[-1] = p[0], p[1]
        if(solve(N, S, ans)):
            p = ''
            for i in range(N):
                if ans[i] == 0:
                    p += 'S'
                else:
                    p += 'W'
            print(p)
            exit()
    else:
        print(-1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

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

for i in range(4):
    ans = ''
    if i == 0:
        ans += 'SS'
    elif i == 1:
        ans += "SW"
    elif i == 2:
        ans += "WS"
    elif i == 3:
        ans += "WW"
    for j in range(N-1):
        if ans[j+1] == 'S':
            if s[j+1] == 'o':
                ans += ans[j]
            else:
                if ans[j] == 'S':
                    ans += 'W'
                else:
                    ans += 'S'
        else:
            if s[j+1] == 'x':
                ans += ans[j]
            else:
                if ans[j] == 'S':
                    ans += 'W'
                else:
                    ans += 'S'
    if ans[0] == ans[-1]:
        print(ans[:-1])
        sys.exit()

print(-1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
s = input()
c0 = [1,1,-1,-1]
c1 = [1,-1,1,-1]
for i in range(4):
  chk = [c0[i],c1[i]]
  for i in range(2,n):
    k = 1 if s[i-1] == 'o' else -1
    c = chk[-2]*chk[-1]*k
    chk.append(c)
  K = 1 if s[0] == 'o' else -1
  L = 1 if s[-1] == 'o' else -1
  if chk[0]*chk[1]*K == chk[-1] and chk[-2]*chk[-1]*L == chk[0]:
    chk = [i+1 for i in chk]
    ans = ''.join(list(map(str,chk)))
    ans = ans.replace('2','S')
    ans = ans.replace('0','W')
    print(ans)
    exit()
print(-1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)


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


def resolve():

    n = int(input())
    s = input()
    s += s[0]

    def case1(cur, ans):
        if cur == n-1:
            if ans[0] == ans[-1]:

                print(ans[:-1])
                exit()
            else:
                case2(0, 'SW')

        elif (s[cur] == 'o' and s[cur+1] == 'o'):
            cur += 1
            ans += 'S'
            case1(cur, ans)
        elif (s[cur] == 'o' and s[cur+1] == 'x'):
            cur += 1
            ans += 'W'
            case2(cur, ans)
        elif (s[cur] == 'x' and s[cur+1] == 'o'):
            cur += 1
            ans += 'S'
            case1(cur, ans)
        else:
            cur += 1
            ans += 'W'
            case2(cur, ans)

    def case2(cur, ans):

        if cur == n-1:
            if ans[0] == ans[-1]:

                print(ans[:-1])
                exit()
            else:
                case3(0, 'WS')

        elif (s[cur] == 'o' and s[cur+1] == 'o'):
            cur += 1
            ans += 'W'
            case4(cur, ans)
        elif (s[cur] == 'o' and s[cur+1] == 'x'):
            cur += 1
            ans += 'S'
            case3(cur, ans)
        elif (s[cur] == 'x' and s[cur+1] == 'o'):
            cur += 1
            ans += 'W'
            case4(cur, ans)
        else:
            cur += 1
            ans += 'S'
            case3(cur, ans)

    def case3(cur, ans):

        if cur == n-1:
            if ans[0] == ans[-1]:

                print(ans[:-1])
                exit()
            else:
                case4(0, 'WW')

        elif (s[cur] == 'o' and s[cur+1] == 'o'):
            cur += 1
            ans += 'W'
            case2(cur, ans)
        elif (s[cur] == 'o' and s[cur+1] == 'x'):
            cur += 1
            ans += 'S'
            case1(cur, ans)
        elif (s[cur] == 'x' and s[cur+1] == 'o'):
            cur += 1
            ans += 'W'
            case2(cur, ans)
        else:
            cur += 1
            ans += 'S'
            case1(cur, ans)

    def case4(cur, ans):

        if cur == n-1:
            if ans[0] == ans[-1]:

                print(ans[:-1])
                exit()
            else:
                print(-1)
                exit()

        if (s[cur] == 'o' and s[cur+1] == 'o'):
            cur += 1
            ans += 'S'
            case3(cur, ans)
        elif (s[cur] == 'o' and s[cur+1] == 'x'):
            cur += 1
            ans += 'W'
            case4(cur, ans)
        elif (s[cur] == 'x' and s[cur+1] == 'o'):
            cur += 1
            ans += 'S'
            case3(cur, ans)
        else:
            cur += 1
            ans += 'W'
            case4(cur, ans)

    case1(0, 'SS')


resolve()