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
input = sys.stdin.readline
N = int(input())
s = list(input())[: -1]
d = ["S", "W"]
for i in range(2):
  for j in range(2):
    res = [d[i], d[j]]
    for k in range(1, N - 1):
      if (s[k] == "o" and res[k] == "S") or (s[k] == "x" and res[k] == "W"):
        if res[k - 1] == "S": res.append("S")
        else: res.append("W")
      elif (s[k] == "x" and res[k] == "S") or (s[k] == "o" and res[k] == "W"):
        if res[k - 1] == "S": res.append("W")
        else: res.append("S")
    if (s[0] == "o" and res[0] == "S") or (s[0] == "x" and res[0] == "W"):
      if res[-1] != res[1]:
        continue
    if (s[0] == "x" and res[0] == "S") or (s[0] == "o" and res[0] == "W"):
      if res[-1] == res[1]:
        continue
    if (s[-1] == "o" and res[-1] == "S") or (s[-1] == "x" and res[-1] == "W"):
      if res[-2] != res[0]:
        continue
    if (s[-1] == "x" and res[-1] == "S") or (s[-1] == "o" and res[-1] == "W"):
      if res[-2] == res[0]:
        continue
    print("".join(res))
    exit(0)
print(-1)

Submission status: Accepted

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

def test(dp,n,a,b):
    dp[0]=a
    dp[-1]=b
    flag=0
    if n%2==0:
        for i in range(n):
            if not ((s[i]=='o') ^ (dp[i]==0)):
                if dp[(i+1)%n]==-1 or dp[(i+1)%n]==dp[i-1]:
                    dp[(i+1)%n]=dp[i-1]
                else:
                    flag=-1
            else:
                if dp[(i+1)%n]==-1 or dp[(i+1)%n]!=dp[i-1]:
                    dp[(i+1)%n]=1-dp[i-1]
                else:
                    flag=-1
    else:
        for i in range(n):
            if not ((s[i]=='o') ^ (dp[i]==0)):
                if dp[(i+1)%n]==-1 or dp[(i+1)%n]==dp[i-1]:
                    dp[(i+1)%n]=dp[i-1]
                else:
                    flag=-1
            else:
                if dp[(i+1)%n]==-1 or dp[(i+1)%n]!=dp[i-1]:
                    dp[(i+1)%n]=1-dp[i-1]
                else:
                    flag=-1
    return flag

for i in range(4):
    flag=0
    dp=[-1]*n
    flag=test(dp,n,i&1,i>>1)
    if flag!=-1:
        break
    
if flag==-1:
    print(-1)
    sys.exit()
for i in range(n):
    if dp[i]==0:
        print('S',end='')
    else:
        print('W',end='')
print('\n')

Submission status: Accepted

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

for lf in ['SS','SW','WS','WW']:
    tmp = lf
    for c in S:
        if (tmp[-1]=='W') ^ (c=='x'):
            t = 'S' if tmp[-2]=='W' else 'W'
        else:
            t = 'S' if tmp[-2]=='S' else 'W'
        tmp += t
    if tmp[:2] == tmp[-2:]:
        print(tmp[1:-1])
        exit()
print(-1)

Submission status: Accepted

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

for first in [1, -1]:
    for second in [1, -1]:
        ans = [first, second]
        prev = first
        now = second
        for s in S[1:] + [S[0]]:
            if s == 1:
                if now == 1:
                    prev, now = now, prev
                else:
                    prev, now = now, -prev
            else:
                if now == 1:
                    prev, now = now, -prev
                else:
                    prev, now = now, prev
            ans.append(now)
        if first == ans[-2] and second == ans[-1]:
            print(*['S' if a == 1 else 'W' for a in ans[:-2]], sep='')
            exit()

print(-1)

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
s += s
candidate = [[1,-1],[1,1],[-1,-1],[-1,1]]
result = []
checker = 0
while checker < 4:
    result = candidate[checker]
    for i in range(1, len(s)-1):
        if s[i] == 'o' and result[i] == 1:
            result.append(result[-2])
        elif s[i] == 'x' and result[i] == 1:
            result.append(-result[-2])
        elif s[i] == 'o' and result[i] == -1:
            result.append(-result[-2])
        else:
            result.append(result[-2])
    if result[:n] == result[n:]:
        checker += 10
    else:
        checker += 1
