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 = list(input())
u = ["S", "W"]
for i in range(2):
    for j in range(2):
        ans = [0] * n
        ans[0] = u[i]
        ans[1] = u[j]
        for k in range(2, n):
            if s[k - 1] == "o":
                if ans[k - 1] == "S":
                    if ans[k - 2] == "S":
                        c = 0
                    else:
                        c = 1
                else:
                    if ans[k - 2] == "S":
                        c = 1
                    else:
                        c = 0
            else:
                if ans[k - 1] == "S":
                    if ans[k - 2] == "S":
                        c = 1
                    else:
                        c = 0
                else:
                    if ans[k - 2] == "S":
                        c = 0
                    else:
                        c = 1
            ans[k] = u[c]
        m = 0
        if s[0] == "o":
            if ans[0] == "S":
                if ans[1] == ans[n - 1]:
                    m += 1
            else:
                if ans[1] != ans[n - 1]:
                    m += 1
        else:
            if ans[0] == "W":
                if ans[1] == ans[n - 1]:
                    m += 1
            else:
                if ans[1] != ans[n - 1]:
                    m += 1
        if s[n - 1] == "o":
            if ans[n - 1] == "S":
                if ans[0] == ans[n - 2]:
                    m += 1
            else:
                if ans[0] != ans[n - 2]:
                    m += 1
        else:
            if ans[n - 1] == "W":
                if ans[0] == ans[n - 2]:
                    m += 1
            else:
                if ans[0] != ans[n - 2]:
                    m += 1
        if m == 2:
            print("".join(ans))
            exit()
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
seed1 = ["S","W"]
ans = ["SS","SW","WS","WW"]
for i in s[1:]:
    for k,v in enumerate(ans):
        now = 0 if i == "o" else 1
        now = (now + seed1.index(v[-1]) + seed1.index(v[-2])) % 2
        if now:
            ans[k] += "W"
        else:
            ans[k] += "S"
now = 0 if s[0] == "x" else 1
for i in ans:  
    now2 = (now + seed1.index(i[-2]) + seed1.index(i[1]) + seed1.index(i[0])) % 2
    if (i[0] == i[-1]) and now2:
        print(i[:-1])
        exit()
print(-1)

Submission status: Accepted

Solution code:
import sys
n=int(input())
s = list(input())
l = list(0 for i in range(n))
l[0]='S'
l[1]='S'
for i in range(1,n-1):
    if l[i]=='S':
        if l[i-1]=='S':
            if (s[i]=='o'):
                l[i+1]='S'
            else:
                l[i+1]='W'
        else:
            if s[i]=='o':
                l[i+1]='W'
            else:
                l[i+1]='S'
    else:
        if l[i-1]=='S':
            if (s[i]=='o'):
                l[i+1]='W'
            else:
                l[i+1]='S'
        else:
            if s[i]=='o':
                l[i+1]='S'
            else:
                l[i+1]='W'
if l[n-1]=='S':
    if l[n-2]=='S':
        if (s[n-1]=='o'):
            if l[0]=='S':
                if s[0]=='o':
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()  
        else:
            if l[0]=='W':
                if s[0]=='o':
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
    else:
        if s[n-1]=='o':
            if l[0]=='W':
                if s[0]=='o':
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
        else:
            if l[0]=='S':
                if s[0]=='o':
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
else:
    if l[n-2]=='S':
        if (s[n-1]=='o'):
            if l[0]=='W':
                if s[0]=='o':
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
        else:
            if l[0]=='S':
                if s[0]=='o':
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()   
    else:
        if s[n-1]=='o':
            if l[0]=='S':
                if s[0]=='o':
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()  
        else:
            if l[0]=='W':
                if s[0]=='o':
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()   
l[0]='S'
l[1]='W'
for i in range(1,n-1):
    if l[i]=='S':
        if l[i-1]=='S':
            if (s[i]=='o'):
                l[i+1]='S'
            else:
                l[i+1]='W'
        else:
            if s[i]=='o':
                l[i+1]='W'
            else:
                l[i+1]='S'
    else:
        if l[i-1]=='S':
            if (s[i]=='o'):
                l[i+1]='W'
            else:
                l[i+1]='S'
        else:
            if s[i]=='o':
                l[i+1]='S'
            else:
                l[i+1]='W'
