Problem:


Score : 500 points

Problem Statement

Snuke, who loves animals, built a zoo.

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

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

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

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

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

Constraints

3 ≤ N ≤ 10^{5}

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

Input

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

N
s

Output

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

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

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

Sample Input 1

6
ooxoox

Sample Output 1

SSSWWS

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

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

Sample Input 2

3
oox

Sample Output 2

-1

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

Sample Input 3

10
oxooxoxoox

Sample Output 3

SSWWSSSWWS



Sample input:
6
ooxoox


Sample output:
SSSWWS

Submission status: Accepted

Solution code:
import sys
N = int(input())
s = input()
def verify(s, tmpAns):
    global N
    for i in range(N):
        if (s[i] == 'o' and tmpAns[i] == 'S') \
                or (s[i] == 'x' and tmpAns[i] == 'W'):
                    if tmpAns[(i+1)%N] != tmpAns[(i-1+N)%N]:
                        return False
        elif (s[i] == 'x' and tmpAns[i] == 'S') \
                or (s[i] == 'o' and tmpAns[i] == 'W'):
                    if tmpAns[(i+1)%N] == tmpAns[(i-1+N)%N]:
                        return False
    return True

for i in range(4):
    a = 'S' if i & 1 else 'W'
    b = 'S' if i & 2 else 'W'
    tmpAns = a + b
    for j in range(N-2):
        c = s[j+1]
        a, b = tmpAns[j], tmpAns[j+1]
        # print('a: %s, b: %s, c: %s' % (a, b, c))
        if c == 'o' and b == 'W':
            tmpAns += 'W' if a == 'S' else 'S'
        elif c == 'o' and b == 'S':
            tmpAns += 'S' if a == 'S' else 'W'
        elif c == 'x' and b == 'W':
            tmpAns += 'S' if a == 'S' else 'W'
        elif c == 'x' and b == 'S':
            tmpAns += 'W' if a == 'S' else 'S'
    # print(tmpAns)
    if verify(s, tmpAns):
        print(tmpAns)
        sys.exit(0)
print(-1)

Submission status: Accepted

Solution code:
def addleave(p):
    for i in range(1, n-1):
        if s[i]=="o":
            if p[i]=="S":
                p += "S" if p[i-1]=="S" else "W"
            else:
                p += "W" if p[i-1]=="S" else "S"
        else:
            if p[i]=="S":
                p += "W" if p[i-1]=="S" else "S"
            else:
                p += "S" if p[i-1]=="S" else "W"
    return p
def solve():
    result = addleave("SS")
    if check(result):
        return result
    result = addleave("SW")
    if check(result):
        return result
    result = addleave("WS")
    if check(result):
        return result
    result = addleave("WW")
    if check(result):
        return result
    return -1

def check(res):
    flag = True
    for i in range(n):
        if res[(i + n - 1) % n] == res[(i + n + 1) % n] and res[i]=="S" and s[i]=="o":
            pass
        elif res[(i + n - 1) % n] != res[(i + n + 1) % n] and res[i]=="S" and s[i]=="x":
            pass
        elif res[(i + n - 1) % n] == res[(i + n + 1) % n] and res[i]=="W" and s[i]=="x":
            pass
        elif res[(i + n - 1) % n] != res[(i + n + 1) % n] and res[i]=="W" and s[i]=="o":
            pass
        else:
            flag = False
    return flag
if __name__=="__main__":
    n = int(input())
    s = input()
    print(solve())

Submission status: Accepted

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

def addleave(p):
    for i in range(1, n-1):
        if s[i]=="o":
            if p[i]=="S":
                p += "S" if p[i-1]=="S" else "W"
            else:
                p += "W" if p[i-1]=="S" else "S"
        else:
            if p[i]=="S":
                p += "W" if p[i-1]=="S" else "S"
            else:
                p += "S" if p[i-1]=="S" else "W"
    return p
