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:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7


#############
# Main Code #
#############

def rand_letter(size):
    ascii_original='ox'
    digits_original='01'

    digits='0123456789'
    ascii_lowercase='abcdefghijklmnopqrstuvwxyz'
    ascii_uppercase='ABCDEFGHIJKLMNOPQRSTUVWXYZ'

    # 好きなものを使ってね
    psuedo = ascii_original

    return ''.join([random.choice(psuedo) for i in range(size)])

N = getN()
S = input()
lista = ['SS', 'SW', 'WS', 'WW']

def judge(string):
    alta = copy.deepcopy(string)
    for i in range(1, N - 1):
        if alta[-1] == "S" and alta[-2] == "S":
            if S[i] == "o":
                alta.append('S')
            else:
                alta.append("W")
        elif alta[-1] == "S" and alta[-2] == "W":
            if S[i] == "o":
                alta.append('W')
            else:
                alta.append("S")
        elif alta[-1] == "W" and alta[-2] == "S":
            if S[i] == "o":
                alta.append('W')
            else:
                alta.append("S")
        else:
            if S[i] == "o":
                alta.append('S')
            else:
                alta.append("W")

    return alta

def judge_2(before, now, after, j):
    if now == "S":
        if j == "o":
            if before == after:
                return True

        else:
            if before != after:
                return True

    else:
        if j == "o":
            if before != after:
                return True

        else:
            if before == after:
                return True
    return False

for i in lista:
    opt = judge(list(i))
    if judge_2(opt[-2], opt[-1], opt[0], S[-1]) and judge_2(opt[-1], opt[0], opt[1], S[0]):
        print(''.join(opt))
        exit()
print(-1)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
S=[1 if S[i]=='o' else -1 for i in range(N)]

#0番目は羊のパターン
tmp=[0]*N
tmp[0]=1
if S[0]==1: #0番目の両隣は同種
  #0番目の両隣は羊
  tmp[1]=1
  tmp[N-1]=1
  
  for i in range(2, N-1):
    tmp[i]=tmp[i-1]*tmp[i-2]*S[i-1]
    
  if tmp[N-1]==tmp[N-2]*tmp[N-3]*S[N-2] and tmp[0]==tmp[N-1]*tmp[N-2]*S[N-1]:
    ans=['S' if tmp[i]==1 else 'W' for i in range(N)]
    print(''.join(ans))
    exit()
    
  #0番目の両隣は狼
  tmp[1]=-1
  tmp[N-1]=-1
  
  for i in range(2, N-1):
    tmp[i]=tmp[i-1]*tmp[i-2]*S[i-1]
    
  if tmp[N-1]==tmp[N-2]*tmp[N-3]*S[N-2] and tmp[0]==tmp[N-1]*tmp[N-2]*S[N-1]:
    ans=['S' if tmp[i]==1 else 'W' for i in range(N)]
    print(''.join(ans))
    exit()
  
else: #0番目の両隣は異種
  #1番目が羊, N-1番目が狼
  tmp[1]=1
  tmp[N-1]=-1
  
  for i in range(2, N-1):
    tmp[i]=tmp[i-1]*tmp[i-2]*S[i-1]
    
  if tmp[N-1]==tmp[N-2]*tmp[N-3]*S[N-2] and tmp[0]==tmp[N-1]*tmp[N-2]*S[N-1]:
    ans=['S' if tmp[i]==1 else 'W' for i in range(N)]
    print(''.join(ans))
    exit()
  
  #1番目が狼, N-1番目が羊
  tmp[1]=-1
  tmp[N-1]=1
  
  for i in range(2, N-1):
    tmp[i]=tmp[i-1]*tmp[i-2]*S[i-1]
    
  if tmp[N-1]==tmp[N-2]*tmp[N-3]*S[N-2] and tmp[0]==tmp[N-1]*tmp[N-2]*S[N-1]:
    ans=['S' if tmp[i]==1 else 'W' for i in range(N)]
    print(''.join(ans))
    exit()
  
#0番目は狼のパターン
tmp=[0]*N
tmp[0]=-1
if S[0]==-1: #0番目の両隣は同種
  #0番目の両隣は羊
  tmp[1]=1
  tmp[N-1]=1
  
  for i in range(2, N-1):
    tmp[i]=tmp[i-1]*tmp[i-2]*S[i-1]
    
  if tmp[N-1]==tmp[N-2]*tmp[N-3]*S[N-2] and tmp[0]==tmp[N-1]*tmp[N-2]*S[N-1]:
    ans=['S' if tmp[i]==1 else 'W' for i in range(N)]
    print(''.join(ans))
    exit()
    
  #0番目の両隣は狼
  tmp[1]=-1
  tmp[N-1]=-1
  
  for i in range(2, N-1):
    tmp[i]=tmp[i-1]*tmp[i-2]*S[i-1]
    
  if tmp[N-1]==tmp[N-2]*tmp[N-3]*S[N-2] and tmp[0]==tmp[N-1]*tmp[N-2]*S[N-1]:
    ans=['S' if tmp[i]==1 else 'W' for i in range(N)]
    print(''.join(ans))
    exit()
  