if checker == 4:
    print(-1)
else:
    result = ['S' if x == 1 else 'W' for x in result[:n]]
    print(''.join(result))

Submission status: Accepted

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

def cali(string):
    lists=[0 for i in range(n)]
    lists[0]=string[0]
    lists[1]=string[1]
    flag1=False
    flag2=False
    for  _ in range(2,n):
        if s[_-1]=="o" and lists[_-2]=="W" and lists[_-1]=="W":
            lists[_]="S"
            
        elif s[_-1]=="o" and lists[_-2]=="W" and lists[_-1]=="S":
            lists[_]="W"
            
        elif s[_-1]=="o" and lists[_-2]=="S" and lists[_-1]=="W":
            lists[_]="W"
            
        elif s[_-1]=="o" and lists[_-2]=="S" and lists[_-1]=="S":
            lists[_]="S"        
            
        elif s[_-1]=="x" and lists[_-2]=="W" and lists[_-1]=="W":   
            lists[_]="W"
            
        elif s[_-1]=="x" and lists[_-2]=="W" and lists[_-1]=="S":
            lists[_]="S"
        
        elif s[_-1]=="x" and lists[_-2]=="S" and lists[_-1]=="W":
            lists[_]="S"
            
        elif s[_-1]=="x" and lists[_-2]=="S" and lists[_-1]=="S":   
            lists[_]="W"
    

    if s[0]=="o" and lists[0]=="W":
        if lists[1]!=lists[-1]:
            flag1=True

    elif s[0]=="o" and lists[0]=="S":
        if lists[1]==lists[-1]:
            flag1=True
     
    
    elif s[0]=="x" and lists[0]=="W":
        if lists[1]==lists[-1]:
            flag1=True

    elif s[0]=="x" and lists[0]=="S":
        if lists[1]!=lists[-1]:
            flag1=True

    if s[-1]=="o" and lists[-1]=="W":
        if lists[-2]!=lists[0]:
            flag2=True
    elif s[-1]=="o" and lists[-1]=="S":
        if lists[-2]==lists[0]:
            flag2=True

    elif s[-1]=="x" and lists[-1]=="W":
        if lists[-2]==lists[0]:
            flag2=True
    elif s[-1]=="x" and lists[-1]=="S":
        if lists[-2]!=lists[0]:
            flag2=True
    if flag1 and flag2:
         return lists
    else:
          return 0
        

stri=""
a=cali("WW")
b=cali("WS")
c=cali("SW")
d=cali("SS")

if a!=0:
    for i in range(n):
        stri+=a[i]
    print(stri)

elif b!=0:
        for i in range(n):
            stri+=b[i]
        print(stri)
        
elif c!=0:
        for i in range(n):
            stri+=c[i]
        print(stri)
        
elif d!=0:
        for i in range(n):
            stri+=d[i]
        print(stri)
else:
    print("-1")

Submission status: Accepted

Solution code:
N=int(input())
S=[1 if c=='o' else 0 for c in input()]
S+=S[:2]
from itertools import product
f=0
for a,b in product(range(2),repeat=2):
    r=[a,b]+[0]*N
    for i in range(N):
        r[i+2]=r[i] if r[i+1]^S[i+1] else r[i]^1
    if r[:2]==r[N:]:
        f=1
        break
print(''.join(['S' if x==0 else 'W' for x in r[:N]]) if f else -1)

Submission status: Accepted

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


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

for j in range(4) :
    t = A[j]
    for i in range(1,N) :
        if S[i] == "o" :
            if t[i] == "S" :
                if t[i-1] == "S" :
                    t += "S"
                else :
                    t += "W"
            else :
                if t[i-1] == "S" :
                    t += "W"
                else :
                    t += "S"
        else :
            if t[i] == "S" :
                if t[i-1] == "S" :
                    t += "W"
                else :
                    t += "S"
            else :
                if t[i-1] == "S" :
                    t += "S"
                else :
                    t += "W"
    
    if t[0] == t[-1] :
        if S[0] == "o" and t[0] == "S" and (t[-2] == t[1]) :
            print(t[:-1])
            break
        elif S[0] == "o" and (t[0] == "W") and (t[-2] != t[1]) :
            print(t[:-1])
            break
        if S[0] == "x" and t[0] == "S" and (t[-2] != t[1]) :
            print(t[:-1])
            break
        elif S[0] == "x" and (t[0] == "W") and (t[-2] == t[1]) :
            print(t[:-1])
            break