def solve():
    result = addleave("SS")
    if check(result):
        return result
    result = addleave("SW")
    if check(result):
        return result
    result = addleave("WS")
    if check(result):
        return result
    result = addleave("WW")
    if check(result):
        return result
    return -1

def check(res):
    res = res + res + res
    flag = True
    for i in range(n):
        if res[n + i - 1] == res[n + i + 1] and res[n + i]=="S" and s[i]=="o":
            pass
        elif res[n + i - 1] != res[n + i + 1] and res[n + i]=="S" and s[i]=="x":
            pass
        elif res[n + i - 1] == res[n + i + 1] and res[n + i]=="W" and s[i]=="x":
            pass
        elif res[n + i - 1] != res[n + i + 1] and res[n + i]=="W" and s[i]=="o":
            pass
        else:
            flag = False
    return flag
print(solve())

Submission status: Accepted

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

def addleave(p):
    for i in range(1,n):
        if s[i]=="o":
            if p[i]=="S":
                p += "S" if p[i-1]=="S" else "W"
            else:
                p += "W" if p[i-1]=="S" else "S"
        else:
            if p[i]=="S":
                p += "W" if p[i-1]=="S" else "S"
            else:
                p += "S" if p[i-1]=="S" else "W"
    return p
def solve():
    result = addleave("SS")
    if result[-1] == result[0]:
        if check(result):
            return result[:-1]
    result = addleave("SW")
    if result[-1] == result[0]:
        if check(result):
            return result[:-1]
    result = addleave("WS")
    if result[-1] == result[0]:
        if check(result):
            return result[:-1]

    result = addleave("WW")
    if result[-1] == result[0]:
        if check(result):
            return result[:-1]
    return -1

def check(res):
    res = res[:-1] + res[:-1] + res[:-1]
    flag = True
    for i in range(n):
        if res[n + i - 1] == res[n + i + 1] and res[n + i]=="S" and s[i]=="o":
            pass
        elif res[n + i - 1] != res[n + i + 1] and res[n + i]=="S" and s[i]=="x":
            pass
        elif res[n + i - 1] == res[n + i + 1] and res[n + i]=="W" and s[i]=="x":
            pass
        elif res[n + i - 1] != res[n + i + 1] and res[n + i]=="W" and s[i]=="o":
            pass
        else:
            flag = False
    return flag
print(solve())

Submission status: Accepted

Solution code:
def judge():
    for i in range(1, n):
        before = list[i-1]
        l = s[i]
        if l == 'o':
            if list[i] == 'S':
                if before == 'S':
                    list.append('S')
                else:
                    list.append('W')
            else:
                if before == 'S':
                    list.append('W')
                else:
                    list.append('S')
        else:
            if list[i] == 'S':
                if before == 'S':
                    list.append('W')
                else:
                    list.append('S')
            else:
                if before == 'S':
                    list.append('S')
                else:
                    list.append('W')
    if list[0] == list[n]:
        if list[0] == 'S':
            if s[0] == 'o' and list[1] == list[n-1] or s[0] == 'x' and list[1] != list[n-1]:
                list.pop()
                print("".join(list))
                exit()
        elif list[0] == 'W':
            if s[0] == 'x' and list[1] == list[n-1] or s[0] == 'o' and list[1] != list[n-1]:
                list.pop()
                print("".join(list))
                exit()

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

list = ['S', 'S']
judge()
list.clear()
list = ['S', 'W']
judge()
list.clear()
list = ['W', 'S']
judge()
list.clear()
list = ['W', 'W']
judge()
list.clear()
print('-1')

Submission status: Accepted

