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()
ans='SS'
for i in range(1,N):
    if s[i]=='o' and ans[-1]=='S' and ans[-2]=='S':
        ans+='S'
    elif s[i]=='o' and ans[-1]=='S' and ans[-2]=='W':
        ans+='W'
    elif s[i]=='o' and ans[-1]=='W' and ans[-2]=='S':
        ans+='W'
    elif s[i]=='o' and ans[-1]=='W' and ans[-2]=='W':
        ans+='S'
    if s[i]=='x' and ans[-1]=='S' and ans[-2]=='S':
        ans+='W'
    elif s[i]=='x' and ans[-1]=='S' and ans[-2]=='W':
        ans+='S'
    elif s[i]=='x' and ans[-1]=='W' and ans[-2]=='S':
        ans+='S'
    elif s[i]=='x' and ans[-1]=='W' and ans[-2]=='W':
        ans+='W'

if ans[0]==ans[-1]:
    ans=ans[:N]
    if s[0]=='o' and ans[-1]==ans[1]:
        print(ans)
        exit()
    elif s[0]=='x' and ans[-1]!=ans[1]:
        print(ans)
        exit()
    

ans='SW'
for i in range(1,N):
    if s[i]=='o' and ans[-1]=='S' and ans[-2]=='S':
        ans+='S'
    elif s[i]=='o' and ans[-1]=='S' and ans[-2]=='W':
        ans+='W'
    elif s[i]=='o' and ans[-1]=='W' and ans[-2]=='S':
        ans+='W'
    elif s[i]=='o' and ans[-1]=='W' and ans[-2]=='W':
        ans+='S'
    if s[i]=='x' and ans[-1]=='S' and ans[-2]=='S':
        ans+='W'
    elif s[i]=='x' and ans[-1]=='S' and ans[-2]=='W':
        ans+='S'
    elif s[i]=='x' and ans[-1]=='W' and ans[-2]=='S':
        ans+='S'
    elif s[i]=='x' and ans[-1]=='W' and ans[-2]=='W':
        ans+='W'

if ans[0]==ans[-1]:
    ans=ans[:N]
    if s[0]=='o' and ans[-1]==ans[1]:
        print(ans)
        exit()
    elif s[0]=='x' and ans[-1]!=ans[1]:
        print(ans)
        exit()

ans='WS'
for i in range(1,N):
    if s[i]=='o' and ans[-1]=='S' and ans[-2]=='S':
        ans+='S'
    elif s[i]=='o' and ans[-1]=='S' and ans[-2]=='W':
        ans+='W'
    elif s[i]=='o' and ans[-1]=='W' and ans[-2]=='S':
        ans+='W'
    elif s[i]=='o' and ans[-1]=='W' and ans[-2]=='W':
        ans+='S'
    if s[i]=='x' and ans[-1]=='S' and ans[-2]=='S':
        ans+='W'
    elif s[i]=='x' and ans[-1]=='S' and ans[-2]=='W':
        ans+='S'
    elif s[i]=='x' and ans[-1]=='W' and ans[-2]=='S':
        ans+='S'
    elif s[i]=='x' and ans[-1]=='W' and ans[-2]=='W':
        ans+='W'

if ans[0]==ans[-1]:
    ans=ans[:N]
    if s[0]=='x' and ans[-1]==ans[1]:
        print(ans)
        exit()
    elif s[0]=='o' and ans[-1]!=ans[1]:
        print(ans)
        exit()

ans='WW'
for i in range(1,N):
    if s[i]=='o' and ans[-1]=='S' and ans[-2]=='S':
        ans+='S'
    elif s[i]=='o' and ans[-1]=='S' and ans[-2]=='W':
        ans+='W'
    elif s[i]=='o' and ans[-1]=='W' and ans[-2]=='S':
        ans+='W'
    elif s[i]=='o' and ans[-1]=='W' and ans[-2]=='W':
        ans+='S'
    if s[i]=='x' and ans[-1]=='S' and ans[-2]=='S':
        ans+='W'
    elif s[i]=='x' and ans[-1]=='S' and ans[-2]=='W':
        ans+='S'
    elif s[i]=='x' and ans[-1]=='W' and ans[-2]=='S':
        ans+='S'
    elif s[i]=='x' and ans[-1]=='W' and ans[-2]=='W':
        ans+='W'