if l[n-1]=='S':
    if l[n-2]=='S':
        if (s[n-1]=='o'):
            if l[0]=='S':
                if s[0]=='o':
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()  
        else:
            if l[0]=='W':
                if s[0]=='o':
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
    else:
        if s[n-1]=='o':
            if l[0]=='W':
                if s[0]=='o':
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
        else:
            if l[0]=='S':
                if s[0]=='o':
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
else:
    if l[n-2]=='S':
        if (s[n-1]=='o'):
            if l[0]=='W':
                if s[0]=='o':
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
        else:
            if l[0]=='S':
                if s[0]=='o':
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()   
    else:
        if s[n-1]=='o':
            if l[0]=='S':
                if s[0]=='o':
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()  
        else:
            if l[0]=='W':
                if s[0]=='o':
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()   
l[0]='W'
l[1]='S'
for i in range(1,n-1):
    if l[i]=='S':
        if l[i-1]=='S':
            if (s[i]=='o'):
                l[i+1]='S'
            else:
                l[i+1]='W'
        else:
            if s[i]=='o':
                l[i+1]='W'
            else:
                l[i+1]='S'
    else:
        if l[i-1]=='S':
            if (s[i]=='o'):
                l[i+1]='W'
            else:
                l[i+1]='S'
        else:
            if s[i]=='o':
                l[i+1]='S'
            else:
                l[i+1]='W'
if l[n-1]=='S':
    if l[n-2]=='S':
        if (s[n-1]=='o'):
            if l[0]=='S':
                if s[0]=='o':
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()  
        else:
            if l[0]=='W':
                if s[0]=='o':
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
    else:
        if s[n-1]=='o':
            if l[0]=='W':
                if s[0]=='o':
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
        else:
            if l[0]=='S':
                if s[0]=='o':
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
else:
    if l[n-2]=='S':
        if (s[n-1]=='o'):
            if l[0]=='W':
                if s[0]=='o':
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
        else:
            if l[0]=='S':
                if s[0]=='o':
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()   
    else:
        if s[n-1]=='o':
            if l[0]=='S':
                if s[0]=='o':
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()  
        else:
            if l[0]=='W':
                if s[0]=='o':
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()   
l[0]='W'
l[1]='W'
for i in range(1,n-1):
    if l[i]=='S':
        if l[i-1]=='S':
            if (s[i]=='o'):
                l[i+1]='S'
            else:
                l[i+1]='W'
        else:
            if s[i]=='o':
                l[i+1]='W'
            else:
                l[i+1]='S'
    else:
        if l[i-1]=='S':
            if (s[i]=='o'):
                l[i+1]='W'
            else:
                l[i+1]='S'
        else:
            if s[i]=='o':
                l[i+1]='S'
            else:
                l[i+1]='W'
if l[n-1]=='S':
    if l[n-2]=='S':
        if (s[n-1]=='o'):
            if l[0]=='S':
                if s[0]=='o':
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
        else:
            if l[0]=='W':
                if s[0]=='o':
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
    else:
        if s[n-1]=='o':
            if l[0]=='W':
                if s[0]=='o':
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
        else:
            if l[0]=='S':
                if s[0]=='o':
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
else:
    if l[n-2]=='S':
        if (s[n-1]=='o'):
            if l[0]=='W':
                if s[0]=='o':
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
        else:
            if l[0]=='S':
                if s[0]=='o':
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
    else:
        if s[n-1]=='o':
            if l[0]=='S':
                if s[0]=='o':
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
        else:
            if l[0]=='W':
                if s[0]=='o':
                    if l[n-1]!=l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()
                else:
                    if l[n-1]==l[1]:
                        l=''.join(l)
                        print(l)
                        sys.exit()    