Solution code:
N = int(input())
s = input()
S = ['SSS', 'SSW', 'SWW', 'WWW', 'WWS', 'WSS', 'SWS', 'WSW']
ans = []
for n in range(4):
    ans = []
    if len(s) == 3:
        print(-1)
        exit()
    elif s[1] == 'o' and n == 0:
        ans.append('S')
        ans.append('S')
    elif s[1] == 'x' and n == 0:
        ans.append('W')
        ans.append('S')
    elif s[1] == 'o' and n == 1:
        ans.append('S')
        ans.append('W')
    elif s[1] == 'x' and n == 1:
        ans.append('S')
        ans.append('W')
    elif s[1] == 'o' and n == 2:
        ans.append('W')
        ans.append('S')
    elif s[1] == 'x' and n == 2:
        ans.append('W')
        ans.append('W')
    elif s[1] == 'o' and n == 3:
        ans.append('W')
        ans.append('W')
    elif s[1] == 'x' and n == 3:
        ans.append('S')
        ans.append('S')

    for i in range(1, N):
        if ans[i - 1] == 'S':
            if ans[i] == 'S':
                if s[i] == 'o':
                    ans.append('S')
                else:
                    ans.append('W')
            else:
                if s[i] == 'o':
                    ans.append('W')
                else:
                    ans.append('S')
        else:
            if ans[i] == 'S':
                if s[i] == 'o':
                    ans.append('W')
                else:
                    ans.append('S')
            else:
                if s[i] == 'o':
                    ans.append('S')
                else:
                    ans.append('W')
    if ans[N-1] == 'S' and ans[N] == ans[0]:
        if ans[0] == 'S':
            if s[0] == 'o' and ans[1] == 'S':
                break
            elif s[0] == 'x' and ans[1] == 'W':
                break
        else:
            if s[0] == 'o' and ans[1] == 'W':
                break
            elif s[0] == 'x' and ans[1] == 'S':
                break
    elif ans[N-1] == 'W' and ans[N] == ans[0]:
        if ans[0] == 'S':
            if s[0] == 'o' and ans[1] == 'W':
                break
            elif s[0] == 'x' and ans[1] == 'S':
                break
        else:
            if s[0] == 'o' and ans[1] == 'S':
                break
            elif s[0] == 'x' and ans[1] == 'W':
                break
    if n == 3:
        print(-1)
        exit()
ans.pop()
ans = ''.join(ans)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

s = input()

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

cand_oo = ["SSSS", "SWWS", "WWSW", "WSWW"]
cand_ox = ["SSSW", "SWWW", "WWSS", "SSWS"]
cand_xo = ["WSSS", "WWWS", "SWSW", "SSWW"]
cand_xx = ["WSSW", "WWWW", "SWSS", "WSWS"]

answered = False

for a in ans_first:
    if answered == True:
        break
        
        
    for i in range(1, N-1):
        if (s[i] == "o" and a[i] == "S") or (s[i] == "x" and a[i] == "W"):
            a = a + a[i-1]
            
        elif (s[i] == "o" and a[i] == "W") or (s[i] == "x" and a[i] == "S") :
            if a[i-1] == "S":
                a = a + "W"
            else:
                a = a + "S"
            
    if s[-1] + s[0] == "oo": 
        for can in cand_oo:
            if can == a[-2:] + a[:2]:
                print(a)
                answered = True
                break
                
    elif s[-1] + s[0] == "ox": 
        for can in cand_ox:
            if can == a[-2:] + a[:2]:
                print(a)
                answered = True
                break
    
    elif s[-1] + s[0] == "xo": 
        for can in cand_xo:
            if can == a[-2:] + a[:2]:
                print(a)
                answered = True
                break
    
    elif s[-1] + s[0] == "xx": 
        for can in cand_xx:
            if can == a[-2:] + a[:2]:
                print(a)
                answered = True
                break
                
if answered == False:
    print(-1)

Submission status: Accepted

Solution code:
from collections import defaultdict


def ok(l, s):
    for i in range(len(s) * 2):
        now = i % len(l)
        pre = now - 1
        nex = (now + 1) % len(l)
        if (l[now] == -1 and s[now] == 'x') or (l[now] == 1 and s[now] == 'o'):
            if l[nex] is None:
                l[nex] = l[pre]
            else:
                if l[nex] != l[pre]:
                    return False
        else:
            if l[nex] is None:
                l[nex] = -1 * l[pre]
            else:
                if l[nex] != -1 * l[pre]:
                    return False

    return True