if ans[0]==ans[-1]:
    ans=ans[:N]
    if s[0]=='x' and ans[-1]==ans[1]:
        print(ans)
        exit()
    elif s[0]=='o' and ans[-1]!=ans[1]:
        print(ans)
        exit()

print(-1)

Submission status: Accepted

Solution code:
# AtCoder Regular Contest 069
# D - Menagerie
# https://atcoder.jp/contests/arc069/tasks/arc069_b
import sys
N = int(input())
S = [1 if s == 'o' else 0 for s in input()]

tostr = ('W', 'S')


def f(a, b):
    last = prev = a
    first = current = b
    ans = []
    for s in S:
        ans.append(tostr[current])
        prev, current = current, prev ^ current ^ s
    if current == first and prev == last:
        print(''.join(ans))
        sys.exit()

f(1, 1)
f(1, 0)
f(0, 1)
f(0, 0)

print('-1')

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
s = ns()
a = [['S','S'],['S','W'],['W','S'],['W','W']]

for b in a:
    for i in range(1,n):
        c = 0 if b[i] == 'S' else 1
        d = 0 if s[i] == 'o' else 1
        e = b[i-1] if not c^d else 'W' if b[i-1] == 'S' else 'S'
        b.append(e)
    c = 0 if b[0] == 'S' else 1
    d = 0 if s[0] == 'o' else 1
    e = 0 if b[-2] == b[1] else 1
    if b[0] == b[-1] and c^d == e:
        print(''.join(b[:-1]))
        exit()

print(-1)

Submission status: Accepted

Solution code:
n = int(input())
s = tuple(input())
ans = ['SS','SW','WS','WW']

for i in range(1,n-1):
    for j in range(4):
        if (s[i] == 'o') ^ (ans[j][-1] == 'S'):
            ans[j] += 'SW'.strip(ans[j][-2])
        else:
            ans[j] += ans[j][-2]
for i in range(4):
    if (s[0] == 'o') ^ (ans[i][0] == 'S'):
        if ans[i][1] == ans[i][n-1]:
            continue
    else:
        if ans[i][1] != ans[i][n-1]:
            continue
    if (s[n-1] == 'o') ^ (ans[i][n-1] == 'S'):
        if ans[i][0] == ans[i][n-2]:
            continue
    else:
        if ans[i][0] != ans[i][n-2]:
            continue
    print(ans[i])
    exit(0)
print(-1)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
animals = ["S", "W"]
for s1 in animals:
    for s2 in animals:
        t = [s1, s2]
        for i in range(1, N-1):
            if s[i] == "o":
                if t[i] == "S":
                    if t[i-1] == "S":
                        t.append("S")
                    else:
                        t.append("W")
                else:
                    if t[i-1] == "S":
                        t.append("W")
                    else:
                        t.append("S")
            else:
                if t[i] == "S":
                    if t[i-1] == "S":
                        t.append("W")
                    else:
                        t.append("S")
                else:
                    if t[i-1] == "S":
                        t.append("S")
                    else:
                        t.append("W")
        ans = ""
        for i in range(N):
            prev = i - 1 if 0 <= i - 1 else N - 1
            next = i + 1 if i + 1 < N else 0
            if t[i] == "S":
                if t[prev] == t[next]:
                    ans += "o"
                else:
                    ans += "x"
            else:
                if t[prev] == t[next]:
                    ans += "x"
                else:
                    ans += "o"
        if ans == s:
            print("".join(t))
            exit()
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
a = "SS"
ct=1
while ct<7:    
    for i in range(n-1):
        p=a[i]
        q="A"
        if a[i]=="S":
            q="W"
        else:
            q="S"
        if a[i+1]=="S":
            if s[i+1] == "o":
                a=a+a[i]
            else:
                a=a+q
        else:
            if s[i+1] == "o":
                 a=a+q
            else:
                a=a+a[i]
    if a[0]==a[n]:
        if a[0]=="S" and s[0]=="o" and a[n-1]==a[1]:
            print(a[0:n])
            break
        elif a[0]=="S" and s[0] !="o" and a[n-1]!=a[1]:         
            print(a[0:n])
            break
        elif a[0]=="W" and s[0] =="o" and a[n-1]!=a[1]: 
            print(a[0:n])
            break
        elif a[0]=="W" and s[0] !="o" and a[n-1]==a[1]: 
            print(a[0:n])
            break
        else:
            ct=ct+1
            if ct==2:
                a="SW"
            elif ct==3:
                a="WS"
            elif ct==4:
                a="WW"
            else:
                print(-1)
                break
    else:
        ct=ct+1
        if ct==2:
            a="SW"
        elif ct==3:
            a="WS"
        elif ct==4:
            a="WW"
        else:
            print(-1)
            break