else: #0番目の両隣は異種
  #1番目が羊, N-1番目が狼
  tmp[1]=1
  tmp[N-1]=-1
  
  for i in range(2, N-1):
    tmp[i]=tmp[i-1]*tmp[i-2]*S[i-1]
    
  if tmp[N-1]==tmp[N-2]*tmp[N-3]*S[N-2] and tmp[0]==tmp[N-1]*tmp[N-2]*S[N-1]:
    ans=['S' if tmp[i]==1 else 'W' for i in range(N)]
    print(''.join(ans))
    exit()
  
  #1番目が狼, N-1番目が羊
  tmp[1]=-1
  tmp[N-1]=1
  
  for i in range(2, N-1):
    tmp[i]=tmp[i-1]*tmp[i-2]*S[i-1]
    
  if tmp[N-1]==tmp[N-2]*tmp[N-3]*S[N-2] and tmp[0]==tmp[N-1]*tmp[N-2]*S[N-1]:
    ans=['S' if tmp[i]==1 else 'W' for i in range(N)]
    print(''.join(ans))
    exit()
  
print(-1)

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr

#####MakeDivisors######
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)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b: a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N = I()
S = S()

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

flag = 0
for prev,now in cases:
  prev_0 = prev
  ans = [now]
  for s in S[:-1]:
    if (now == 1 and s == "o" ) or (now == 0 and s == "x"):
      ans.append(prev)
    else:
      ans.append(1-prev)
    prev = now
    now = ans[-1]
  if ans[-1] == prev_0 == 1:
    if (S[-1] == "o" and ans[0]==ans[-2]) or (S[-1] == "x" and ans[0]!=ans[-2]):
      flag = 1
      break
  if ans[-1] == prev_0 == 0:
    if (S[-1] == "o" and ans[0]!=ans[-2]) or (S[-1] == "x" and ans[0]==ans[-2]):
      flag = 1
      break
if flag:
  print(*["S" if a else "W" for a in ans],sep="")
else:
  print(-1)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input()
    for t in ["SS", "SW", "WS", "WW"]:
        for i in s[1:-1]:
            if t[-1] == "S":
                if i == "o":
                    t += t[-2]
                else:
                    t += "S" if t[-2] == "W" else "W"
            else:
                if i == "x":
                    t += t[-2]
                else:
                    t += "S" if t[-2] == "W" else "W"
        # check1
        if t[0] == "S":
            if s[0] == "o":
                f = t[-1] == t[1]
            else:
                f = t[-1] != t[1]
        else:
            if s[0] == "x":
                f = t[-1] == t[1]
            else:
                f = t[-1] != t[1]

        # check2
        if t[-1] == "S":
            if s[-1] == "o":
                g = t[-2] == t[0]
            else:
                g = t[-2] != t[0]
        else:
            if s[-1] == "x":
                g = t[-2] == t[0]
            else:
                g = t[-2] != t[0]

        if f and g:
            print(t)
            break

    else:
        print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

    def get_next(p_prev, prev, prev_mark):
        anms = ["S", "W"]
        p_prev_idx = anms.index(p_prev)
        if prev_mark == "o":
            return anms[p_prev_idx] if prev== "S" else anms[(p_prev_idx+1)%2]
        else:
            return anms[(p_prev_idx+1)%2] if prev == "S" else anms[p_prev_idx]

    out = ["S", "S"]
    for i in range(2, N):
        out.append(get_next(out[i-2], out[i-1], S[i-1]))
    if get_next(out[-2], out[-1], S[-1]) == out[0] and get_next(out[-1], out[0], S[0]) == out[1]:
        print("".join(out))
        return

    out = ["S", "W"]
    for i in range(2, N):
        out.append(get_next(out[i-2], out[i-1], S[i-1]))
    if get_next(out[-2], out[-1], S[-1]) == out[0] and get_next(out[-1], out[0], S[0]) == out[1]:
        print("".join(out))
        return
    
    out = ["W", "S"]
    for i in range(2, N):
        out.append(get_next(out[i-2], out[i-1], S[i-1]))
    if get_next(out[-2], out[-1], S[-1]) == out[0] and get_next(out[-1], out[0], S[0]) == out[1]:
        print("".join(out))
        return
    
    out = ["W", "W"]
    for i in range(2, N):
        out.append(get_next(out[i-2], out[i-1], S[i-1]))
    if get_next(out[-2], out[-1], S[-1]) == out[0] and get_next(out[-1], out[0], S[0]) == out[1]:
        print("".join(out))
        return
    
    print(-1)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