else :
    print(-1)

Submission status: Accepted

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


def check(t):
    t = t[-1] + t + t[0]
    for i in range(1, n + 1):
        if s[i - 1] != ('o' if (t[i] == 'S') ^ (t[i - 1] != t[i + 1]) else 'x'):
            return
    print(t[1:n+1])
    import sys
    sys.exit()


def getans(ans):
    ans = list(ans)
    for i in range(1, n):
        ans[i + 1] = 'S' if (ans[i] == 'S') ^ (s[i] == 'o') ^ (ans[i - 1] == 'S') else 'W'
    check("".join(ans)[:-1])


getans('WW' + '.' * (n - 1))
getans('WS' + '.' * (n - 1))
getans('SW' + '.' * (n - 1))
getans('SS' + '.' * (n - 1))

print(-1)

Submission status: Accepted

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


def check(t):
    t = t[-1] + t + t[0]
    for i in range(1, n + 1):
        j = i - 1
        if t[i] == 'S':
            consumed = 'o' if t[i - 1] == t[i + 1] else 'x'
        else:
            consumed = 'o' if t[i - 1] != t[i + 1] else 'x'
        if consumed == s[j]: continue
        return
    print(t[1:n+1])
    import sys
    sys.exit()

def getans(ans):
    ans = list(ans)
    for i in range(1, n):
        ans[i + 1] = 'S' if (ans[i] == 'S') ^ (s[i] == 'o') ^ (ans[i - 1] == 'S') else 'W'
        # if ans[i] == 'S':
        #     if s[i] == 'o':
        #         ans[i + 1] = 'S' if ans[i - 1] == 'S' else 'W'
        #     else:
        #         ans[i + 1] = 'W' if ans[i - 1] == 'S' else 'S'
        # else:
        #     if s[i] == 'x':
        #         ans[i + 1] = 'S' if ans[i - 1] == 'S' else 'W'
        #     else:
        #         ans[i + 1] = 'W' if ans[i - 1] == 'S' else 'S'

    check("".join(ans)[:-1])


getans('WW' + '.' * (n - 1))
getans('WS' + '.' * (n - 1))
getans('SW' + '.' * (n - 1))
getans('SS' + '.' * (n - 1))

print(-1)

Submission status: Accepted

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


def check(t):
    # print('check', t)
    t = t[-1] + t + t[0]
    for i in range(1, n + 1):
        j = i - 1
        # if (t[i] == 'S') ^ (s[j] == 'o') ^ (t[i - 1] != t[i + 1]): continue
        if t[i] == 'S':
            consumed = 'o' if t[i - 1] == t[i + 1] else 'x'
        else:
            consumed = 'o' if t[i - 1] != t[i + 1] else 'x'

        if consumed == s[j]: continue

        # print('ng i=',i,t[i-1:i+2],s[j])
        return
    # print('ok')
    print(t[1:n+1])
    import sys
    sys.exit()

def getans(ans):
    ans = list(ans)
    for i in range(1, n):
        if ans[i] == 'S':
            if s[i] == 'o':
                ans[i + 1] = 'S' if ans[i - 1] == 'S' else 'W'
            else:
                ans[i + 1] = 'W' if ans[i - 1] == 'S' else 'S'
        else:
            if s[i] == 'x':
                ans[i + 1] = 'S' if ans[i - 1] == 'S' else 'W'
            else:
                ans[i + 1] = 'W' if ans[i - 1] == 'S' else 'S'

    # if ans[0] == ans[-1]:
    #     print("".join(ans)[:-1])
    #     import sys
    #     sys.exit()
    check("".join(ans)[:-1])


getans('WW' + '.' * (n - 1))
getans('WS' + '.' * (n - 1))
getans('SW' + '.' * (n - 1))
getans('SS' + '.' * (n - 1))

print(-1)

Submission status: Accepted

Solution code:
N = int(input())
S = [1 if a == 'o' else 0 for a in list(input())]
S.append(S[0])