Submission status: Accepted

Solution code:
n = int(input())
s = input()
flag = False
for i in range(4):
    if i == 0:
        ans = 'SS'
        past = 'S'
        cur = 'S'
        for j in range(1,n-1):
            if cur == 'S':
                if s[j] == 'o':
                    if past == 'S':
                        ans += 'S'
                    else:
                        ans += 'W'
                        past = 'S'
                        cur = 'W'
                else:
                    if past == 'S':
                        ans += 'W'
                        cur = 'W'
                    else:
                        ans += 'S'
                        past = 'S'
            else:
                if cur == 'W':
                    if s[j] == 'o':
                        if past == 'S':
                            ans += 'W'
                            past = 'W'
                        else:
                            ans += 'S'
                            cur = 'S'
                    else:
                        if past == 'S':
                            ans += 'S'
                            past = 'W'
                            cur = 'S'
                        else:
                            ans += 'W'
        if ans[len(ans)-1] == 'S':
            if s[n-1] == 'o':
                if ans[0] == ans[len(ans)-2]:
                    if s[0] == 'o':
                        if ans[1] == ans[len(ans)-1]:
                            flag = True
                            break
                    else:
                        if ans[1] != ans[len(ans)-1]:
                            flag = True
                            break
            else:
                if ans[0] != ans[len(ans)-2]:
                    if s[0] == 'o':
                        if ans[1] == ans[len(ans)-1]:
                            flag = True
                            break
                    else:
                        if ans[1] != ans[len(ans)-1]:
                            flag = True
                            break
        else:
            if s[n-1] == 'o':
                if ans[0] != ans[len(ans)-2]:
                    if s[0] == 'o':
                        if ans[1] == ans[len(ans)-1]:
                            flag = True
                            break
                    else:
                        if ans[1] != ans[len(ans)-1]:
                            flag = True
                            break
            else:
                if ans[0] == ans[len(ans)-2]:
                    if s[0] == 'o':
                        if ans[1] == ans[len(ans)-1]:
                            flag = True
                            break
                    else:
                        if ans[1] != ans[len(ans)-1]:
                            flag = True
                            break
    elif i == 1:
        ans = 'SW'
        past = 'S'
        cur = 'W'
        for j in range(1,n-1):
            if cur == 'S':
                if s[j] == 'o':
                    if past == 'S':
                        ans += 'S'
                    else:
                        ans += 'W'
                        past = 'S'
                        cur = 'W'
                else:
                    if past == 'S':
                        ans += 'W'
                        cur = 'W'
                    else:
                        ans += 'S'
                        past = 'S'
            else:
                if cur == 'W':
                    if s[j] == 'o':
                        if past == 'S':
                            ans += 'W'
                            past = 'W'
                        else:
                            ans += 'S'
                            cur = 'S'
                    else:
                        if past == 'S':
                            ans += 'S'
                            past = 'W'
                            cur = 'S'
                        else:
                            ans += 'W'
        if ans[len(ans)-1] == 'S':
            if s[n-1] == 'o':
                if ans[0] == ans[len(ans)-2]:
                    if s[0] == 'o':
                        if ans[1] == ans[len(ans)-1]:
                            flag = True
                            break
                    else:
                        if ans[1] != ans[len(ans)-1]:
                            flag = True
                            break
            else:
                if ans[0] != ans[len(ans)-2]:
                    if s[0] == 'o':
                        if ans[1] == ans[len(ans)-1]:
                            flag = True
                            break
                    else:
                        if ans[1] != ans[len(ans)-1]:
                            flag = True
                            break
        else:
            if s[n-1] == 'o':
                if ans[0] != ans[len(ans)-2]:
                    if s[0] == 'o':
                        if ans[1] == ans[len(ans)-1]:
                            flag = True
                            break
                    else:
                        if ans[1] != ans[len(ans)-1]:
                            flag = True
                            break
            else:
                if ans[0] == ans[len(ans)-2]:
                    if s[0] == 'o':
                        if ans[1] == ans[len(ans)-1]:
                            flag = True
                            break
                    else:
                        if ans[1] != ans[len(ans)-1]:
                            flag = True
                            break
    elif i == 2:
        ans = 'WS'
        past = 'W'
        cur = 'S'
        for j in range(1,n-1):
            if cur == 'S':
                if s[j] == 'o':
                    if past == 'S':
                        ans += 'S'
                    else:
                        ans += 'W'
                        past = 'S'
                        cur = 'W'
                else:
                    if past == 'S':
                        ans += 'W'
                        cur = 'W'
                    else:
                        ans += 'S'
                        past = 'S'
            else:
                if cur == 'W':
                    if s[j] == 'o':
                        if past == 'S':
                            ans += 'W'
                            past = 'W'
                        else:
                            ans += 'S'
                            cur = 'S'
                    else:
                        if past == 'S':
                            ans += 'S'
                            past = 'W'
                            cur = 'S'
                        else:
                            ans += 'W'
        if ans[len(ans)-1] == 'S':
            if s[n-1] == 'o':
                if ans[0] == ans[len(ans)-2]:
                    if s[0] == 'o':
                        if ans[1] != ans[len(ans)-1]:
                            flag = True
                            break
                    else:
                        if ans[1] == ans[len(ans)-1]:
                            flag = True
                            break
            else:
                if ans[0] != ans[len(ans)-2]:
                    if s[0] == 'o':
                        if ans[1] != ans[len(ans)-1]:
                            flag = True
                            break
                    else:
                        if ans[1] == ans[len(ans)-1]:
                            flag = True
                            break
        else:
            if s[n-1] == 'o':
                if ans[0] != ans[len(ans)-2]:
                    if s[0] == 'o':
                        if ans[1] != ans[len(ans)-1]:
                            flag = True
                            break
                    else:
                        if ans[1] == ans[len(ans)-1]:
                            flag = True
                            break
            else:
                if ans[0] == ans[len(ans)-2]:
                    if s[0] == 'o':
                        if ans[1] != ans[len(ans)-1]:
                            flag = True
                            break
                    else:
                        if ans[1] == ans[len(ans)-1]:
                            flag = True
                            break
    else:
        ans = 'WW'
        past = 'W'
        cur = 'W'
        for j in range(1,n-1):
            if cur == 'S':
                if s[j] == 'o':
                    if past == 'S':
                        ans += 'S'
                    else:
                        ans += 'W'
                        past = 'S'
                        cur = 'W'
                else:
                    if past == 'S':
                        ans += 'W'
                        cur = 'W'
                    else:
                        ans += 'S'
                        past = 'S'
            else:
                if cur == 'W':
                    if s[j] == 'o':
                        if past == 'S':
                            ans += 'W'
                            past = 'W'
                        else:
                            ans += 'S'
                            cur = 'S'
                    else:
                        if past == 'S':
                            ans += 'S'
                            past = 'W'
                            cur = 'S'
                        else:
                            ans += 'W'
        if ans[len(ans)-1] == 'S':
            if s[n-1] == 'o':
                if ans[0] == ans[len(ans)-2]:
                    if s[0] == 'o':
                        if ans[1] != ans[len(ans)-1]:
                            flag = True
                            break
                    else:
                        if ans[1] == ans[len(ans)-1]:
                            flag = True
                            break
            else:
                if ans[0] != ans[len(ans)-2]:
                    if s[0] == 'o':
                        if ans[1] != ans[len(ans)-1]:
                            flag = True
                            break
                    else:
                        if ans[1] == ans[len(ans)-1]:
                            flag = True
                            break
        else:
            if s[n-1] == 'o':
                if ans[0] != ans[len(ans)-2]:
                    if s[0] == 'o':
                        if ans[1] != ans[len(ans)-1]:
                            flag = True
                            break
                    else:
                        if ans[1] == ans[len(ans)-1]:
                            flag = True
                            break
            else:
                if ans[0] == ans[len(ans)-2]:
                    if s[0] == 'o':
                        if ans[1] != ans[len(ans)-1]:
                            flag = True
                            break
                    else:
                        if ans[1] == ans[len(ans)-1]:
                            flag = True
                            break