# assume [0 is S, -1 is S]

for ans0, ans_last in [ ('S','S'),('S','W'),('W','S'),('W','W') ]:
    ans = ['tbd']*n
    ans[0] = ans0
    ans[-1] = ans_last
        
    for i in range(n-2):
        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'

    last_two_flag = False
    last_one_flag = False

    if (s[n-2] == 'o' and ans[n-2] == 'S') or (s[n-2] == 'x' and ans[n-2] == 'W'):
        if ans[n-3] == ans[n-1]:
            last_two_flag = True
    else:
        if ans[n-3] != ans[n-1]:
            last_two_flag = True


    if (s[n-1] == 'o' and ans[n-1] == 'S') or (s[n-1] == 'x' and ans[n-1] == 'W'):
        if ans[n-2] == ans[0]:
            last_one_flag = True
    else:
        if ans[n-2] != ans[0]:
            last_one_flag = True

    if last_one_flag and last_two_flag:
        print(''.join(ans))
        exit()

print(-1)

Submission status: Accepted

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

for p1,p2 in [(True,True),(True,False),(False,True),(False,False)]:
  ans = []
  ans.extend([p1,p2])  
  for i in range(1,n-1):
    if ans[i] == True:
      if s[i]=='o':
        ans.append(ans[-2])
      else:
        ans.append(not ans[-2])
    else:
      if s[i]=='o':
        ans.append(not ans[-2])
      else:
        ans.append(ans[-2])
  
  f = False
  if ans[-1]==True:
    if s[-1]=='o' and ans[-2]==ans[0]:        
      f = True
    if s[-1]=='x' and ans[-2]!=ans[0]:      
      f = True
  else:
    if s[-1]=='o' and ans[-2]!=ans[0]:
      f = True
    if s[-1]=='x' and ans[-2]==ans[0]:
      f = True
      
  if f:    
    if ans[0] and s[0]=='o' and ans[1] == ans[-1]:
      break
    if ans[0] and s[0]=='x' and ans[1] != ans[-1]:
      break
    if not ans[0] and s[0]=='o' and ans[1] != ans[-1]:
      break
    if not ans[0] and s[0]=='x' and ans[1] == ans[-1]:
      break
  ans = []
  
if len(ans)>0:
  print(''.join(['S' if a else 'W' for a in ans] ))
else:
  print(-1)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
def func(a,b):
  ans=[a,b] #True:S, False:W
  for i in range(1,n):
    if s[i]=='o':
      if ans[-1]:
        ans.append(ans[-2])
      else:
        ans.append(not ans[-2])
    else:
      if ans[-1]:
        ans.append(not ans[-2])
      else:
        ans.append(ans[-2])
  if ans[0]==ans[-1]:
    flg=False
    if s[0]=='o' and a and ans[1]==ans[-2]:
      flg=True
    elif s[0]=='o' and not a and ans[1]!=ans[-2]:
      flg=True
    elif s[0]=='x' and a and ans[1]!=ans[-2]:
      flg=True
    elif s[0]=='x' and not a and ans[1]==ans[-2]:
      flg=True
    if flg:
      print(''.join(['S' if ansi else 'W' for ansi in ans[:-1]]))
      exit()
func(True,True)
func(True,False)
func(False,True)
func(False,False)
print(-1)

Submission status: Accepted

Solution code:
#!python3

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

d = {"S": "W", "W": "S"}


def f(x, y, i, j):
    if y == "S":
        if s[j] == "o":
            return x
        else:
            return d[x]
    else:
        if s[j] == "o":
            return d[x]
        else:
            return x


def solve(t):
    l = [None] * N
    l[0], l[1] = t
    l[-1] = f(l[1], l[0], 1, 0)
    for i in range(2, N - 1):
        l[i] = f(l[i - 2], l[i - 1], i - 2, i - 1)
    x = f(l[-3], l[-2], -3, -2)
    y = f(l[-2], l[-1], -2, -1)
    if x == l[-1] and y == l[0]:
        return l
    else:
        return None
        

