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()

C = ('SS', 'SW', 'WS', 'WW')
for i in range(4):
    ans = list(C[i])
    for j in range(1, N):
        if (ans[-1] == 'S' and S[j] == 'o') or(ans[-1] == 'W' and S[j] == 'x'):
            ans.append(ans[-2])
        else:
            ans.append('S' if ans[-2] == 'W' else 'W')
    if ans[0] == ans[-1]:
        ans.pop()
        if (ans[0] == 'S' and S[0] == 'o' and ans[-1] == ans[1]) or \
            (ans[0] == 'S' and S[0] == 'x' and ans[-1] != ans[1]) or \
                (ans[0] == 'W' and S[0] == 'o' and ans[-1] != ans[1]) or \
                (ans[0] == 'W' and S[0] == 'x' and ans[-1] == ans[1]):
            print(''.join(ans))
            exit()
print(-1)

Submission status: Accepted

Solution code:
from collections import deque

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

patterns = ["SS", "SW", "WS", "WW"]

for pat in patterns:
    q = deque(list(pat))
    bef = q[0]
    cur = q[1]
    for i in range(1,N+1):
        aft = ""
        if (cur == "S" and S[i%N] == "o") or (cur == "W" and S[i%N] == "x"):
            aft = "S" if bef == "S" else "W"
        else:
            aft = "S" if bef == "W" else "W"
        q.append(aft)
        bef = cur
        cur = aft

    if q[0] == q[-2] and q[1]==q[-1]:
        for i in range(N):
            print(q.popleft(), end="")
        print()
        exit()

print(-1)

Submission status: Accepted

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

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ZipArray(a):
    aa = [[a[i],i]for i in range(n)]

    aa.sort(key = lambda x : x[0])
    for i in range(n):
        aa[i][0]=i+1
    aa.sort(key = lambda x : x[1])
    b=[aa[i][0] for i in range(len(a))]
    return b

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans

def Zeros(a,b):
    if(b<=-1):
        return [0 for i in range(a)]
    else:
        return [[0 for i in range(b)] for i in range(a)]

def AddV2(v,w):
    return [v[0]+w[0],v[1]+w[1]]

dir4 = [[1,0],[0,1],[-1,0],[0,-1]]


def clamp(x,y,z):
    return max(y,min(z,x))

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
            
#
def Zaatsu(a):
    a.sort()
    now = a[0][0]
    od = 0
    for i in range(n):
        if(now==a[i][0]):
            a[i][0]=od
        else:
            now = a[i][0]
            od+=1
            a[i][0] = od
    
    a.sort(key = lambda x : x[1])
    return a

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 2
N = 10 ** 6 + 2
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