if flag:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
l = [[0,0],[0,1],[1,0],[1,1]]
for i in range(4):
  for j in range(1,n-1):
    if s[j] == 'o':
      if l[i][-1] == 0:
        l[i].append(l[i][-2])
      else:
        l[i].append(1-l[i][-2])
    else:
      if l[i][-1] == 0:
        l[i].append(1-l[i][-2])
      else:
        l[i].append(l[i][-2])
  if (s[0]=='o' and l[i][0]==0 and l[i][1]!=l[i][-1]) or (s[0]=='o' and l[i][0]==1 and l[i][1]==l[i][-1]) or (s[0]=='x' and l[i][0]==0 and l[i][1]==l[i][-1]) or (s[0]=='x' and l[i][0]==1 and l[i][1]!=l[i][-1]):
    continue
  if (s[-1]=='o' and l[i][-1]==0 and l[i][0]!=l[i][-2]) or (s[-1]=='o' and l[i][-1]==1 and l[i][0]==l[i][-2]) or (s[-1]=='x' and l[i][-1]==0 and l[i][0]==l[i][-2]) or (s[-1]=='x' and l[i][-1]==1 and l[i][0]!=l[i][-2]):
    continue
  for j in l[i]:
    print('SW'[j],end='')
  exit()
print(-1)