def main():
    l = [("S", "S"), ("S", "W"), ("W", "S"), ("W", "W")]
    ans = None
    for t in l:
        ans = solve(t)
        if ans is not None:
            break
    if ans is None:
        print(-1)
    else:
        print("".join(ans))
    

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Ans = [[0] * N for _ in range(4)]
Ans[0][0] = 1
Ans[0][1] = 1
Ans[1][0] = 1
Ans[1][1] = 2
Ans[2][0] = 2
Ans[2][1] = 1
Ans[3][0] = 2
Ans[3][1] = 2

def isok(a):
    for i, s in enumerate(S):
        if i + 2 < N:
            if s == 'o':
                if a[i + 1] == 1:
                    a[i + 2] = a[i]
                else:
                    a[i + 2] = 3 - a[i]
            else:
                if a[i + 1] == 1:
                    a[i + 2] = 3 - a[i]
                else:
                    a[i + 2] = a[i]
        else:
            if s == 'o':
                if a[(i + 1) % N] == 1:
                    if a[(i + 2) % N] != a[i]:
                        return False
                else:
                    if a[(i + 2) % N] + a[i] != 3:
                        return False
            else:
                if a[(i + 1) % N] == 1:
                    if a[(i + 2) % N] + a[i] != 3:
                        return False
                else:
                    if a[(i + 2) % N] != a[i]:
                        return False
    return True

flag = True
num = -1
for i in range(4):
    if isok(Ans[i]):
        num = i
        flag = False
        break
if flag:
    print(-1)
else:
    for a in Ans[num][1:]:
        if a == 1:
            print('S', end='')
        else:
            print('W',end='')
    if Ans[num][0] == 1:
        print('S')
    else:
        print('W')

Submission status: Accepted

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

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

for x in kouho:
    ans = copy.deepcopy(x)
    for i in range(1,n+1):
        if (s[i]=='o' and ans[i]=='S') or (s[i]=='x' and ans[i]=='W'):
            if ans[i-1]=='S':
                ans+= 'S'
            else:
                ans+='W'
        else:
            if ans[i-1]=='S':
                ans+='W'
            else:
                ans+='S'
    #円環成してるので正しければ頭二個と後ろ2個は同じになるはず
    if ans[-2:]==x:
       print(ans[:n])
       exit()
print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
n = int(input())
s = input()
t = [''] * n
for p in itertools.product(['o', 'x'], repeat=2):
  t[-1] = p[0]
  t[0] = p[1]
  for i in range(1, n - 1):
    if (t[i - 2] + t[i - 1] + s[i - 1]).count('x') % 2 == 0:
      t[i] = 'o'
    else:
      t[i] = 'x'
  if (t[-3] + t[-2] + t[-1] + s[-2]).count('x') % 2 == 0 and (t[-2] + t[-1] + t[0] + s[-1]).count('x') % 2 == 0:
    ans = []
    for c in t:
      if c == 'o':
        ans.append('S')
      else:
        ans.append('W')
    print(*ans, sep='')
    break
else:
  print(-1)

Submission status: Accepted

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

def check(s, i, j, k):
    if j and s == "o" and i != k:
        return True
    elif j and s == "x" and i == k:
        return True
    elif not j and s == "o" and i == k:
        return True
    elif not j and s == "x" and i != k:
        return True
    return False

def func(f, s):
    l = [-1]*N
    l[0] = f
    l[1] = s
    for i in range(1, len(l) - 1):
        if l[i] and S[i] == "o" or not l[i] and S[i] == "x":
            l[i + 1] = l[i - 1] ^ 1
        else:
            l[i + 1] = l[i - 1]
    if check(S[-1], l[-2], l[-1], l[0]):
        if check(S[0], l[-1], l[0], l[1]):
            return l
    return False

for i, j in [(0, 0), (0, 1), (1, 1), (1, 0)]:
    l = func(i, j)
    if l:
        for v in l:
            print("SW"[v], end="")
        print()
        exit()
print(-1)

Submission status: Accepted

Solution code:
def next_animal(before, current, claim):
    if (before == current and claim == 'x') or \
        (before != current and claim == 'o'):
        return 'W'
    else:
        return 'S'


def search(S):
    assert len(S) > 2
    init_twos = ['SS', 'SW', 'WS', 'WW']
    ret = ''
    for init_two in init_twos:
        animals = init_two
        for i in range(1, len(S) - 1):
            a = next_animal(animals[i - 1], animals[i], S[i])
            animals += a
        # check: claim of first and last animals
        if animals[1] != next_animal(animals[-1], animals[0], S[0]):
            continue
        if animals[0] != next_animal(animals[-2], animals[-1], S[-1]):
            continue
        ret = animals
        break
    return ret