def check(a0, a1):
    dp = [None]*(N+2) #TrueならSheep, FalseならWolf
    dp[0] = a0
    dp[1] = a1
    for i in range(2, N+2):
        if dp[i-2] and dp[i-1]:
            dp[i] = True if S[i-1] == 1 else False
        if dp[i-2] and not dp[i-1]:
            dp[i] = True if S[i-1] == 0 else False
        if not dp[i-2] and dp[i-1]:
            dp[i] = True if S[i-1] == 0 else False
        if not dp[i-2] and not dp[i-1]:
            dp[i] = True if S[i-1] == 1 else False
    return dp


def main():
    ok = False
    for a0 in [True, False]:
        for a1 in [True, False]:
            dp = check(a0, a1)
            if (dp[N] == a0) and (dp[N+1] == a1):
                ok = True
                break
        if ok:
            break
    if not ok:
        print(-1)
    else:
        ans = ['S' if dp[i] else 'W' for i in range(N)]
        print(''.join(ans))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
s = input()
ls = ['SS', 'SW', 'WS', 'WW']
for l in ls:
  ans = []
  m = [0]*N
  m[0] = l[0]
  m[1] = l[1]
  for i in range(1,N):
    
    if m[i]=='S':
      flag = True
    else:
      flag = False
      
    if i==N-1:
      if flag:
        if s[i]=='o':
          if m[0]==m[N-2]:
            ans = m
            break
        else:
          if m[0]!=m[N-2]:
            ans = m
            break
      else:
        if s[i]=='o':
          if m[0]!=m[N-2]:
            ans = m
            break
        else:
          if m[0]==m[N-2]:
            ans = m
            break
      break
          
    if flag:
      if s[i]=='o':
        m[i+1] = m[i-1]
      else:
        if m[i-1]=='S':
          m[i+1] = 'W'
        else:
          m[i+1] = 'S'
    else:
      if s[i]=='o':
        if m[i-1]=='S':
          m[i+1] = 'W'
        else:
          m[i+1] = 'S'
      else:
        m[i+1] = m[i-1]
        
  if ans:
    if ans[0]=='S':
      if s[0]=='o':
        if ans[1]==ans[N-1]:
          break
        else:
          ans = []
          continue
      else:
        if ans[1]!=ans[N-1]:
          break
        else:
          ans = []
          continue
    else:
      if s[0]=='o':
        if ans[1]!=ans[N-1]:
          break
        else:
          ans = []
          continue
      else:
        if ans[1]==ans[N-1]:
          break
        else:
          ans = []
          continue
if ans:
  print(''.join(ans))
else:
  print(-1)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
s += s[0]

T = ['' for _ in range(N+2)]
for T[0],T[1] in [['S','S'],['S','W'],['W','S'],['W','W']]:
  for i in range(2,N+2):
    if (T[i-1] == 'S' and s[i-1] == 'o') or (T[i-1] == 'W' and s[i-1] == 'x'):
      T[i] = T[i-2]
    else:
      if T[i-2] == 'S':
        T[i] = 'W'
      else:
        T[i] = 'S'
  #print(T)
  if T[:2] == T[-2:]:
    print(''.join(T[:N]))
    exit()
print(-1)

Submission status: Accepted

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

T = ['' for _ in range(N+1)]
for T[0],T[1] in [['S','S'],['S','W'],['W','S'],['W','W']]:
  for i in range(2,N+1):
    if (T[i-1] == 'S' and s[i-1] == 'o') or (T[i-1] == 'W' and s[i-1] == 'x'):
      T[i] = T[i-2]
    else:
      if T[i-2] == 'S':
        T[i] = 'W'
      else:
        T[i] = 'S'
  #print(T)
  if (T[0] == T[N]) and ((T[0] == 'S' and s[0] == 'o' and T[1] == T[N-1]) or (T[0] == 'S' and s[0] == 'x' and T[1] != T[N-1]) or (T[0] == 'W' and s[0] == 'o' and T[1] != T[N-1]) or (T[0] == 'W' and s[0] == 'x' and T[1] == T[N-1])):
    print(''.join(T[:N]))
    exit()
print(-1)

Submission status: Accepted

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

#XX: SS or SW or WW or WS
#X:0 or 1
def checkNext(XX,X):
  dicNext={"SS":["S","W"],
          "SW":["W","S"],
           "WW":["S","W"],
           "WS":["W","S"]          
          }
  
  if X=="o":xNum=0
  else:xNum=1
  return dicNext[XX][xNum]