Submission status: Accepted

Solution code:
def calc(s,ans1,ans2):
  if s=="o":
    if ans1=="S":
      if ans2=="S":
        return "S"
      else:
        return "W"
    else:
      if ans2=="S":
        return "W"
      else:
        return "S"
  else:
    if ans1=="S":
      if ans2=="S":
        return "W"
      else:
        return "S"
    else:
      if ans2=="S":
        return "S"
      else:
        return "W"

def check(ans0,ans1):
  ans=[""]*n
  ans[0]=ans0
  ans[1]=ans1
  ans[-1]=calc(s[0],ans[0],ans[1])
  for i in range(2,n):ans[i]=calc(s[i-1],ans[i-1],ans[i-2])
  if ans[-1]==calc(s[0],ans[0],ans[1]) and ans[0]==calc(s[-1],ans[-1],ans[-2]):
    print("".join(ans))
    exit()
  else:
    return 0
n=int(input())
s=input()
check("S","S")
check("S","W")
check("W","S")
check("W","W")
print(-1)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
l=['SS','SW','WS','WW']
if s[0]=='o':
    g=['S','W','W','S']
else:
    g=['W','S','S','W']
for i in range(4):
    ans=l[i]
    for j in range(1,n):
        if s[j]=='o':
            for k in range(4):
                if ans[j-1]+ans[j]==l[k]:
                    if k==0 or k==3:
                        ans+='S'
                    else:
                        ans+='W'
        else:
            for k in range(4):
                if ans[j-1]+ans[j]==l[k]:
                    if k==0 or k==3:
                        ans+='W'
                    else:
                        ans+='S'
    if ans[-1]==ans[0] and ans[-2]==g[i]:
        print(ans[:-1])
        exit()