def main():
    N = int(input())
    s = input()

    for x in [(-1, -1), (-1, 1), (1, -1), (1, 1)]:
        l = [None] * N
        l[-1], l[0] = x[0], x[1]
        if ok(l, s):
            print("".join(['W' if x == -1 else 'S' for x in l]))
            return
    print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

sl = list(s)

def calc(i, resl):
  now = resl[i]
  back = resl[i-1]

  if now == 'W':
    if back == 'W':
      if sl[i] == 'o':
        resl[i+1] = 'S'
      else:
        resl[i+1] = 'W'
    else:
      if sl[i] == 'o':
        resl[i+1] = 'W'
      else:
        resl[i+1] = 'S'
  else: # 'S'
    if back == 'W':
      if sl[i] == 'o':
        resl[i+1] = 'W'
      else:
        resl[i+1] = 'S'
    else:
      if sl[i] == 'o':
        resl[i+1] = 'S'
      else:
        resl[i+1] = 'W'

def test2(resl):
  i = 0
  now = resl[0]
  back = resl[-1]
  
  if now == 'W':
    if back == 'W':
      if sl[i] == 'o':
        return resl[1] == 'S' and test2
      else:
        return resl[1] == 'W' and test2
    else:
      if sl[i] == 'o':
        return resl[1] == 'W' and test2
      else:
        return resl[1] == 'S' and test2
  else: # 'S'
    if back == 'W':
      if sl[i] == 'o':
        return resl[1] == 'W' and test2
      else:
        return resl[1] == 'S' and test2
    else:
      if sl[i] == 'o':
        return resl[1] == 'S' and test2
      else:
        return resl[1] == 'W' and test2


def test(resl):
  i = N-1
  now = resl[-1]
  back = resl[-2]
  
  if now == 'W':
    if back == 'W':
      if sl[i] == 'o':
        return resl[0] == 'S' and test2(resl)
      else:
        return resl[0] == 'W' and test2(resl)
    else:
      if sl[i] == 'o':
        return resl[0] == 'W' and test2(resl)
      else:
        return resl[0] == 'S' and test2(resl)
  else: # 'S'
    if back == 'W':
      if sl[i] == 'o':
        return resl[0] == 'W' and test2(resl)
      else:
        return resl[0] == 'S' and test2(resl)
    else:
      if sl[i] == 'o':
        return resl[0] == 'S' and test2(resl)
      else:
        return resl[0] == 'W' and test2(resl)


def printer(resl):
  print("".join(resl))

def check(resl):
  for i in range(1, N-1):
    calc(i, resl)
  return test(resl)

resl = [""]*N
(resl[0], resl[1]) = ('W', 'W')
if check(resl):
  printer(resl)
else:
  resl = [""]*N
  (resl[0], resl[1]) = ('W', 'S')
  if check(resl):
    printer(resl)
  else:
    resl = [""]*N
    (resl[0], resl[1]) = ('S', 'W')
    if check(resl):
      printer(resl)
    else:
      resl = [""]*N
      (resl[0], resl[1]) = ('S', 'S')
      if check(resl):
        printer(resl)
      else:
        print(-1)

Submission status: Accepted

Solution code:
import sys

# sys.stdin = open('d1.in')


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


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


def solve(n, r):
    for a0 in range(2):
        for a1 in range(2):
            a = [a0, a1] + [-1] * (n - 2)
            for i in range(2, n):
                if r[i - 1] == 'o':
                    t = a[i - 2]
                else:
                    t = 1 - a[i - 2]
                if a[i - 1] == 1:
                    t = 1 - t
                a[i] = t

            ok = True
            for i in range(-1, n - 1):
                if a[i - 1] != a[i + 1]:
                    s = 1
                else:
                    s = 0
                if a[i] == 1:
                    s = 1 - s
                if s == 1:
                    expected = 'x'
                else:
                    expected = 'o'
                ok &= r[i] == expected
            if ok:
                l = map(lambda j: 'SW'[j], a)
                res = ''.join(l)
                return res
    return -1