print(-1)

Submission status: Accepted

Solution code:
N=int(input())
S=list(input().rstrip())
for i in range(N):
  if S[i]=='o': S[i]=True
  else: S[i]=False
S.append(S[0])
A=[True]*(N+2)
for a in [[True,True],[True,False],[False,True],[False,False]]:
  A[0],A[1]=a[0],a[1]
  for j in range(1,N+1):
    if A[j] ^ S[j]:
      A[j+1]=not A[j-1]
    else:
      A[j+1]=A[j-1]
  if A[0]==A[-2] and A[1]==A[-1]:
    for k in range(N):
      if A[k]: A[k]='S'
      else: A[k]='W'
    print("".join(A[:N]))
    exit()
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
from itertools import product

for v in product([1,0], repeat=2):
    v = list(v)
    for i in range(n-2):
        if v[i+1]:
            if s[i+1] == 'o':
                v.append(v[i])
            else:
                v.append(not v[i])
        else:
            if s[i+1] == 'o':
                v.append(not v[i])
            else:
                v.append(v[i])
    ans = [0,0]
    for i in range(-1,1):
        if v[i]:
            if s[i] == 'o':
                if v[i-1] == v[i+1]:
                    ans[i+1] = 1
                else:
                    ans[i+1] = 0
            else:
                if v[i-1] == v[i+1]:
                    ans[i+1] = 0
                else:
                    ans[i+1] = 1
        else:
            if s[i] == 'o':
                if v[i-1] == v[i+1]:
                    ans[i+1] = 0
                else:
                    ans[i+1] = 1
            else:
                if v[i-1] == v[i+1]:
                    ans[i+1] = 1
                else:
                    ans[i+1] = 0
    if ans[0] and ans[1]:
        print(''.join(['S' if x == 1 else 'W' for x in v]))
        exit()
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
from itertools import product

for v in product([1,0], repeat=2):
    v = list(v)
    for i in range(n-2):
        if v[i+1]:
            if s[i+1] == 'o':
                v.append(v[i])
            else:
                v.append(int(not v[i]))
        else:
            if s[i+1] == 'o':
                v.append(int(not v[i]))
            else:
                v.append(v[i])
    ans = [0,0]
    for i in range(-1,1):
        if v[i]:
            if s[i] == 'o':
                if v[i-1] == v[i+1]:
                    ans[i+1] = 1
                else:
                    ans[i+1] = 0
            else:
                if v[i-1] != v[i+1]:
                    ans[i+1] = 1
                else:
                    ans[i+1] = 0
        else:
            if s[i] == 'o':
                if v[i-1] == v[i+1]:
                    ans[i+1] = 0
                else:
                    ans[i+1] = 1
            else:
                if v[i-1] != v[i+1]:
                    ans[i+1] = 0
                else:
                    ans[i+1] = 1
    if ans[0] and ans[1]:
        for i in range(n):
            if v[i]:
                v[i] = 'S'
            else:
                v[i] = 'W'
        print(''.join(v))
        exit()
print(-1)

Submission status: Accepted

Solution code:
from itertools import product

N = int(input())
S = input()
for s1, s2 in product('SW', repeat=2):
    temp = s1 + s2
    for s in S[1:N-1]:
        if (s == 'o' and temp[-1] == 'S') or (s == 'x' and temp[-1] == 'W'):
            temp += temp[-2]
        else:
            temp += ''.join(set('SW') - set(temp[-2]))
    for i in [N-1, 0]:
        if S[i] == 'o' and temp[i] == 'S' and temp[(i-1) % N] == temp[(i+1) % N]: continue
        elif S[i] == 'o' and temp[i] == 'W' and temp[(i-1) % N] != temp[(i+1) % N]: continue
        elif S[i] == 'x' and temp[i] == 'S' and temp[(i-1) % N] != temp[(i+1) % N]: continue
        elif S[i] == 'x' and temp[i] == 'W' and temp[(i-1) % N] == temp[(i+1) % N]: continue
        else: break
    else:
        print(temp); break