print(-1)

Submission status: Accepted

Solution code:
from itertools import product
N = int(input())
s = input()
s += s[0]

def isOK(x, y):
    lst = [-1] * (N+2)
    lst[0] = x
    lst[1] = y
    for i in range(1, N+1):
        if (s[i] == 'o') ^ (lst[i]):    # 両隣は違う
            lst[i+1] = 1 - lst[i-1]
        else:                       # 両隣は同じ
            lst[i+1] = lst[i-1]
    if lst[0] == lst[-2] and lst[1] == lst[-1]:     # 1周回って同じ
        return lst[:-2]
    else:
        return []

for x, y in product([1,0], repeat=2):
    chk = isOK(x, y)
    if len(chk) > 0:
        break

if len(chk) == 0:
    ans = -1
else:
    ans = ''
    for x in chk:
        if x:
            ans += 'S'
        else:
            ans += 'W'
print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n = int(input())
s = input()

def solve(first,second):
    sheep=[]

    sheep.append(first)
    sheep.append(second)

    for i in range(1,n):
        rep= s[i] == "o"
        if not rep ^ sheep[i]:
            sheep.append(sheep[i-1])
        else:
            sheep.append(not sheep[i-1])

    if sheep[0] == sheep[-1]:
        rep= s[0] == "o"
        same = sheep[-2] == sheep[1]
        if sheep[0] ^ rep != same:
            for a in sheep[:-1]:
                if a:
                    print("S",end="")
                else:
                    print("W",end="")
            exit()

fir=[True,False]
sec=[True,False]

for i in fir:
    for j in sec:
        solve(i,j)
print(-1)

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

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

N = I()
s = str(input())

# 0:羊  1:狼
a = [-1]*N
A1,A2,A3,A4 = a[:],a[:],a[:],a[:]

def check(A):
    for i in range(1,N):
        now = 0 if s[i] == 'o' else 1
        temp = A[i-1] ^ A[i] ^ now
        if 1 <= i <= N-2:
            A[i+1] = temp
        else:
            if temp != A[0]:
                return
    now = 0 if s[0] == 'o' else 1
    if A[N-1] ^ A[0] ^ A[1] ^ now:
        return
    return A

A1[0],A1[1] = 0,0
A2[0],A2[1] = 0,1
A3[0],A3[1] = 1,0
A4[0],A4[1] = 1,1

ans = []
ans.extend([check(A1),check(A2),check(A3),check(A4)])
for x in ans:
    if x is not None:
        x = map(str,x)
        print(''.join(x).replace('0','S').replace('1','W'))
        exit()

print(-1)

Submission status: Accepted

Solution code:
# True -> S or o
# False -> W or x

N = int(input())
s = [x=="o" for x in input()]

def is_true(L1:bool, L2:bool):
    L = [None] * N

    L[0] = L1
    L[1] = L2
    for i in range(1,N):
        if i == N-1:
            if s[i]: # o
                tmp = L[i-1] if L[i] else (not L[i-1])
            else: # x
                tmp = L[i-1] if not L[i] else (not L[i-1])
            if tmp == L[0]:
                continue
            else:
                return (False, L)

        if s[i]: # o
            L[i+1] = L[i-1] if L[i] else (not L[i-1])
        else: # x
            L[i+1] = L[i-1] if not L[i] else (not L[i-1])
    
    if s[0]: # o
        if L[0]: # 羊
            return (True, L) if L[-1] == L[1] else (False, L)
        else: # 狼
            return (True, L) if L[-1] != L[1] else (False, L)
    else: # x
        if L[0]: # 羊
            return (True, L) if L[-1] != L[1] else (False, L)
        else: # 狼
            return (True, L) if L[-1] == L[1] else (False, L)