start=["SS","SW","WW","WS"]
for item in start:
  result=item
  for ox in s[1:-1]:
    result=result+checkNext(result[-2:],ox)
  #print(item,result)
  if checkNext(result[-2:],s[-1])==result[0]:
    if checkNext(result[-1]+result[0],s[0])==result[1]:
      print(result)
      exit()
print(-1)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
s += s[0]

testcase = ((0, 0), (0, 1), (1, 0), (1, 1))  # 0: sheep, 1: wolf

for p0, n0 in testcase:
    p, n = p0, n0
    ans = []
    for i in range(1, N+1):
        ans.append(p)
        if n == 0 and s[i] == 'o':
            p, n = n, p
        elif n == 0 and s[i] == 'x':
            p, n = n, 1-p
        elif n == 1 and s[i] == 'o':
            p, n = n, 1-p
        elif n == 1 and s[i] == 'x':
            p, n = n, p
    if (p, n) == (p0, n0):
        a = ['S' if x == 0 else 'W' for x in ans]
        print(''.join(a))
        exit()
print(-1)

Submission status: Accepted

Solution code:
import itertools
n = int(input())
s = input()
a = [""] * (n+1)
r = ["S", "W"]

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

for i in list(itertools.product(r, r)): 
  a[0] = i[0]
  a[1] = i[1]
  for j in range(1, n): 
    if s[j] == "o":
      if a[j] == "S":
        a[j+1] = a[j-1]
      else:
        a[j+1] = rev(a[j-1])
    else:
      if a[j] == "S":
        a[j+1] = rev(a[j-1])
      else:
        a[j+1] = a[j-1]
  if a[0] == a[-1]:
    if s[0] == "o":
      if a[0] == "S":
        if a[1] == a[-2]:
          print("".join(a[:-1]))
          exit()
      else:
        if a[1] != a[-2]:
          print("".join(a[:-1]))
          exit()
    else:
      if a[0] == "S":
        if a[1] != a[-2]:
          print("".join(a[:-1]))
          exit()
      else:
        if a[1] == a[-2]:
          print("".join(a[:-1]))
          exit()
print(-1)

Submission status: Accepted

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

def check(a:list):
    for i in range(1,n):
        if a[i] == "S":
            if s[i] == "o":
                a.append(a[i-1])
            else:
                if a[i-1] == "S":
                    a.append("W")
                else:
                    a.append("S")
        if a[i] == "W":
            if s[i] == "o":
                if a[i-1] == "S":
                    a.append("W")
                else:
                    a.append("S")
            else:
                a.append(a[i-1])
    if a[0] != a[-1]:
        return 0
    else:
        a.pop()
        if a[1] == a[-1]:
            return "".join(a) if (a[0] == "S" and s[0] == "o") or (a[0] == "W" and s[0] == "x") else 0
        else:
            return "".join(a) if (a[0] == "S" and s[0] == "x") or (a[0] == "W" and s[0] == "o") else 0 

if check(["S","S"]):
    print(check(["S","S"]))
    exit()

if check(["S","W"]):
    print(check(["S","W"]))
    exit()

if check(["W","S"]):
    print(check(["W","S"]))
    exit()

if check(["W","W"]):
    print(check(["W","W"]))
    exit()

print(-1)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

def check(x):
    res[0] = x[0]
    res[1] = x[1]
    for i in range(1,n-1):
        if (res[i] == 'S' and s[i] == 'o') or (res[i] == 'W' and s[i] == 'x'):
            if res[i-1] == 'S':
                res[i+1] = 'S'
            else:
                res[i+1] = 'W'
        else:
            if res[i-1] == 'S':
                res[i+1] = 'W'
            else:
                res[i+1] = 'S'
    # print(res)
    if (res[n-1] == 'S' and s[n-1] == 'o') or (res[n-1] == 'W' and s[n-1] == 'x'):
        if res[n-2] != res[0]:
            return False
    else:
        if res[n-2] == res[0]:
            return False
    if (res[0] == 'S' and s[0] == 'o') or (res[0] == 'W' and s[0] == 'x'):
        if res[n-1] != res[1]:
            return False
    else:
        if res[n-1] == res[1]:
            return False
    return res