def main():
    n = read_int()
    r = read_str()
    res = solve(n, r)
    print(res)


main()

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
for i in range(len(s)):
    if s[i] == 'o':
        s[i] = True
    else:
        s[i] = False

state = [None]*n
state[0] = 1
state[1] = 1


def next_predict(before_state,current_state,output):#i+1の動物を出力する
    if current_state == 1:
        if output == True:
            if before_state == 1:
                return 1
            else:
                return 0
        else:
            if before_state == 1:
                return 0
            else:
                return 1
    else:
        return next_predict(before_state,1,not(output))

isFound = -1
for i in range(4):
    for j in range(1,n-1):
        state[j+1] = next_predict(state[j-1],state[j],s[j])
    tmp0 = next_predict(state[n-2],state[n-1],s[n-1])
    tmp1 = next_predict(state[n-1],state[0],s[0])
    if tmp0 == state[0] and tmp1 == state[1] :
        isFound = 1
        ans = []
        for k in range(n):
            if state[k] == 0:
                ans.append('W')
            else:
                ans.append('S')
        print("".join(ans))
        break
    else:
        if i == 0:
            state[1] = 0
        elif i == 1:
            state[0] = 0
        else:
            state[1] = 1

if isFound == -1:
    print(-1)

Submission status: Accepted

Solution code:
import sys
N = int(input())
s = [i for i in input()]
animal = ['S','W']

for x in animal:
    ans = [i for i in range(N)]
    ans[-1] = x

    for y in animal:
        ans[0] = y
        for i in range(N):
            if (s[i] == 'o' and ans[i] == 'S') or (s[i] == 'x' and ans[i] == 'W'):
                if i < N-2:
                    ans[i+1] = ans[i-1]
                elif i == N-2:
                    if ans[i-1] != ans[-1]:
                        break
                elif i == N-1:
                    if ans[i-1] == ans[0]:
                        print(''.join(ans))
                        sys.exit()
            elif (s[i] == 'x' and ans[i] == 'S') or (s[i] == 'o' and ans[i] == 'W'):
                if i < N-2:
                    if ans[i-1] == 'S':
                        ans[i+1] = 'W'
                    elif ans[i-1] == 'W':
                        ans[i+1] = 'S'
                elif i == N-2:
                    if ans[i-1] == ans[-1]:
                        break
                elif i == N-1:
                    if ans[i-1] != ans[0]:
                        print(''.join(ans))
                        sys.exit()

print(-1)

Submission status: Accepted

Solution code:
import sys
N=int(input())
s=str(input())
t=s.replace("o","2")
t=t.replace("x","0")
t=str(t)

ans="22"
for i in range(1,N):
  ans+=str( (int(t[i])-1) * (int(ans[i-1])-1)*(int(ans[i])-1)+1 )
if ans[0]==ans[N] and (int(ans[0])-1)*(int(t[0])-1)==(int(ans[1])-1)*(int(ans[N-1])-1):
  ans=ans.replace("2","S")
  ans=ans.replace("0","W")
  print(ans[0:N])
  sys.exit()

ans="20"
for i in range(1,N):
  ans+=str( (int(t[i])-1) * (int(ans[i-1])-1)*(int(ans[i])-1)+1 )
if ans[0]==ans[N] and (int(ans[0])-1)*(int(t[0])-1)==(int(ans[1])-1)*(int(ans[N-1])-1):
  ans=ans.replace("2","S")
  ans=ans.replace("0","W")
  print(ans[0:N])
  sys.exit()

ans="02"
for i in range(1,N):
  ans+=str( (int(t[i])-1) * (int(ans[i-1])-1)*(int(ans[i])-1)+1 )