def main():
    N = int(input())
    S = input()
    ret = search(S)
    if len(ret) > 0:
        print(ret)
    else:
        print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------

def check(a0, a1):
    b0,b1 = a0,a1
    Ans = a1
    for i in range(N):
        # 羊
        if a1 == "S":
            # 両隣が同じ
            if S[i] == "o":
                a2 = a0
            else:
                a2 = B[a0]
        # 狼
        else:
            # 両隣が異なる
            if S[i] == "o":
                a2 = B[a0]
            else:
                a2 = a0
        Ans += a2
        a0,a1 = a1,a2
    if Ans[0] == Ans[N] and Ans[0] == b1 and Ans[N-1] == b0:
        return Ans[0:N]
    else:
        return -1

# -------------------------------------------------------------
# main
# -------------------------------------------------------------
N = int(input())
S = input().rstrip()

B = {"S":"W", "W":"S"}
S0S1 = [("S","S"), ("S","W"), ("W","S"), ("W","W")]

for s0,s1 in S0S1:
    Ans = check(s0,s1)
    if Ans != -1:
        print(Ans)
        sys.exit()

print(-1)

Submission status: Accepted

Solution code:
import bisect
import collections
import copy
import functools
import heapq
import math
import sys
from collections import deque
from collections import defaultdict
input = sys.stdin.readline
MOD = 10**9+7

N = int(input())
s = list(input().rstrip())
s.append(s[0])
a = [-1]*(N+2) # 0==N 1==N+1

def d(self,f,ans):
    if ans == "o":
        return (self + f)%2
    else:
        return (self + f +1)%2

flag = 0
for p in [(0,0),(0,1),(1,0),(1,1)]:
    a[0],a[1] = p
    for i in range(1,N+1):
        a[i+1] = d(a[i],a[i-1],s[i])
    if a[0] == a[N] and a[1] == a[N+1]:
        break
else:
    flag = -1

if flag == -1:
    print(-1)
else:
    ans = [None]*N
    for i in range(N):
        if a[i] == 0:
            ans[i] = "S"
        else:
            ans[i] = "W"
    output = "".join(ans)
    print(output)

Submission status: Accepted

Solution code:
from itertools import product

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

swap = {'S': 'W', 'W': 'S'}
for a, b in product('SW', repeat=2):
    t = [None] * N
    t[0] = a
    t[1] = b
    for i in range(1, N - 1):
        if t[i] == 'S':
            if s[i] == 'o':
                t[i + 1] = t[i - 1]
            elif s[i] == 'x':
                t[i + 1] = swap[t[i - 1]]
        elif t[i] == 'W':
            if s[i] == 'o':
                t[i + 1] = swap[t[i - 1]]
            elif s[i] == 'x':
                t[i + 1] = t[i - 1]
    if t[N - 1] == 'S':
        if s[N - 1] == 'o' and t[N - 2] != t[0]:
            continue
        elif s[N - 1] == 'x' and t[N - 2] == t[0]:
            continue
    elif t[N - 1] == 'W':
        if s[N - 1] == 'o' and t[N - 2] == t[0]:
            continue
        elif s[N - 1] == 'x' and t[N - 2] != t[0]:
            continue
    if t[0] == 'S':
        if s[0] == 'o' and t[N - 1] != t[1]:
            continue
        elif s[0] == 'x' and t[N - 1] == t[1]:
            continue
    elif t[0] == 'W':
        if s[0] == 'o' and t[N - 1] == t[1]:
            continue
        elif s[0] == 'x' and t[N - 1] != t[1]:
            continue
    print(''.join(t))
    exit()
print(-1)

Submission status: Accepted

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

for j in pat:
    now = j
    flag = True
    for i in range(1,n+1):
        if (now[i] == "S" and s[i] == "o") or (now[i] == "W" and s[i] == "x"):
            now += now[i-1]
        else:
            if now[i-1] == "S":
                now += "W"
            else:
                now += "S"
    if now[0] == now[-2] and now[1] == now[-1]:
        print(now[:-2])
        exit()

print(-1)

Submission status: Accepted

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

for j in pat:
    now = j
    flag = True
    for i in range(1,n+1):
        if (now[i] == "S" and s[i] == "o") or (now[i] == "W" and s[i] == "x"):
            now += now[i-1]
        else:
            if now[i-1] == "S":
                now += "W"
            else:
                now += "S"
    if now[0] == now[-2] and now[1] == now[-1]:
        print(now[:-2])
        exit()

print(-1)