Problem:


Score : 500 points

Problem Statement

Snuke, who loves animals, built a zoo.

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

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

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

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

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

Constraints

3 ≤ N ≤ 10^{5}

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

Input

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

N
s

Output

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

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

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

Sample Input 1

6
ooxoox

Sample Output 1

SSSWWS

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

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

Sample Input 2

3
oox

Sample Output 2

-1

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

Sample Input 3

10
oxooxoxoox

Sample Output 3

SSWWSSSWWS



Sample input:
6
ooxoox


Sample output:
SSSWWS

Submission status: Accepted

Solution code:
n=int(input())
s=input()
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"


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

Submission status: Accepted

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

def judge(start):
    ans = start
    for i in range(1, N):
        # 羊の処理
        if ans[i]=="S":
            # o or xで分岐
            if s[i]=="o":
                 ans[i+1]=ans[i-1]
            else:
                  # 羊x
                 ans[i+1]= "S" if ans[i-1]=="W" else "W"
        else:
            if s[i]=="o":
                # 狼o
                ans[i+1]= "S" if ans[i-1]=="W" else "W"
            else:
                  # 狼x
                 ans[i+1]=ans[i-1]

    # 判定
    if ans[0]==ans[N]:
        if ans[0]=="S":
            if s[0]=="o" and ans[1] == ans[N-1]:
                print("".join(ans[:-1]))
                exit()
            elif s[0]=="x" and ans[1] != ans[N-1]:
                print("".join(ans[:-1]))
                exit()
        elif ans[0]=="W":
            if s[0]=="o" and ans[1] != ans[N-1]:
                print("".join(ans[:-1]))
                exit()
            elif s[0]=="x" and ans[1] == ans[N-1]:
                print("".join(ans[:-1]))
                exit()
    else:
        return(-1)

judge(list("W"+"W"*(N)))
judge(list("W"+"S"*(N)))
judge(list("S"+"S"*(N)))
judge(list("S"+"W"*(N)))

print(-1)

Submission status: Accepted

Solution code:
import sys
n=int(input())
s=input()
S=[]
for i in range(n):
  if s[i]=="o":
    S.append(1)
  else:
    S.append(-1)
A=[1,1]
for i in range(1,n-1):
  A.append(S[i]*A[-2]*A[-1])
if S[-1]==A[-2]*A[-1]*A[0]:
  if S[0]==A[-1]*A[0]*A[1]:
    for i in range(n):
      if A[i]==1:
        print("S",end="")
      else:
        print("W",end="")
      if i==n-1:
        print()
        sys.exit()
A=[1,-1]
for i in range(1,n-1):
  A.append(S[i]*A[-2]*A[-1])
if S[-1]==A[-2]*A[-1]*A[0]:
  if S[0]==A[-1]*A[0]*A[1]:
    for i in range(n):
      if A[i]==1:
        print("S",end="")
      else:
        print("W",end="")
      if i==n-1:
        print()
        sys.exit()
A=[-1,1]
for i in range(1,n-1):
  A.append(S[i]*A[-2]*A[-1])
if S[-1]==A[-2]*A[-1]*A[0]:
  if S[0]==A[-1]*A[0]*A[1]:
    for i in range(n):
      if A[i]==1:
        print("S",end="")
      else:
        print("W",end="")
      if i==n-1:
        print()
        sys.exit()
A=[-1,-1]
for i in range(1,n-1):
  A.append(S[i]*A[-2]*A[-1])
if S[-1]==A[-2]*A[-1]*A[0]:
  if S[0]==A[-1]*A[0]*A[1]:
    for i in range(n):
      if A[i]==1:
        print("S",end="")
      else:
        print("W",end="")
      if i==n-1:
        print()
        sys.exit()
print(-1)

Submission status: Accepted

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

w = {-1: 'W', 1: 'S'}
p = {1:'o', -1:'x'}