if ans[0]==ans[N] and (int(ans[0])-1)*(int(t[0])-1)==(int(ans[1])-1)*(int(ans[N-1])-1):
  ans=ans.replace("2","S")
  ans=ans.replace("0","W")
  print(ans[0:N])
  sys.exit()

ans="00"
for i in range(1,N):
  ans+=str( (int(t[i])-1) * (int(ans[i-1])-1)*(int(ans[i])-1)+1 )
if ans[0]==ans[N] and (int(ans[0])-1)*(int(t[0])-1)==(int(ans[1])-1)*(int(ans[N-1])-1):
  ans=ans.replace("2","S")
  ans=ans.replace("0","W")
  print(ans[0:N])
  sys.exit()

print(-1)

Submission status: Accepted

Solution code:
N = int(raw_input())
s = raw_input()
# 0: S, 1: W

def init():
    if s[0] == 'o':
        v = [-1 for i in range(N)]
        v[0], v[1], v[N-1] = 0, 1, 1
        yield v
        v = [-1 for i in range(N)]
        v[0], v[1], v[N-1] = 0, 0, 0
        yield v
        v = [-1 for i in range(N)]
        v[0], v[1], v[N-1] = 1, 0, 1
        yield v
        v = [-1 for i in range(N)]
        v[0], v[1], v[N-1] = 1, 1, 0
        yield v
    else:
        v = [-1 for i in range(N)]
        v[0], v[1], v[N-1] = 0, 0, 1
        yield v
        v = [-1 for i in range(N)]
        v[0], v[1], v[N-1] = 0, 1, 0
        yield v
        v = [-1 for i in range(N)]
        v[0], v[1], v[N-1] = 1, 1, 1
        yield v
        v = [-1 for i in range(N)]
        v[0], v[1], v[N-1] = 1, 0, 0
        yield v

def run():
    ok = False
    for v in init():
        ok = True
        for i in range(1, N):
            if s[i] == 'o':
                if v[i] == 0:
                    nv = v[i-1]
                else:
                    nv = 1-v[i-1]
            else:
                if v[i] == 0:
                    nv = 1-v[i-1]
                else:
                    nv = v[i-1]
            ni = (i+1) % N
            if (v[ni] > -1) and (v[ni] != nv):
                ok = False
                break
            v[ni] = nv 
        if ok:
            return ''.join(['S' if _ == 0 else 'W' for _ in v])
    return "-1"

print run()

Submission status: Accepted

Solution code:

def check(s, ans, i) :
    return s[i-1] ^ ans[i-1] ^ ans[i-2] ^ ans[i]

def solve(first, second, s) :
    n = len(s)
    ans = [0] * (n)
    ans[-2] = first;
    ans[-1] = second;



    for i in range(0, n-2) :
        nsame = 0
        nsame ^= s[i-1]
        nsame ^= ans[i-1]
        ans[i] = ans[i-2] ^ nsame
    if check( s, ans, n-2 ) == 0 and check(s, ans, n-1) == 0 :
        ans.append(1)
#    print(ans)
    return ans

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

data = [0] * N
for i in range(N) :
    data[i] = 1 if s[i] == 'x' else 0
ok = 0

for i in range(2) :
    if ok == 1 :
        break
    for j in range(2) :
        if ok == 1 :
            break
        ans = solve(i, j, data)
        ok = len(ans) - N
if ok == 0 :
    print("-1")
else :
    for i in range(N) :
        print('W' if ans[i] == 1 else 'S', end = "")
    print()

Submission status: Accepted

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

import sys
import os


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