'''

def rl(x):
    return range(len(x))

#a = list(map(int, input().split()))
# list(map(int, input().split()))

#################################################
#################################################
#################################################
#################################################



#22-

n = int(input())

s = input()

ani = [0 for i in range(n)]



def calc():
    for i in range(1,n-1):
        if((ani[i]==0 and s[i]=='o') or (ani[i]==1 and s[i]=='x')):
            ani[i+1] = ani[i-1]
        else:
            ani[i+1] = 1 - ani[i-1]
    if(ani[0]==0):
        if((s[0]=='o' and ani[1]!=ani[-1])or(s[0]=='x' and ani[1]==ani[-1])):
            #print('a',ani)
            return
    else:
        if((s[0]=='x' and ani[1]!=ani[-1])or(s[0]=='o' and ani[1]==ani[-1])):
            #print('a',ani)
            return
    if(ani[-1]==0):
        if((s[-1]=='o' and ani[0]!=ani[-2])or(s[-1]=='x' and ani[0]==ani[-2])):
            #print('b')
            return
    else:
        if((s[-1]=='x' and ani[0]!=ani[-2])or(s[-1]=='o' and ani[0]==ani[-2])):
            #print('b')
            return
    anss=[]
    for j in range(n):
        anss.append('S' if ani[j]==0 else 'W')
    print(''.join(anss))
    exit()

ani[0] = 0
ani[1] = 0
calc()

ani[0] = 1
ani[1] = 0
calc()

ani[0] = 0
ani[1] = 1
calc()

ani[0] = 1
ani[1] = 1
calc()

print(-1)

Submission status: Accepted

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

N = int(readline())
S = readline().rstrip()

# 0,1がSS,SW,WS,WWの4パターンで始めてN-1まで埋める。
# このとき、0が言っていることに矛盾がなければOK

# 1:S
# 0:W
ANI = "WS"
for n in range(4):
  pat = [None] * N
  pat[0] = n & 1
  pat[1] = (n >> 1) & 1
  for i in range(1, N - 1):
    if pat[i]:
      # 羊の場合
      if S[i] == "o":
        pat[i + 1] = pat[i - 1]
      else:
        pat[i + 1] = pat[i - 1] ^ 1
    else:
      # 狼の場合
      if S[i] == "o":
        pat[i + 1] = pat[i - 1] ^ 1
      else:
        pat[i + 1] = pat[i - 1]
  # 0の動物とN-1の動物が正しいことを言っているか判定する
  # 0の動物
  ok = True
  if (pat[0] == 1 and S[0] == "o") or (pat[0] == 0 and S[0] == "x"):
    if pat[N - 1] != pat[1]:
      ok = False
  elif (pat[0] == 1 and S[0] == "x") or (pat[0] == 0 and S[0] == "o"):
    if pat[N - 1] == pat[1]:
      ok = False
  if (pat[N - 1] == 1 and S[N - 1] == "o") or (pat[N - 1] == 0 and S[N - 1] == "x"):
    if pat[N - 2] != pat[0]:
      ok = False
  elif (pat[N - 1] == 1 and S[N - 1] == "x") or (pat[N - 1] == 0 and S[N - 1] == "o"):
    if pat[N - 2] == pat[0]:
      ok = False
  if ok:
    ans = ""
    for p in pat:
      ans += ANI[p]
    print(ans)
    break
else:
  print(-1)

Submission status: Accepted

Solution code:
def main():
    from itertools import product

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

    def is_ok(t):
        # 文字列tの構築に必要なsだけ使用し、後で検算にs[0]とs[-1]を用いる
        for i, c in enumerate(s[1:-1], start=1):
            if t[i]:  # S
                if c == 'o':
                    t[i + 1] = t[i - 1]
                else:
                    t[i + 1] = t[i - 1] ^ 1
            else:
                if c == 'o':
                    t[i + 1] = t[i - 1] ^ 1
                else:
                    t[i + 1] = t[i - 1]

        ret = True
        if t[0]:
            if s[0] == 'o':
                ret &= t[-1] == t[1]
            else:
                ret &= t[-1] != t[1]
        else:
            if s[0] == 'o':
                ret &= t[-1] != t[1]
            else:
                ret &= t[-1] == t[1]

        if t[-1]:
            if s[-1] == 'o':
                ret &= t[-2] == t[0]
            else:
                ret &= t[-2] != t[0]
        else:
            if s[-1] == 'o':
                ret &= t[-2] != t[0]
            else:
                ret &= t[-2] == t[0]
        return ret

    for prd in product([0, 1], repeat=2):
        # 最初の二匹を決め打ちし、以後矛盾なく構築できればよい
        t = [''] * N
        t[:2] = prd
        if is_ok(t):
            print(*map(lambda x: 'WS'[x], t), sep='')
            return

    print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

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(sys.stdin.readline().strip())
s = sys.stdin.readline().strip()

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=input()
ans=""
for f,t in [["S","S"],["S","W"],["W","S"],["W","W"]]:
  ss=f+(n-2)*"X"+t
  ss=list(ss)
  ok=True
  for i in range(n-1):
    if s[i]=="o":
      if ss[i]=="S":
        if ss[i-1]=="S":
          ss[i+1]="S"
        else:
          ss[i+1]="W"
      elif ss[i]=="W":
        if ss[i-1]=="S":
          ss[i+1]="W"
        else:
          ss[i+1]="S"
    elif s[i]=="x":
      if ss[i]=="S":
        if ss[i-1]=="S":
          ss[i+1]="W"
        else:
          ss[i+1]="S"
      elif ss[i]=="W":
        if ss[i-1]=="S":
          ss[i+1]="S"
        else:
          ss[i+1]="W"
  for i in range(n):
    if i==n-1:
      if s[i]=="o":
        if ss[i]=="S":
          if ss[i-1]==ss[0]:
            pass
          else:
            ok=False
            break
        elif ss[i]=="W":
          if ss[i-1]!=ss[0]:
            pass
          else:
            ok=False
            break
      elif s[i]=="x":
        if ss[i]=="S":
          if ss[i-1]!=ss[0]:
            pass
          else:
            ok=False
            break
        elif ss[i]=="W":
          if ss[i-1]==ss[0]:
            pass
          else:
            ok=False
            break
      continue
    if s[i]=="o":
      if ss[i]=="S":
        if ss[i-1]==ss[i+1]:
          pass
        else:
          ok=False
          break
      elif ss[i]=="W":
        if ss[i-1]!=ss[i+1]:
          pass
        else:
          ok=False
          break
    elif s[i]=="x":
      if ss[i]=="S":
        if ss[i-1]!=ss[i+1]:
          pass
        else:
          ok=False
          break
      elif ss[i]=="W":
        if ss[i-1]==ss[i+1]:
          pass
        else:
          ok=False
          break
      
  if ok:
    for si in ss:
      ans+=si
    print(ans)
    exit(0)
print(-1)

Submission status: Accepted

Solution code:
import sys
#import numpy as np
import math
from fractions import Fraction
import itertools

input=sys.stdin.readline

n=int(input())
s=input()
a=[0]*(n+1)
a[0]="S"
a[1]="S"
for i in range(1,n):
    if (a[i]=="S" and s[i]=="o") or (a[i]=="W" and s[i]=="x"):
        a[i+1]=a[i-1]
    else:
        if a[i-1]=="W":
            a[i+1]="S"
        else:
            a[i+1]="W"
if a[n]==a[0] and ((s[0]=="o" and a[1]==a[n-1] or s[0]=="x" and a[1]!=a[n-1])) :
    print("".join(a[:n]))
    exit()
a=[None]*(n+1)
a[0]="S"
a[1]="W"
for i in range(1,n):
    if (a[i]=="S" and s[i]=="o") or (a[i]=="W" and s[i]=="x"):
        a[i+1]=a[i-1]
    else:
        if a[i-1]=="W":
            a[i+1]="S"
        else:
            a[i+1]="W"
if a[n]==a[0] and ((s[0]=="o" and a[1]==a[n-1] or s[0]=="x" and a[1]!=a[n-1])):
    print("".join(a[:n]))
    exit()
    
a=[0]*(n+1)
a[0]="W"
a[1]="S"
for i in range(1,n):
    if (a[i]=="S" and s[i]=="o") or (a[i]=="W" and s[i]=="x"):
        a[i+1]=a[i-1]
    else:
        if a[i-1]=="W":
            a[i+1]="S"
        else:
            a[i+1]="W"
if a[n]==a[0] and ((s[0]=="o" and a[1]!=a[n-1] or s[0]=="x" and a[1]==a[n-1])):
    print("".join(a[:n]))
    exit()
a=[None]*(n+1)
a[0]="W"
a[1]="W"
for i in range(1,n):
    if (a[i]=="S" and s[i]=="o") or (a[i]=="W" and s[i]=="x"):
        a[i+1]=a[i-1]
    else:
        if a[i-1]=="W":
            a[i+1]="S"
        else:
            a[i+1]="W"

if a[n]==a[0] and ((s[0]=="o" and a[1]!=a[n-1] or s[0]=="x" and a[1]==a[n-1])):
    print("".join(a[:n]))
    exit()
print(-1)

Submission status: Accepted

Solution code:
import sys
# input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

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


# S-->Sと並べる
lst = [None] * (N + 2)
lst[0] = 'S'
if S[0] == 'o':
    lst[1] = 'S'
    lst[-1] = 'S'
else:
    lst[1] = 'S'
    lst[-1] = 'W'
for i in range(1, N):
    if S[i] == 'o':
        if lst[i] == 'S':
            lst[i + 1] = lst[i - 1]
        else:
            lst[i + 1] = 'S' if lst[i - 1] == 'W' else 'W'
    else: #x
        if lst[i] == 'S':
            lst[i + 1] = 'S' if lst[i - 1] == 'W' else 'W'
        else:
            lst[i + 1] = lst[i - 1]

if lst[N - 1] == lst[N + 1] and lst[0] == lst[N]:
    print (*lst[:-2], sep = '')
    # print (lst)
    exit()

#S-->Wと並べる
lst = [None] * (N + 2)
lst[0] = 'S'
if S[0] == 'o':
    lst[1] = 'W'
    lst[-1] = 'W'
else:
    lst[1] = 'W'
    lst[-1] = 'S'
for i in range(1, N):
    if S[i] == 'o':
        if lst[i] == 'S':
            lst[i + 1] = lst[i - 1]
        else:
            lst[i + 1] = 'S' if lst[i - 1] == 'W' else 'W'
    else: #x
        if lst[i] == 'S':
            lst[i + 1] = 'S' if lst[i - 1] == 'W' else 'W'
        else:
            lst[i + 1] = lst[i - 1]

if lst[N - 1] == lst[N + 1] and lst[0] == lst[N]:
    print (*lst[:-2], sep = '')
    # print (lst)
    exit()

#W-->Wと並べる
lst = [None] * (N + 2)
lst[0] = 'W'
if S[0] == 'o':
    lst[1] = 'W'
    lst[-1] = 'S'
else:
    lst[1] = 'W'
    lst[-1] = 'W'
for i in range(1, N):
    if S[i] == 'o':
        if lst[i] == 'S':
            lst[i + 1] = lst[i - 1]
        else:
            lst[i + 1] = 'S' if lst[i - 1] == 'W' else 'W'
    else: #x
        if lst[i] == 'S':
            lst[i + 1] = 'S' if lst[i - 1] == 'W' else 'W'
        else:
            lst[i + 1] = lst[i - 1]

if lst[N - 1] == lst[N + 1] and lst[0] == lst[N]:
    print (*lst[:-2], sep = '')
    # print (lst)
    exit()

#W-->Sと並べる
lst = [None] * (N + 2)
lst[0] = 'W'
if S[0] == 'o':
    lst[1] = 'S'
    lst[-1] = 'W'
else:
    lst[1] = 'S'
    lst[-1] = 'S'
for i in range(1, N):
    if S[i] == 'o':
        if lst[i] == 'S':
            lst[i + 1] = lst[i - 1]
        else:
            lst[i + 1] = 'S' if lst[i - 1] == 'W' else 'W'
    else: #x
        if lst[i] == 'S':
            lst[i + 1] = 'S' if lst[i - 1] == 'W' else 'W'
        else:
            lst[i + 1] = lst[i - 1]

if lst[N - 1] == lst[N + 1] and lst[0] == lst[N]:
    print (*lst[:-2], sep = '')
    # print (lst)
    exit()

print (-1)

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    s = input()
    # 1 == sheep, 0 == wolf
    def next(before, now, answer):
        if now == True and answer == "o":
            return before
        elif now == True and answer == "x":
            return not before
        elif now == False and answer == "o":
            return not before
        elif now == False and answer == "x":
            return before
    for i in range(2**2):
        mena = [0]*n
        for j in range(2):
            mena[j] = (i >> j) & 1
        for j in range(1, n-1):
            mena[j+1] = next(mena[j-1], mena[j], s[j])
        if (next(mena[n-2], mena[n-1], s[n-1]) == mena[0]) and (next(mena[n-1], mena[0], s[0]) == mena[1]):
            OK = True
        else:
            OK = False
        if OK:
            ans = []
            for k in range(len(mena)):
                if mena[k]:
                    ans.append("S")
                else:
                    ans.append("W")
            print("".join(ans))
            exit()
    print(-1)
resolve()

Submission status: Accepted

Solution code:

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


def check(a1, a2):
    ans = [a1, a2]
    for i, s in enumerate(S + [S[0]]):
        if i == 0:
            continue

        if ans[i] == 0:
            if s == "o":
                ans.append(ans[i - 1])
            else:
                ans.append(ans[i - 1] ^ 1)
        else:
            if s == "x":
                ans.append(ans[i - 1])
            else:
                ans.append(ans[i - 1] ^ 1)

    if ans[:2] != ans[-2:]:
        return None
    else:
        return ans[:-2]


# 0: S / 1:W
for a1 in range(2):
    for a2 in range(2):
        ans = check(a1, a2)
        if ans is not None:
            print("".join(map(lambda x: "S" if x == 0 else "W", ans)))
            exit()


print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

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

def isOk(first, second):
    prev, now = first, second
    ret = [prev]

    for s in S[1:] + S[0]:
        ret.append(now)
        isSame = (s == 'o')

        if isSame == now:
            prev, now = now, prev
        else:
            prev, now = now, not prev

    return ret if (first == prev and second == now) else []

for first in [True, False]:
    for second in [True, False]:
        ans = isOk(first, second)
        if ans:
            print(''.join(['S' if a else 'W' for a in ans[:-1]]))
            exit()

print('-1')

Submission status: Accepted

Solution code:
def check(a, b, S):
    animal = [a, b]
    for s in S[1:]:
        if s == "o" and animal[-1]:
            animal.append(animal[-2])
        elif s == "o" and not animal[-1]:
            animal.append(animal[-2] ^ 1)
        elif s == "x" and animal[-1]:
            animal.append(animal[-2] ^ 1)
        else:
            animal.append(animal[-2])

    if animal[:2] != animal[-2:]:
        return False

    ans = ""
    for x in animal[:-2]:
        if x:
            ans += "S"
        else:
            ans += "W"
    print(ans)
    return True


if __name__ == "__main__":
    N = int(input())
    S = input()
    S += S[0]
    for i in range(2):
        for j in range(2):
            if check(i, j, S):
                exit()
    print(-1)

Submission status: Accepted

Solution code:
N=int(input())
s=input()
a,b,c,d='SS','SW','WW','WS'
def f(a,N):
  for i in range(N-2):
    if a[-1]=='S' and s[i+1]=='o':
      if a[-2]=='S':
        a=a+'S'
      else:
        a=a+'W'
    elif a[-1]=='S' and s[i+1]=='x':
      if a[-2]=='S':
        a=a+'W'
      else:
        a=a+'S'
    elif a[-1]=='W' and s[i+1]=='o':
      if a[-2]=='S':
        a=a+'W'
      else:
        a=a+'S'
    else:
      if a[-2]=='S':
        a=a+'S'
      else:
        a=a+'W'
  return a

A,B,C,D=f(a,N),f(b,N),f(c,N),f(d,N)    
def g(s,A):
  a=True
  if A[-1]=='S'and s[-1]=='o':
    if A[-2]!=A[0]:
      a=False
  elif A[-1]=='S'and s[-1]=='x':
    if A[-2]==A[0]:
      a=False
  elif A[-1]=='W'and s[-1]=='o':
    if A[-2]==A[0]:
      a=False    
  elif A[-1]=='W'and s[-1]=='x':
    if A[-2]!=A[0]:
      a=False  
  
  if A[0]=='S'and s[0]=='o':
    if A[-1]!=A[1]:
      a=False
  elif A[0]=='S'and s[0]=='x':
    if A[-1]==A[1]:
      a=False
  elif A[0]=='W'and s[0]=='o':
    if A[-1]==A[1]:
      a=False    
  elif A[0]=='W'and s[0]=='x':
    if A[-1]!=A[1]:
      a=False
  return a
      
if g(s,A):
  print(A)
  exit()
if g(s,B):
  print(B)
  exit()      
if g(s,C):
  print(C)
  exit()
if g(s,D):
  print(D)
  exit() 
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
s = input() #sys.stdin.readlineは最後が改行
s=s+s[0]

ans = ['X']*(n+2)

ans[0] = 'S'
ans[1] = 'S'

for zero, one in ['SS','SW','WS','WW']:
    ans[0] = zero
    ans[1] = one
    for i in range(1,n+1):
        if (s[i] == 'o' and ans[i]=='S') or (s[i] == 'x' and ans[i]=='W'):
            ans[i+1] = ans[i-1]
        else:
            ans[i+1] = 'S' if ans[i-1]=='W' else 'W'

    if ans[0:2] == ans[n:]:
        print(*ans[:n],sep='')
        exit()

print(-1)

Submission status: Accepted

Solution code:
n,s=int(input()),input()
s+=s[:2]
# 羊:0 狼:1
for i in range(2):
    for j in range(2):
        a=[0]*(n+2)
        a[0]=i
        a[1]=j
        for k in range(1,n+1):
            if a[k]==0:
                if s[k]=="o":
                    a[k+1]=a[k-1]
                else:
                    a[k+1]=not a[k-1]
            else:
                if s[k]=="o":
                    a[k+1]=not a[k-1]
                else:
                    a[k+1]=a[k-1]
        if a[0]==a[n] and a[1]==a[n+1]:
            print(*["W" if k else "S" for k in a[:n]],sep="")
            exit()
print(-1)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
for p in range(4):
    l=[p//2,p%2]+[0]*(n-2)
    for i in range(1,n):
        l[(i+1)%n]=[l[i-1],1-l[i-1]][::(-1)**int(s[i]=="o")][l[i]]
    for i in range(n):
        if s[i]!="ox"[int(l[(i-1)%n]==l[(i+1)%n])-l[i]]:break
    else:print("".join("WS"[x] for x in l));exit()
print(-1)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
for p in range(4):
    first=p//2
    second=p%2
    l=[first,second]+[0]*(n-2)
    for i in range(1,n):
        l[(i+1)%n]=[l[i-1],1-l[i-1]][::(-1)**int(s[i]=="o")][l[i]]
    for i in range(n):
        if s[i]!="ox"[int(l[(i-1)%n]==l[(i+1)%n])-l[i]]:break
    else:
        print("".join("WS"[x] for x in l))
        exit()
print(-1)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
for p in range(4):
    first=p//2
    second=p%2
    l=[first,second]+[0]*(n-2)
    for i in range(1,n):
        if s[i]=="o":
            l[(i+1)%n]=l[i-1] if l[i] else 1-l[i-1]
        else:l[(i+1)%n]=1-l[i-1] if l[i] else l[i-1]
    d=""
    for i in range(n):
        if s[i]!="ox"[int(l[(i-1)%n]==l[(i+1)%n])-l[i]]:break
    else:
        print("".join("WS"[x] for x in l))
        exit()
print(-1)

Submission status: Accepted

Solution code:
def f(a0,a1, N,s, A):
    A[0] = a0
    A[1] = a1
    WOLF = -1
    SHEEP = 1
    for i in range(2,N):
        if s[i-1] == "o" and A[i-1] == WOLF:
            A[i] = -A[i-2]
        elif s[i-1] == "o" and A[i-1] == SHEEP:
            A[i] = A[i-2]
        elif s[i-1] == "x" and A[i-1] == SHEEP:
            A[i] = -A[i-2]
        elif s[i-1] == "x" and A[i-1] == WOLF:
            A[i] = A[i-2]
    for i in range(N):
        same = A[(i-1)%N] == A[(i+1)%N]
        if A[i] == WOLF:
            if  s[i] == "o" and same or s[i] == "x" and not same:
                return False
        if A[i] == SHEEP:
            if  s[i] == "x" and same or s[i] == "o" and not same:
                return False
    return True
                
        

def main():
    N = int(input())
    s = input()
    A = [0]*N
    if f(1,1,N,s,A):
        print("".join(["S" if A[i] == 1 else "W" for i in range(N)]))

    elif f(-1,1,N,s,A):
        print("".join(["S" if A[i] == 1 else "W" for i in range(N)]))

    elif f(1,-1,N,s,A):
        print("".join(["S" if A[i] == 1 else "W" for i in range(N)]))

    elif f(-1,-1,N,s,A):
        print("".join(["S" if A[i] == 1 else "W" for i in range(N)]))
    else:
        print(-1)


main()

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
s+=s[0]+s[1]

# 決め打ちで作ってみる。
ANS=[list("SS"+"?"*(n)),list("WS"+"?"*(n)),list("SW"+"?"*(n)),list("WW"+"?"*(n))]
for ans in ANS:
    for i in range(2, n+2):
        if (ans[i - 2] == 'S' and s[i - 1] == 'o' and ans[i - 1] == 'S') or (
                ans[i - 2] == 'W' and s[i - 1] == 'o' and ans[i - 1] == 'W') \
                or (ans[i - 2] == 'S' and ans[i - 1] == 'W' and s[i - 1] == 'x') or (
                ans[i - 2] == 'W' and ans[i - 1] == 'S' and s[i - 1] == 'x'):
            ans[i] = 'S'
        else:
            ans[i] = 'W'
    if ans[:2]==ans[-2:]:
        print(''.join(ans[:-2]))
        exit()


print(-1)

Submission status: Accepted

Solution code:
"""
sheep oXo xXx ⇨ True
wolf  oXx xXo ⇨ True 
２点決める

s[i] == "o" and a[i] == "W"  a[i+1] != a[i-1]
s[i] == "o" and a[i] == "S"  a[i+1] == a[i-1]
s[i] == "x" and a[i] == "W"  a[i+1] == a[i-1]
s[i] == "x" and a[i] == "S"  a[i+1] != a[i-1]
"""
n = int(input())
s = input()
# S -> 0, W -> 1
ds = [(0,0),(0,1),(1,0),(1,1)]

for d0,d1 in ds:
    a = [d0,d1]
    for i in range(1,n):
        if s[i] == "o" and a[i] == 0:
            a.append(a[i-1])
        elif s[i] == "x" and a[i] == 0:
            a.append(a[i-1]^1)
        elif s[i] == "o" and a[i] == 1:
            a.append(a[i-1]^1)
        else:
            a.append(a[i-1])
    # 帳尻があっているかどうか
    X = ["S","W"]
    # print(a)
    if a[0] != a[-1]:continue
    a = a[:-1]
    if a[0] == 0 and s[0] == "x":
        if a[1] != a[-1]:
            a = "".join([X[i] for i in a])
            print(a)
            exit()
    if a[0] == 0 and s[0] == "o":
        if a[1] == a[-1]:
            a = "".join([X[i] for i in a])
            print(a)
            exit()
    if a[0] == 1 and s[0] == "x":
        if a[1] == a[-1]:
            a = "".join([X[i] for i in a])
            print(a)
            exit()
    if a[-0] == 1 and s[0] == "o":
        if a[1] != a[-1]:
            a = "".join([X[i] for i in a])
            print(a)
            exit()
print(-1)

Submission status: Accepted

Solution code:



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



candidate = [("S", "S"), ("S", "W"), ("W", "S"), ("W", "W")]
for a,b in candidate:
    ans = [""] * (N+2)

    ans[0], ans[1] = a, b
    for i in range(N):
        if ans[i+1] == "S":
            if S[i] == "o":
                ans[i+2] = ans[i]
            else:
                if ans[i] == "S":
                    ans[i+2] = "W"
                else:
                    ans[i+2] = "S"
        else:
            if S[i] == "o":
                if ans[i] == "S":
                    ans[i+2] = "W"
                else:
                    ans[i+2] = "S"
            else:
                ans[i+2] = ans[i]
    
    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:
# 15:35-

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

o = "o"
x = "x"

sheep = "S"
wolf = "W"

ans = [None] * n

ans[0] = sheep 
ans[1] = sheep 
for i, c in enumerate(s[1:-1]):
  if ((c == o) == (ans[i+1] == sheep)) == (ans[i] == sheep):
    ans[i+2] = sheep
  else:
    ans[i+2] = wolf

if ((s[0] == o) == (ans[0] == sheep)) == (ans[1] == ans[-1]):
  if ((s[-1] == o) == (ans[-1] == sheep)) == (ans[0] == ans[-2]):
    print("".join(ans))
    exit()

ans[0] = sheep 
ans[1] = wolf
for i, c in enumerate(s[1:-1]):
  if ((c == o) == (ans[i+1] == sheep)) == (ans[i] == sheep):
    ans[i+2] = sheep
  else:
    ans[i+2] = wolf

if ((s[0] == o) == (ans[0] == sheep)) == (ans[1] == ans[-1]):
  if ((s[-1] == o) == (ans[-1] == sheep)) == (ans[0] == ans[-2]):
    print("".join(ans))
    exit()

ans[0] = wolf
ans[1] = sheep 
for i, c in enumerate(s[1:-1]):
  if ((c == o) == (ans[i+1] == sheep)) == (ans[i] == sheep):
    ans[i+2] = sheep
  else:
    ans[i+2] = wolf

if ((s[0] == o) == (ans[0] == sheep)) == (ans[1] == ans[-1]):
  if ((s[-1] == o) == (ans[-1] == sheep)) == (ans[0] == ans[-2]):
    print("".join(ans))
    exit()

ans[0] = wolf
ans[1] = wolf
for i, c in enumerate(s[1:-1]):
  if ((c == o) == (ans[i+1] == sheep)) == (ans[i] == sheep):
    ans[i+2] = sheep
  else:
    ans[i+2] = wolf

if ((s[0] == o) == (ans[0] == sheep)) == (ans[1] == ans[-1]):
  if ((s[-1] == o) == (ans[-1] == sheep)) == (ans[0] == ans[-2]):
    print("".join(ans))
    exit()

print(-1)