def main():
    for L1 in (True, False):
        for L2 in (True, False):
            flag, tmp = is_true(L1, L2)
            if not flag:
                continue
            else:
                ans = ""
                for x in tmp:
                    ans += "S" if x else "W"
                print(ans)
                exit()
    
    print(-1)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import product
N = int(input())
S = input()
S += S[0]


def isOK(x, y):
    WS = [-1] * (N + 2)
    WS[0] = x
    WS[1] = y
    for i in range(1, N + 1):
        if S[i] == "o":
            WS[i + 1] = WS[i - 1] if WS[i] else 1 - WS[i - 1]
        else:
            WS[i + 1] = 1 - WS[i - 1] if WS[i] else WS[i - 1]
    if WS[0] == WS[-2] and WS[1] == WS[-1]:
        return WS[:-2]
    else:
        return []


for x, y in product(range(2), repeat=2):
    ws = isOK(x, y)
    if len(ws) > 0:
        break

if len(ws) == 0:
    ans = -1
else:
    ans = ""
    for x in ws:
        if x:
            ans += "S"
        else:
            ans += "W"

print(ans)

Submission status: Accepted

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

cnt = 0
while cnt < 4:
    L = [0]*N
    if cnt == 0:
        L[-1] = "S"
        L[-2] = "S"
    elif cnt == 1:
        L[-1] = "S"
        L[-2] = "W"
    elif cnt == 2:
        L[-1] = "W"
        L[-2] = "S"
    else:
        L[-1] = "W"
        L[-2] = "W"

    for j in range(N-2, -1, -1):
        if L[j] == "S" and S[j] == "o":
            L[j-1] = L[j+1]
        elif L[j] == "S":
            if L[j+1] == "S":
                L[j-1] = "W"
            else:
                L[j-1] = "S"
        elif L[j] == "W" and S[j] == "o":
            if L[j+1] == "S":
                L[j-1] = "W"
            else:
                L[j-1] = "S"
        else:
            L[j-1] = L[j+1]
    if cnt <= 1 and L[-1] == "S" and S[-1] == "o":
        if L[-2] == L[0]:
            break
    elif cnt <= 1 and L[-1] == "S" and S[-1] == "x":
        if L[-2] != L[0]:
            break
    elif cnt >= 2 and L[-1] == "W" and S[-1] == "o":
        if L[-2] != L[0]:
            break
    elif cnt >= 2 and L[-1] == "W" and S[-1] == "x":
        if L[-2] == L[0]:
            break
    cnt += 1

if cnt == 4:
    print(-1)
else:
    print("".join(L))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    dsw = tuple([("S", "S"), ("S", "W"), ("W", "S"), ("W", "W")])
    for d1, d2 in dsw:
        cur = d1+d2
        for s in S[1:N-1]:
            if cur[-1] == "S":
                if s == "o":
                    cur += cur[-2]
                else:
                    cur += ("W" if cur[-2] == "S" else "S")
            else:
                if s == "o":
                    cur += ("W" if cur[-2] == "S" else "S")
                else:
                    cur += cur[-2]
        flag = False
        if cur[-1] == "S":
            if S[-1] == "o":
                flag = (cur[-2] == cur[0])
            else:
                flag = (cur[-2] != cur[0])
        else:
            if S[-1] == "o":
                flag = (cur[-2] != cur[0])
            else:
                flag = (cur[-2] == cur[0])
        if flag:
            if cur[0] == "S":
                if S[0] == "o":
                    flag = (cur[-1] == cur[1])
                else:
                    flag = (cur[-1] != cur[1])
            else:
                if S[0] == "o":
                    flag = (cur[-1] != cur[1])
                else:
                    flag = (cur[-1] == cur[1])
        if flag:
            return print(cur)

    print(-1)


if __name__ == '__main__':
    main()