else:
    print(-1)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
def check(s1,s2):
  ans=[s1,s2]
  for i in range(1,n-1):
    if (ans[i]=='S' and s[i]=='o') or (ans[i]=='W' and s[i]=='x'):
      ans.append(ans[i-1])
    elif ans[i]=='S' and s[i]=='x' and ans[i-1]=='S':
      ans.append('W')
    elif ans[i]=='S' and s[i]=='x' and ans[i-1]=='W':
      ans.append('S')
    elif ans[i]=='W' and s[i]=='o' and ans[i-1]=='S':
      ans.append('W')
    elif ans[i]=='W' and s[i]=='o' and ans[i-1]=='W':
      ans.append('S')
  if ans[-1]=='S':
    if s[-1]=='o' and ans[0]==ans[-2]:
      if ans[0]=='S' and s[0]=='o' and ans[1]==ans[-1]:
        print(''.join(ans))
        exit()
      if ans[0]=='S' and s[0]=='x' and ans[1]!=ans[-1]:
        print(''.join(ans))
        exit()
      if ans[0]=='W' and s[0]=='o' and ans[1]!=ans[-1]:
        print(''.join(ans))
        exit()
      if ans[0]=='W' and s[0]=='x' and ans[1]==ans[-1]:
        print(''.join(ans))
        exit()
    if s[-1]=='x' and ans[0]!=ans[-2]:
      if ans[0]=='S' and s[0]=='o' and ans[1]==ans[-1]:
        print(''.join(ans))
        exit()
      if ans[0]=='S' and s[0]=='x' and ans[1]!=ans[-1]:
        print(''.join(ans))
        exit()
      if ans[0]=='W' and s[0]=='o' and ans[1]!=ans[-1]:
        print(''.join(ans))
        exit()
      if ans[0]=='W' and s[0]=='x' and ans[1]==ans[-1]:
        print(''.join(ans))
        exit()
  else:
    if s[-1]=='o' and ans[0]!=ans[-2]:
      if ans[0]=='S' and s[0]=='o' and ans[1]==ans[-1]:
        print(''.join(ans))
        exit()
      if ans[0]=='S' and s[0]=='x' and ans[1]!=ans[-1]:
        print(''.join(ans))
        exit()
      if ans[0]=='W' and s[0]=='o' and ans[1]!=ans[-1]:
        print(''.join(ans))
        exit()
      if ans[0]=='W' and s[0]=='x' and ans[1]==ans[-1]:
        print(''.join(ans))
        exit()
    if s[-1]=='x' and ans[0]==ans[-2]:
      if ans[0]=='S' and s[0]=='o' and ans[1]==ans[-1]:
        print(''.join(ans))
        exit()
      if ans[0]=='S' and s[0]=='x' and ans[1]!=ans[-1]:
        print(''.join(ans))
        exit()
      if ans[0]=='W' and s[0]=='o' and ans[1]!=ans[-1]:
        print(''.join(ans))
        exit()
      if ans[0]=='W' and s[0]=='x' and ans[1]==ans[-1]:
        print(''.join(ans))
        exit()
  return
check('S','S')
check('S','W')
check('W','S')
check('W','W')
print(-1)

Submission status: Accepted

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]

n=int(input())
s=input()
lst=[[0,0],[0,1],[1,0],[1,1]]

for i in range(n-1):
    for j in range(4):
        if lst[j][-1]==0 and s[1+i]=="o":
            lst[j].append(lst[j][-2])
        elif lst[j][-1]==0 and s[1+i]=="x":
            lst[j].append(1-lst[j][-2])
        elif lst[j][-1]==1 and s[1+i]=="o":
            lst[j].append(1-lst[j][-2])
        else:
            lst[j].append(lst[j][-2])