def judge(X):
    for i in range(1, N - 1):
        left = X[i - 1]

        if X[i] == 0:
            if s[i] == 'o':
                right = left
            else:
                right = 1 - left
        else:
            if s[i] == 'o':
                right = 1 - left
            else:
                right = left
        X.append(right)

    # 全体で確認
    is_consistent = True
    for i in range(N):
        left = X[i-1]
        if i+1 <= N-1:
            right = X[i + 1]
        else:
            right = X[i + 1 - N]

        if X[i] == 0:
            if s[i] == 'o':
                if left != right:
                    is_consistent = False
            else:
                if left == right:
                    is_consistent = False
        else:
            if s[i] == 'o':
                if left == right:
                    is_consistent = False
            else:
                if left != right:
                    is_consistent = False
    return is_consistent, X

# 0: sheep
# 1: wolf
X_list = [[0, 0], [0, 1], [1, 0], [1, 1]]

results = []
for X in X_list:
    results.append(judge(X))

trues = [result for result in results if result[0] is True]
if trues:
    result = trues[0]
    X = result[1]
    for x in X:
        if x == 0:
            print('S', end='')
        else:
            print('W', end='')
    print()
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
s = list(input())
check_SW = True
X1 = ["S","S"]
X2 = ["S","W"]
X3 = ["W","S"]
X4 = ["W","W"]

flg1 , flg2 , flg3 , flg4 = "","","",""
def check(X):
    for i in range(1,N-1):
        if (X[i] == "S" and s[i]=="o") or (X[i] == "W" and s[i] == "x"):
            X.append(X[i-1])
        else:
            if X[i-1] == "S":
                X.append("W")
            else:
                X.append("S")
    for i in range(N):
        if (s[i] == "o" and X[i] == "S") or (s[i] == "x" and X[i] == "W") :
            if X[(i-1)%N] != X[(i+1)%N]:
                return False , X
                break
        else:
            if X[(i-1)%N] == X[(i+1)%N]:
                return False , X
                break
    return True , X

flg1 , X1 = check(X1)
flg2 , X2 = check(X2)
flg3 , X3 = check(X3)
flg4 , X4 = check(X4)

if flg1:
    print("".join(X1))
elif flg2:
    print("".join(X2))   
elif flg3:
    print("".join(X3))    
elif flg4:
    print("".join(X4))     
else:
    print(-1)

Submission status: Accepted

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

a = False
b = False

sw = [None] * n

sw[0] = True
sw[1] = True

def next(s,sw,j,i):
    if s[i] == 'o':
        return not (sw[j] ^ sw[i])
    else:
        return sw[j] ^ sw[i]

def printsw(sw,n):
    s = ''
    for i in range(n):
        if sw[i]:
            s += 'S'
        else:
            s += 'W'
    print(s)

flag = True

for j in range(4):
    for i in range(2,n):
        sw[i] = next(s,sw,i-2,i-1)

    zero = next(s,sw,n-2,n-1)
    one = next(s,sw,n-1,0)
    if zero == sw[0]:
        if one == sw[1]:
            printsw(sw,n)
            flag = False
            break

    sw[0] = not sw[0]
    if j == 1:
        sw[1] = False

if flag:
    print(-1)

Submission status: Accepted

Solution code:
init_ts = ["SS", "SW", "WS", "WW"]

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

def nextAnimal(ts, s):
    if ts[1]=="S":
        if s=="o":
            return ts[0]
        else:
            return notanim(ts[0])
    elif ts[1]=="W":
        if s=="o":
            return notanim(ts[0])
        else:
            return ts[0]

def maket(n, s, init_t):
    t = init_t
    new_s = s[1:] + s[0]
    for i in range(n):
        t += nextAnimal(t[-2:], new_s[i])
    return t

n = int(input())
s = input()
flag = True
for init_t in init_ts:
    tmp_t = maket(n, s, init_t)
    if tmp_t[-2:]==init_t:
        print(tmp_t[:-2])
        flag = False
        break
    else:
        pass

if flag:
    print(-1)

Submission status: Accepted

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

b = [0 if si == 'o' else 1 for si in s]
sw = [0] * N