n = inp()
s = input()
a = [['S','S'],['S','W'],['W','S'],['W','W']]
res = [''] * n
# print(a)
for i in range(4):
    if check(a[i]):
        print(''.join(res))
        quit()
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
s = list(map(int,list(input().replace("o","1").replace("x","0"))))
for bit in range(4):
  ani = [None]*n
  ani[0] = bit%2
  ani[1] = bit//2
  for i in range(1,n-1):
    ani[i+1] = ani[i-1]^ani[i]^s[i]
  if ani[0] == ani[n-2]^ani[n-1]^s[n-1] and ani[1] == ani[-1]^ani[0]^s[0]:
    print("".join(list(map(str,ani))).replace("0","W").replace("1","S"))
    break
else:
  print(-1)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    s = input()
    o_ = {"SS": "S", "WS": "W", "SW": "W", "WW": "S"}
    x_ = {"SS": "W", "WS": "S", "SW": "S", "WW": "W"}
    ok = False
    for ans in ["SW", "WS", "SS", "WW"]:
        for i in range(1, N):
            if s[i] == "o": # oの時
                ans += o_[ans[-2:]]
            else:           # xの時
                ans += x_[ans[-2:]]
    
        if ans[0] == ans[-1]:
            if s[0] == "o":
                if o_[ans[-2:]] == ans[1]:
                    ok = True
            else:
                if x_[ans[-2:]] == ans[1]:
                    ok = True
        if ok:
            print(ans[:-1])
            break
    if not ok:
        print(-1)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    s = input()
    c = ["SW", "WS", "SS", "WW"]
    o_ = {"SS": "S", "WS": "W", "SW": "W", "WW": "S"}
    x_ = {"SS": "W", "WS": "S", "SW": "S", "WW": "W"}
    _o_ = ["SSS", "WSW", "SWW", "WWS"]
    _x_ = ["WSS", "SSW", "SWS", "WWW"]
    ok = False
    for tmp in c:
        ans = tmp
        for i in range(1, N):
            if s[i] == "o": # oの時
                ans += o_[ans[-2:]]
            else:           # xの時
                ans += x_[ans[-2:]]
    
        if ans[0] == ans[-1]:
            jud = ans[-2:] + ans[1]
            if s[0] == "o":
                if jud in _o_:
                    ok = True
            else:
                if jud in _x_:
                    ok = True
        if ok:
            print(ans[:-1])
            break
    if not ok:
        print(-1)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    s = input()
    # c = ["WS", "SW", "SS", "WW"]
    c = ["SW", "WS", "SS", "WW"]
    ok = False
    for tmp in c:
        ans = tmp
        for i in range(1, N):
            if s[i] == "o":# oの時
                if ans[i-1] == "S" and ans[i] == "S":
                    ans += "S"
                elif ans[i-1] == "W" and ans[i] == "S":
                    ans += "W"
                elif ans[i-1] == "S" and ans[i] == "W":
                    ans += "W"
                elif ans[i-1] == "W" and ans[i] == "W":
                    ans += "S"
            else:# xの時
                if ans[i-1] == "S" and ans[i] == "S":
                    ans += "W"
                elif ans[i-1] == "W" and ans[i] == "S":
                    ans += "S"
                elif ans[i-1] == "S" and ans[i] == "W":
                    ans += "S"
                elif ans[i-1] == "W" and ans[i] == "W":
                    ans += "W"
    
        if ans[0] == ans[-1]:
            if ans[0] == "S":
                if s[0] == "o":
                    if (ans[-2] == "S" and ans[1] == "S") or (ans[-2] == "W" and ans[1] == "W"):
                        ok = True
                else:
                    if (ans[-2] == "S" and ans[1] == "W") or (ans[-2] == "W" and ans[1] == "S"):
                        ok = True
            else:
                if s[0] == "o":
                    if (ans[-2] == "S" and ans[1] == "W") or (ans[-2] == "W" and ans[1] == "S"):
                        ok = True
                else:
                    if (ans[-2] == "S" and ans[1] == "S") or (ans[-2] == "W" and ans[1] == "W"):
                        ok = True
        if ok:
            print(ans[:-1])
            # print(len(ans))
            break
    if not ok:
        print(-1)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ARC069D - Menagerie (ABC055D)