for tmp in [[1, 1], [-1, -1], [1, -1], [-1, 1]]:
    for i in range(1,n):
        tmp.append(tmp[i-1] if s[i] == p[tmp[i]] else (tmp[i-1] * -1))
    if tmp[0] == tmp[-1]:
        flag = 1 if s[0] == p[tmp[0]] else -1
        if tmp[-2] == tmp[1] * flag:
            tmp.pop()
            print(''.join(map(w.get, tmp)))
            break
else:
    print(-1)

Submission status: Accepted

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

w = {-1: 'W', 1: 'S'}
p = {1:'o', -1:'x'}
lst = [[1, 1], [-1, -1], [1, -1], [-1, 1]]

tmp = [0 for i in range(n+1)]
for a in lst:
    tmp[0] = a[0]
    tmp[1] = a[1]
    for i in range(1,n):
        tmp[i+1] = tmp[i-1] if s[i] == p[tmp[i]] else (tmp[i-1] * -1)
    if tmp[0] == tmp[-1]:
        flag = 1 if s[0] == p[tmp[0]] else -1
        if tmp[-2] == tmp[1] * flag:
            tmp.pop()
            print(''.join(map(w.get, tmp)))
            break
else:
    print(-1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
from itertools import product

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
S = input()
S += S[0]

def check(a, b):
    res = [a, b]
    # 自分と1つ前がS,Wどっちかと、自分の発言のo,xが分かれば、次が定まる
    for i in range(1, N+1):
        # 隣が同じで発言がo,違ってxなら羊
        if res[i-1] == res[i] and S[i] == 'o' \
                or res[i-1] != res[i] and S[i] == 'x':
            res.append('S')
        # 隣が同じで発言がx,違ってoなら狼
        else:
            res.append('W')
    # 1周して矛盾していないか
    if res[0] == res[N] and res[1] == res[N+1]:
        return res
    else:
        return []

# 1,2番目がS,Wどっちかの4通り試す
for a, b in product(['S', 'W'], repeat=2):
    res = check(a, b)
    if res:
        print(''.join(res[:N]))
        exit()
print(-1)

Submission status: Accepted

Solution code:
import sys

def rev(t):
    if t == "S":
        return "W"
    else:
        return "S"

N = int(input())

s = input()

for p in range(2):

    for q in range(2):

        lis = []

        if p == 0:
            lis.append("S")
        else:
            lis.append("W")
        if q == 0:
            lis.append("S")
        else:
            lis.append("W")

        for i in range(N-2):

            if lis[-1] == "S":
                if s[i+1] == "o":
                    lis.append(lis[i])
                else:
                    lis.append(rev(lis[i]))

            else:
                if s[i+1] == "o":
                    lis.append(rev(lis[i]))
                else:
                    lis.append(lis[i])

        flag = True
        
        if lis[0] == "S":
            if s[0] == "o" and lis[-1] != lis[1]:
                flag = False
            elif s[0] == "x" and lis[-1] == lis[1]:
                flag = False

        else:
            if s[0] == "o" and lis[-1] == lis[1]:
                flag = False
            elif s[0] == "x" and lis[-1] != lis[1]:
                flag = False

        
        if lis[-1] == "S":
            if s[-1] == "o" and lis[-2] != lis[0]:
                flag = False
            elif s[-1] == "x" and lis[-2] == lis[0]:
                flag = False

        else:
            if s[-1] == "o" and lis[-2] == lis[0]:
                flag = False
            elif s[-1] == "x" and lis[-2] != lis[0]:
                flag = False

        if flag:
            print ("".join(lis))
            sys.exit()

print (-1)

Submission status: Accepted

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

a = {'S':'W', 'W':'S'}
for t0, t1 in ('SS', 'SW', 'WS', 'WW'):
    T = [t0, t1]
    for i in range(2, N+2):
        if (S[(i-1)%N]=='o' and T[-1]=='S') or (S[(i-1)%N]=='x' and T[-1]=='W'):
            T.append(T[-2])
        else:
            T.append(a[T[-2]])

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = ["SS","SW","WS","WW"]
for a in ans:
    for i in range(2,n+1):
        if s[i-1] == "o":
            if a[-2:] == "SS" or a[-2:] == "WW":
                a += "S"
            else:
                a += "W"
        if s[i-1] == "x":
            if a[-2:] == "SW" or a[-2:] == "WS":
                a += "S"
            else:
                a += "W"
    else:
        if a[n] == a[0]:
            if (((s[0],a[0]) in (("o","S"),("x","W")) and a[1] == a[n-1])
                or ((s[0],a[0]) in (("x","S"),("o","W")) and a[1] != a[n-1])):
                print(a[:n])
                exit()
print(-1)

Submission status: Accepted

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

#末尾と1番目を羊または狼と仮定して貪欲。4パターン
import itertools
for x,y in itertools.product(["S","W"], repeat=2):
  ans=[None]*n
  ans[-1]=x
  ans[0]=y
  flag=1
  for i in range(n): #各発言から次の動物を特定する。末尾らへんにきたら矛盾を検証
    if 0<=i<=n-3: #このifに関して間違ってないはず…
      if ans[i]=="S":
        if s[i]=="o":
          ans[i+1]=ans[i-1] #同じと言ってる場合は同じのため、次の番号は2つ前と同じ動物。
        else: #そうじゃない場合は、次の番号は2つ前と違う動物。
          if ans[i-1]=="W":
            ans[i+1]="S"
          else:
            ans[i+1]="W"
      #今の番号が狼の場合は上の全部逆にする
      if ans[i]=="W": 
        if s[i]=="x":
          ans[i+1]=ans[i-1]
        else:
          if ans[i-1]=="W":
            ans[i+1]="S" 
          else:
            ans[i+1]="W"
    else: #iがn-2とn-1の場合は、先頭と末尾の羊を指定するため、矛盾を確認しないといけない。こっからが肝だよ
      if ans[i]=="S":
        if s[i]=="o":
          if ans[(i+1)%n]!=ans[i-1]:
            flag=0
        else:
          if ans[(i+1)%n]==ans[i-1]:
            flag=0
      #今の番号が狼の場合は上の全部逆にする
      if ans[i]=="W":
        if s[i]=="x":
          if ans[(i+1)%n]!=ans[i-1]:
            flag=0
        else:
          if ans[(i+1)%n]==ans[i-1]:
            flag=0
  if flag==1:
    print("".join(ans))
    exit()

#どんなケースでも矛盾するとループを抜けてしまう
print(-1)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,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
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()
    S+=S[0]
    def is_ok():
        for i,s in enumerate(S[1:-1],start=1):
            if ans[i]==(s=="o"):
                ans[i+1]=ans[i-1]
            else:
                ans[i+1]=ans[i-1]^1
        ok = (ans[0]==ans[-1])
        if ans[0]==(S[0]=="o"):
            if ans[1]!=ans[-2]:
                ok=False
        else:
            if ans[1]==ans[-2]:
                ok=False
        return ok

    for bit in range(1<<2):
        ans=[0]*(N+1)
        for i in range(2):
            ans[i] = bit>>i&1
        if is_ok():
            ans="".join(map(str,ans[:-1]))
            print(ans.replace("1","S").replace("0","W"))
                
            return
    print(-1)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from itertools import product

n = int(sys.stdin.readline().rstrip())
s = sys.stdin.readline().rstrip()
resp = s
resp = resp.replace('o', '1')
resp = resp.replace('x', '0')
resp = [None] + [int(x) for x in resp]

def restore(pattern):
    for i in range(1, n):
        if resp[i] ^ pattern[i]:
            pattern[i+1] = 1 if pattern[i-1] ^ 1 else 0
        else:
            pattern[i+1] = pattern[i-1]
    return pattern

def contradicting(pattern):
    if pattern[0] != pattern[-1]:
        return True
    if resp[-1] ^ pattern[-1]:
        if not pattern[-2] ^ pattern[1]:
            return True
    else:
        if pattern[-2] ^ pattern[1]:
            return True   
    return False

def main():
    res = [None] * (n + 1)
    for i in range(4):
        pattern = res.copy()
        pattern[0] = i >> 0 & 1
        pattern[1] = i >> 1 & 1
        ans = restore(pattern)
        if not contradicting(ans):
            t = ''
            for i in range(1, n+1):
                t += 'S' if ans[i] & 1 else 'W'
            return t
    return -1

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

T = [["S", "S"], ["S", "W"], ["W", "S"], ["W", "W"]]

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

Submission status: Accepted

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

def main():
    n = int(input())
    s = input()
    ans = "-1"

    ls = list(product("SW", repeat = 2))

    for tpl in ls:
        anss = str(tpl[0]) + str(tpl[1])
        for i in range(1,n+1):
            if anss[-1] == "S":
                if s[i-1] == "o":
                    anss += anss[-2]
                else:
                    if anss[-2] == "S":
                        anss += "W"
                    else:
                        anss += "S"
            else:
                if s[i-1] == "x":
                    anss += anss[-2]
                else:
                    if anss[-2] == "S":
                        anss += "W"
                    else:
                        anss += "S"
        if anss[:2] == anss[-2:]:
            ans = anss[1:-1]
            break
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:



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



# 1,Nをヒツジと狼のどちらかと仮定して場合分け

for s1,sn in [("S","S"), ("S","W"), ("W","S"), ("W","W")]:
    ans = [""]*(N+2)
    ans[1] = s1
    ans[0] = sn
    for i in range(1,N+1):
       
        if ans[i] == "S":

            if S[i-1] == "o":
                ans[i+1] = "S" if ans[i-1] =="S" else "W"
            else:
                ans[i+1] = "W" if ans[i-1] =="S" else "S"
        else:
            if S[i-1] == "o":
                ans[i+1] = "S" if ans[i-1] =="W" else "W"
            else:
                ans[i+1] = "W" if ans[i-1] =="W" else "S"

    if ans[0] == ans[N] and ans[1] == ans[N+1]:
        print("".join(ans[1:N+1]))
        exit()

print(-1)

Submission status: Accepted

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

def output(lst):
    map_lst = map(str, lst[:-1])
    s = ''.join(map_lst)
    print (s.replace('0', 'S').replace('1', 'W'))
    return

# 0: 羊、1: 狼
for a in range(2):
    for b in range(2):
        lst = [-1] * (N + 1)
        lst[0] = a
        lst[1] = b
        for i in range(1, N):
            if S[i] == 'o':
                flag = True
            else:
                flag = False

            if flag == (lst[i] == 0):
                lst[i + 1] = lst[i - 1]
            else:
                lst[i + 1] = 1 - lst[i - 1]
        if lst[0] == lst[-1]: #最初の仮定が成り立っているかどうか
            if (S[0] == 'o') == (lst[0] == 0):
                if lst[1] == lst[N - 1]:
                    output(lst)
                    exit()
            else:
                if lst[1] != lst[N - 1]:
                    output(lst)
                    exit()

        # print (lst)

print (-1)

Submission status: Accepted

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

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

    # i = n-1
    if ans[n-1] == 'S' and s[n-1] == 'o':
        if ans[n-2] != ans[0]:
            f = False
    elif ans[n-1] == 'S' and s[n-1] == 'x':
        if ans[n-2] == ans[0]:
            f = False
    elif ans[n-1] == 'W' and s[n-1] == 'o':
        if ans[n-2] == ans[0]:
            f = False
    elif ans[n-1] == 'W' and s[n-1] == 'x':
        if ans[n-2] != ans[0]:
            f = False

    # i == 0
    if ans[0] == 'S' and s[0] == 'o':
        if ans[n-1] != ans[1]:
            f = False
    elif ans[0] == 'S' and s[0] == 'x':
        if ans[n-1] == ans[1]:
            f = False
    elif ans[0] == 'W' and s[0] == 'o':
        if ans[n-1] == ans[1]:
            f = False
    elif ans[0] == 'W' and s[0] == 'x':
        if ans[n-1] != ans[1]:
            f = False

    return ans, f


# SS
ans1, f1 = test('SS')
ans2, f2 = test('SW')
ans3, f3 = test('WW')
ans4, f4 = test('WS')

if f1:
    print(ans1)
elif f2:
    print(ans2)
elif f3:
    print(ans3)
elif f4:
    print(ans4)
else:
    print(-1)

Submission status: Accepted

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

def convert(ss):
    if ss == 0:
        return "S"
    else:
        return "W"


def solve(N: int, s: str):
    animals = [0]*N
    ## 0 がひつじ 1が狼を表す
    init = [(0,1),(1,0),(0,0),(1,1)]

    for first,second in init:
        animals[0] = first
        animals[1] = second

        for j in range(1,N):
            ## 最後にたどり着いたとき
            if j == N-1:
                if animals[j] == 0:
                    if s[j] == "o":
                        if animals[j-1] != animals[0]:
                            break
                    else:
                       if animals[j-1] == animals[0]:
                            break
                elif animals[j] == 1:
                    if s[j] == "o":
                        if animals[j-1] == animals[0]:
                            break
                    else:
                       if animals[j-1] != animals[0]:
                            break

                if animals[0] == 0:
                    if s[0] == "o":
                        if animals[N-1] != animals[1]:
                            break
                    else:
                       if animals[N-1] == animals[1]:
                            break
                elif animals[0] == 1:
                    if s[0] == "o":
                        if animals[N-1] == animals[1]:
                            break
                    else:
                       if animals[N-1] != animals[1]:
                            break
                
                print(''.join(list(map(convert,animals))))
                return

            if animals[j] == 0:
                if s[j] == "o":
                    animals[j+1] = animals[j-1]
                else:
                    animals[j+1] = 1^animals[j-1]
            elif animals[j] == 1:
                if s[j] == "o":
                    animals[j+1] = 1^animals[j-1]
                else:
                    animals[j+1] = animals[j-1]    

    print(-1)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    s = next(tokens)  # type: str
    solve(N, s)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import product
n = int(input())
s = input()
s_dict = {"o":1, "x":-1}
flg = False
for combi in product([-1,1], repeat=2):
    state = list(combi)
    for i, _s in enumerate(s[1:], 1):
        cur = state[i]
        res = s_dict[_s] * cur * state[i-1]
        state.append(res)
    if state[0] == state[-1] and state[n-1] == state[0] * state[1] * s_dict[s[0]]:
        flg = True
        break
res = "".join(["S" if i == 1 else "W" for i in state])
if flg:
    print(res[:-1])
else:
    print(-1)

Submission status: Accepted

Solution code:
from itertools import product

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

answer = [''] * N

for i, j in product(['S', 'W'], repeat=2):
    answer[0] = i
    answer[1] = j
    for k in range(2, N):
        if answer[k - 1] == 'S':
            if s[k - 1] == 'o':
                answer[k] = answer[k - 2]
            else:
                if answer[k - 2] == 'W':
                    answer[k] = 'S'
                else:
                    answer[k] = 'W'
        else:
            if s[k - 1] == 'x':
                answer[k] = answer[k - 2]
            else:
                if answer[k - 2] == 'W':
                    answer[k] = 'S'
                else:
                    answer[k] = 'W'

    for k in [-1, 0]:
        if answer[k] == 'S':
            if s[k] == 'o':
                if answer[k - 1] != answer[k + 1]:
                    break
            else:
                if answer[k - 1] == answer[k + 1]:
                    break
        else:
            if s[k] == 'x':
                if answer[k - 1] != answer[k + 1]:
                    break
            else:
                if answer[k - 1] == answer[k + 1]:
                    break
    else:
        print(''.join(answer))
        exit()

print(-1)

Submission status: Accepted

Solution code:
N = int(input())
S = [1 if a == "x" else 0 for a in input()]
for a, b in ((0, 0), (0, 1), (1, 0), (1, 1)):
    X = [a, b]
    for i in range(N):
        X.append(X[-1]^X[-2]^S[i])
    if X[-2:] == X[:2]:
        print("".join(["W" if x else "S" for x in X[1:-1]]))
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
def f(n, s, t):
    for i in range(2, n):
        if (t[i-2] == t[i-1] and s[i-1] == 'o') or (t[i-2] != t[i-1] and s[i-1] == 'x'):
            t += 'S'
        else:
            t += 'W'
    for i in range(0, 2):
        if (t[(i+n-2)%n] == t[(i+n-1)%n] and s[(i+n-1)%n] == 'o') or (t[(i+n-2)%n] != t[(i+n-1)%n] and s[(i+n-1)%n] == 'x'):
            if t[i] != 'S': return ":("
        else:
            if t[i] != 'W': return ":("
    return t
n = int(input())
s = input()
u = ["SS", "SW", "WS", "WW"]
for i in range(4):
    t = f(n, s, u[i])
    if t != ":(":
        print(t)
        exit(0)
print(-1)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
def solve(ans):
    for i in range(2,N):
        if ans[i-2]=='S' and ans[i-1]=='S':
            if s[i-1]=='o':
                ans += 'S'
            else:
                ans += 'W'
        elif ans[i-2]=='S' and ans[i-1]=='W':
            if s[i-1]=='o':
                ans += 'W'
            else:
                ans += 'S'
        elif ans[i-2]=='W' and ans[i-1]=='S':
            if s[i-1]=='o':
                ans += 'W'
            else:
                ans += 'S'
        else:
            if s[i-1]=='o':
                ans += 'S'
            else:
                ans += 'W'
    if (ans[0]=='S' and s[0]=='o') or (ans[0]=='W' and s[0]=='x'):
        if ans[-1]!=ans[1]:
            return
    elif (ans[0]=='S' and s[0]=='x') or (ans[0]=='W' and s[0]=='o'):
        if ans[-1]==ans[1]:
            return
    if (ans[-1]=='S' and s[-1]=='o') or (ans[-1]=='W' and s[-1]=='x'):
        if ans[-2]!=ans[0]:
            return
    elif (ans[-1]=='S' and s[-1]=='x') or (ans[-1]=='W' and s[-1]=='o'):
        if ans[-2]==ans[0]:
            return
    return ans

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

for c in C:
    if solve(c):
        print(solve(c))
        break
else:
    print(-1)

Submission status: Accepted

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

l = [0] * n
l2 = [0 if s[i]=='o' else 1 for i in range(n)]
def f(x,y):
    l[0]=x
    l[1]=y
    for i in range(2,n):
        l[i]=l[i-2]^l[i-1]^l2[i-1]
    if l[0] == l[n-2]^l[n-1]^l2[n-1] and l[1]==l[n-1]^l[0]^l2[0]:
        return True
    else: return False

if f(0,0):
    print(''.join(['S' if not l[i] else 'W' for i in range(n)]))
elif f(0,1):
    print(''.join(['S' if not l[i] else 'W' for i in range(n)]))
elif f(1,0):
    print(''.join(['S' if not l[i] else 'W' for i in range(n)]))
elif f(1,1):
    print(''.join(['S' if not l[i] else 'W' for i in range(n)]))
else:print(-1)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
L1,L2,L3,L4='SS','SW','WS','WW'

def make_seq(L):
    for i in range(1,N-1):
        if S[i]+L[i-1]+L[i] in ['oSS','oWW','xSW','xWS']:
            L+='S'
        else:
            L+='W'
    return L

T=['oSSS','oWSW','oSWW','oWWS','xWSS','xSSW','xSWS','xWWW']
def check(L):
    if S[0]+L[-1]+L[0]+L[1] in T and S[-1]+L[-2]+L[-1]+L[0] in T:
        return L
    else:
        return -1

L1=make_seq(L1)
L2=make_seq(L2)
L3=make_seq(L3)
L4=make_seq(L4)
C=[check(L1),check(L2),check(L3),check(L4)]

import sys
for i in C:
    if i!=-1:
        print(i)
        sys.exit()
print(-1)