for i in range(4):
    sw[0] = i & 1
    sw[1] = (i >> 1) & 1

    for k in range(2, N):
        sw[k] = b[k - 1] ^ sw[k - 1] ^ sw[k - 2]

    if b[N - 1] != (sw[N - 2] ^ sw[N - 1] ^ sw[0]):
        continue
    if b[0] != (sw[N - 1] ^ sw[0] ^ sw[1]):
        continue

    ans = ['W' if si else 'S' for si in sw]
    print(''.join(ans))
    quit()

print(-1)

Submission status: Accepted

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

b = [0 if si == 'o' else 1 for si in s]

for i in range(4):
    sw = [i & 1] + [(i >> 1) & 1] + [0] * (N - 2)

    for k in range(2, N):
        sw[k] = b[k - 1] ^ sw[k - 1] ^ sw[k - 2]

    if b[N - 1] != (sw[N - 2] ^ sw[N - 1] ^ sw[0]):
        continue
    if b[0] != (sw[N - 1] ^ sw[0] ^ sw[1]):
        continue

    ans = ['W' if si else 'S' for si in sw]
    print(''.join(ans))
    quit()

print(-1)

Submission status: Accepted

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

b = [0 if si == 'o' else 1 for si in s]

for i in range(2):
    for j in range(2):
        sw = [i] + [j] + [0] * (N - 2)

        for k in range(2, N):
            sw[k] = b[k - 1] ^ sw[k - 1] ^ sw[k - 2]

        if b[N - 1] != (sw[N - 2] ^ sw[N - 1] ^ sw[0]):
            continue
        if b[0] != (sw[N - 1] ^ sw[0] ^ sw[1]):
            continue

        ans = ['W' if si else 'S' for si in sw]
        print(''.join(ans))
        quit()

print(-1)

Submission status: Accepted

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

P = [[1]*N,[1]*N,[-1]*N,[-1]*N]

if S[0]==1:
    P[0][1]=1
    P[0][N-1]=1
    P[1][1] = -1
    P[1][N - 1] = -1
    P[2][1] = 1
    P[2][N - 1] = -1
    P[3][1] = -1
    P[3][N - 1] = 1
else:
    P[0][1] = 1
    P[0][N - 1] = -1
    P[1][1] = -1
    P[1][N - 1] = 1
    P[2][1] = 1
    P[2][N - 1] = 1
    P[3][1] = -1
    P[3][N - 1] = -1

result = -1
for i in range(1,N-1):
    if i == N-2:
        for j in range(4):
            if S[i] * P[j][i] == 1:
                if P[j][i + 1] == P[j][i - 1]:
                    if S[i+1] * P[j][i+1] == 1:
                        if P[j][0] == P[j][i]:
                            result = j
                            break
                    else:
                        if P[j][0] == - P[j][i]:
                            result = j
                            break
            else:
                if P[j][i + 1] == - P[j][i - 1]:
                    if S[i+1] * P[j][i+1] == 1:
                        if P[j][0] == P[j][i]:
                            result = j
                            break
                    else:
                        if P[j][0] == - P[j][i]:
                            result = j
                            break
    for j in range(4):
        if S[i]*P[j][i]==1:
            P[j][i+1]=P[j][i-1]
        else:
            P[j][i + 1] = - P[j][i - 1]

if result == -1:
    print(result)
else:
    for i in range(N):
        if P[result][i]==1:
            print('S',end="")
        else:
            print('W',end="")

Submission status: Accepted

Solution code:
options = [(0, 0), (0, 1), (1, 0), (1, 1)]


def judge(n, s):
    res = [None] * n
    for op in options:
        res[-1], res[0] = op
        s_num = list(map(lambda a: 0 if a == 'o' else 1, s))
        for i in range(n):
            res[(i + 1) % n] = res[i] ^ s_num[i] ^ res[i - 1]
        if (res[-1], res[0]) == op:
            print(''.join(map(lambda a: 'S' if a == 0 else 'W', res)))
            exit()

n = int(input())
s = input()
judge(n, s)
print(-1)