def check(a: int, b: int) -> list:
    ret, cur, prev = [], a, b
    for i in S:
        ret += [cur]
        if cur ^ (i == "o"):  # oSo or oWx
            prev, cur = cur, prev ^ 1  # both neighbors are different
        else:  # oSx or oWo
            prev, cur = cur, prev  # both are the same
    flg = cur == a and prev == b  # check the consistency of input and result
    return ret if flg else []


def main():
    global S
    N = int(input())
    S = input().rstrip()
    for a in range(2):
        for b in range(2):
            x = check(a, b)
            if x:
                ans = "".join("S" if i else "W" for i in x)
                print(ans)
                return
    print(-1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ARC069D - Menagerie (ABC055D)
def check(a: int, b: int) -> list:
    ret, cur, prev = [], a, b
    for i in S:
        ret += [cur]
        if cur ^ (i == "o"):  # oSo or oWx
            prev, cur = cur, prev ^ 1  # both neighbors are different
        else:  # oSx or oWo
            prev, cur = cur, prev  # both are the same
    flg = cur == a and prev == b  # check the consistency of input and result
    return ret if flg else []


def main():
    global S
    N = int(input())
    S = input().rstrip()
    for a in range(2):
        for b in range(2):
            ans = check(a, b)
            if ans:
                print("".join("S" if i else "W" for i in ans))
                return
    print(-1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ARC069D - Menagerie (ABC055D)
def check(a: int, b: int, S: str) -> list:
    ret, cur, prev = [], a, b
    for i in S:
        ret += [cur]
        if cur ^ (i == "o"):  # oSo or oWx
            prev, cur = cur, prev ^ 1  # both neighbors are different
        else:  # oSx or oWo
            prev, cur = cur, prev  # both are the same
    flg = cur == a and prev == b  # check the consistency of input and result
    return ret if flg else []


def main():
    N = int(input())
    S = input().rstrip()
    for a in range(2):
        for b in range(2):
            ans = check(a, b, S)
            if ans:
                print("".join("S" if i else "W" for i in ans), sep="")
                return
    print(-1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ARC069D - Menagerie (ABC055D)
def check(a: int, b: int) -> list:
    ret, cur, prev = [], a, b
    for i in S:
        ret += [cur]
        if cur ^ (i == "o"):
            prev, cur = cur, prev ^ 1  # both neighbors are different
        else:
            prev, cur = cur, prev  # both are the same
    flg = cur == a and prev == b  # check the consistency of input and result
    return ret if flg else []


def main():
    global S
    N = int(input())
    S = input().rstrip()
    for a in range(2):
        for b in range(2):
            ans = check(a, b)
            if ans:
                print("".join("S" if i else "W" for i in ans), sep="")
                return
    print(-1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ARC069D - Menagerie (ABC055D)
def check(a: str, b: str, S: str) -> str:
    ret, cur, prev = "", a, b
    for i in S:
        ret += cur
        if (cur == "S" and i == "o") or (cur == "W" and i == "x"):
            prev, cur = cur, prev  # both neighbors are the same
        elif (cur == "S" and i == "x") or (cur == "W" and i == "o"):
            x = "W" if prev == "S" else "S"  # both are different
            prev, cur = cur, x
    flg = cur == a and prev == b  # check the consistency of input and result
    return ret if flg else ""


def main():
    N = int(input())
    S = input().rstrip()
    for a in ("S", "W"):
        for b in ("S", "W"):
            ans = check(a, b, S)
            if ans:
                print(ans)
                return
    print(-1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ARC069D - Menagerie (ABC055D)
def check(a: str, b: str) -> str:
    ret, cur, prev = "", a, b
    for i in S:
        ret += cur
        if (cur == "S" and i == "o") or (cur == "W" and i == "x"):
            prev, cur = cur, prev
        elif (cur == "S" and i == "x") or (cur == "W" and i == "o"):
            x = "W" if prev == "S" else "S"
            prev, cur = cur, x
    flg = cur == a and prev == b
    return ret if flg else ""


def main():
    global S
    N = int(input())
    S = input().rstrip()
    for a in ("S", "W"):
        for b in ("S", "W"):
            ans = check(a, b)
            if ans:
                print(ans)
                return
    print(-1)


if __name__ == "__main__":
    main()