for j in range(4):
    if lst[j][-1]==0 and s[0]=="o":
        lst[j].append(lst[j][-2])
    elif lst[j][-1]==0 and s[0]=="x":
        lst[j].append(1-lst[j][-2])
    elif lst[j][-1]==1 and s[0]=="o":
        lst[j].append(1-lst[j][-2])
    else:
        lst[j].append(lst[j][-2])
# print(lst)

for i in range(4):
    if lst[i][-2:]==lst[i][:2]:
        tmp=[]
        for j in range(n):
            if lst[i][j]==0:
                tmp.append("S")
            else:
                tmp.append("W")
        print("".join(tmp))
        quit()
print(-1)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
#print(s)
for a in [0,1]:
    for b in [0,1]:
        #0 means it's a false speaking animal
        #1 means it's a truth speaking animal
        ass=[-1]*n
        ass[0]=a
        ass[1]=b
        #we know about the assignment of first two animals
        for i in range(1,n-1):
            if ass[i]==0:#false speaking animal
                if s[i]=='o':
                    ass[i+1]=1-ass[i-1]
                else:
                    ass[i+1]=ass[i-1]
            else:#true speaking animal
                if s[i]=='o':
                    ass[i+1]=ass[i-1]
                else:
                    ass[i+1]=1-ass[i-1]
#        print(ass)
        if ass[0]==1:
            if (s[0]=='o' and ass[-1]!=ass[1]) or (s[0]=='x' and ass[-1]==ass[1]):
                continue
        else:
            if (s[0]=='o' and ass[-1]==ass[1]) or (s[0]=='x' and ass[-1]!=ass[1]):
                continue
        if ass[-1]==1:
            if (s[-1]=='o' and ass[n-2]!=ass[0]) or (s[-1]=='x' and ass[n-2]==ass[0]):
                continue
        else:
            if (s[-1]=='o' and ass[n-2]==ass[0]) or (s[-1]=='x' and ass[n-2]!=ass[0]):
                continue
        ass=['W' if i==0 else 'S' for i in ass]
        print(''.join(ass))
        exit()
print(-1)

Submission status: Accepted

Solution code:
import itertools

N = int(input())
S = [True if x == 'o' else False for x in input()]
S.append(S[0])
S.append(S[1])
lst = [None] * (N + 2)
for x0, x1 in itertools.product((True, False), repeat=2):
    lst[0], lst[1] = x0, x1
    for i in range(1, N + 1):
        lst[i + 1] = lst[i] ^ S[i] ^ lst[i - 1]
    if (lst[N], lst[N + 1]) == (lst[0], lst[1]):
        print(''.join(map(str, ('S' if x else 'W' for x in lst[:-2]))))
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
def solve(n, S):
    for i in range(4):
        ans = list()
        t = i
        for j, s in enumerate(S):
            f0 = t // 2
            f1 = t % 2
            f2 = f0 ^ (0 if s == 'o' else 1)
            ans.append('S' if f2 == 0 else 'W')
            t = ((f1 ^ f2) << 1) | f2

        if (i == t) :
            x = ans[:-1]
            x.insert(0, ans[-1])
            return "".join(x)
    return "-1"

if __name__ == '__main__':
    print(solve(int(input()), input()))

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 = list(input())
ans = -1
for x in ['SS', 'SW', 'WS', 'WW']:
    for i, s in enumerate(S):
        if s=='o':
            if x[-1]=='S':
                x += x[-2]
            else:
                if x[-2]=='S':
                    x += 'W'
                else:
                    x += 'S'
        else:
            if x[-1] == 'S':
                if x[-2]=='S':
                    x += 'W'
                else:
                    x += 'S'
            else:
                x += x[-2]
    if x[0]==x[-2] and x[1]==x[-1]:
        ans = x[1:-1]
        break
print(ans)