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

Solution (Python):
n =int(input())
s = input()
cir = ['']*n
judge = ''
def convertSW(x):
  if x == 'S':
    return 'W'
  elif x == 'W':
    return 'S'
for i in range(4):
  if i == 0:
    cir[0],cir[1] = 'S','S'
  elif i == 1:
    cir[0],cir[1] = 'S','W'
  elif i == 2:
    cir[0],cir[1] = 'W','W'
  else:
    cir[0],cir[1] = 'W','S'
  if (s[0] == 'o' and cir[0] == 'S') or (s[0] == 'x' and cir[0] == 'W'):
    judge = cir[1]
  else:
    judge = convertSW(cir[1])
  for j in range(2,n):
    if (s[j-1] == 'o' and cir[j-1] == 'S') or (s[j-1] == 'x' and cir[j-1] == 'W'):
      cir[j] = cir[j-2]
    else:
      cir[j] = convertSW(cir[j-2])
  if (s[-1] == 'o' and cir[-1] == 'S') or (s[-1] == 'x' and cir[-1] == 'W'):
    if (cir[0] == cir[-2]) and (judge == cir[-1]):
      print(''.join(cir))
      exit()
  else:
    if cir[0] == convertSW(cir[-2]) and (judge == cir[-1]):
      print(''.join(cir))
      exit()
print(-1)

Solution (Python):
import sys

N=int(input())
S=input()

flgT=["SSS","SWW","WWS","WSW"]
flgF=["SSW","WSS","SWS","WWW"]

if S[0]=='o':
  for i in range(4):
    str=flgT[i]
    for j in range(1,N):
      if (S[j]=="o" and str[-1]=="S") or (S[j]=="x" and str[-1]=="W"):
        str=str+str[-2]
      else:
        if str[-2]=="W":
          str=str+"S"
        else:
          str=str+"W"
    if str[-2]==str[0] and str[-1]==str[1]:
      str=str[1:-1]
      print(str)
      sys.exit()
else:
  for i in range(4):
    str=flgF[i]
    for j in range(1,N):
      if (S[j]=="o" and str[-1]=="S") or (S[j]=="x" and str[-1]=="W"):
        str=str+str[-2]
      else:
        if str[-2]=="W":
          str=str+"S"
        else:
          str=str+"W"
    if str[-2]==str[0] and str[-1]==str[1]:
      str=str[1:-1]
      print(str)
      sys.exit()

print(-1)

Solution (Python):
N=input()
s=list(raw_input())

def judge(a,b):
	t=[]
	t.append(a)
	t.append(b)

	for ind, i in enumerate(s):
		if ind==0:
			continue

		if i=="o" and t[ind]=="S":
			t.append(t[ind-1])
		elif i=="o" and t[ind]=="W":
			if t[ind-1]=="S":
				t.append("W")
			elif t[ind-1]=="W":
				t.append("S")
		elif i=="x" and t[ind]=="S":
			if t[ind-1]=="S":
				t.append("W")
			elif t[ind-1]=="W":
				t.append("S")
		elif i=="x" and t[ind]=="W":
			t.append(t[ind-1])

	if t[0]==t[-1]:
		if s[0]=="o" and t[0]=="S":
			if t[1]==t[-2]:
				print "".join(t[:-1])
				quit()
		elif s[0]=="o" and t[0]=="W":
			if t[1]!=t[-2]:
				print "".join(t[:-1])
				quit()
		elif s[0]=="x" and t[0]=="S":
			if t[1]!=t[-2]:
				print "".join(t[:-1])
				quit()
		elif s[0]=="w" and t[0]=="W":
			if t[1]==t[-2]:
				print "".join(t[:-1])
				quit()			
	else:
		return False

judge("S","S")
judge("S","W")
judge("W","S")
judge("W","W")
print -1

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

pat1 = [-1] * N
pat2 = [-1] * N
pat3 = [-1] * N
pat4 = [-1] * N

pat1[1] = pat2[1] = 1
pat3[1] = pat4[1] = 0

if s[1] == 'o':
	pat1[0] = pat1[2] = 0
	pat2[0] = pat2[2] = 1
	pat3[0] = pat4[2] = 0
	pat3[2] = pat4[0] = 1

else:
	pat1[0] = pat2[2] = 0
	pat2[0] = pat1[2] = 1
	pat3[0] = pat3[2] = 0
	pat4[2] = pat4[0] = 1




for i in range(2,N-1):
	for lis in [pat1,pat2,pat3,pat4]:
		if lis[i] == 1:
			if s[i] == 'o':
				lis[i+1] = lis[i-1]

			else:
				lis[i+1] = (lis[i-1] + 1)%2
		else:
			if s[i] == 'x':
				lis[i+1] = lis[i-1]

			else:
				lis[i+1] = (lis[i-1] + 1)%2



def ok1(List):
	global s
	if List[N-1] == 1:
		if s[N-1] == 'o':
			return List[N-2] == List[0]
		else:
			return List[N-2] != List[0]

	else:
		if s[N-1] == 'x':
			return List[N-2] == List[0]
		else:
			return List[N-2] != List[0]

def ok2(List):
	global s
	if List[0] == 1:
		if s[0] == 'o':
			return List[N-1] == List[1]
		else:
			return List[N-1] != List[1]

	else:
		if s[0] == 'x':
			return List[N-1] == List[1]
		else:
			return List[N-1] != List[1]





flg = []
for lis in [pat1,pat2,pat3,pat4]:
	if ok1(lis) and ok2(lis):
		flg = lis
		break
	

ans = ""
if flg != []:
	for num in flg:
		if num ==  0:
			ans += 'W'
		else:
			ans += 'S'

	print(ans)

else:
	print(-1)

Solution (Python):
def d_Menagerie(N, S):
    s = S + S[0:2]  
    for ans in ['SS', 'SW', 'WS', 'WW']:  
        cond = False
        for i in range(1, N + 1):
            if (ans[-1] == 'S' and s[i] == 'o') or (ans[-1] == 'W' and s[i] == 'x'):
                
                
                ans += ans[-2]
            else:
                
                ans += ('S' if ans[-2] == 'W' else 'W')
        if ans[:2] == ans[N:]:
            
            
            cond = True
            ans = ans[:N]
            break
    
    if cond:
        return ans
    else:
        return '-1'
    return None

N = int(input())
S = input().strip()
print(d_Menagerie(N, S))

Solution (Python):
N = int(input())
S = input()
S += S[0:2]

for ans in ['SS','SW','WS','WW']:
    for i in range(1,N+1):
        if (ans[-1] == 'S' and S[i] == 'o') or (ans[-1] == 'W' and S[i] == 'x'):
            ans += ans[-2]
        else:
            ans += ('S' if ans[-2] == 'W' else 'W')
    if ans[:2] == ans[N:]:
        print(ans[:N])
        exit()
print(-1)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Solution (Python):
N = int(input())

s = input()

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

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

answered = False

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

Solution (Python):
from collections import defaultdict


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

    return True


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

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


if __name__ == '__main__':
    main()

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

sl = list(s)

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

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

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


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


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

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

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

Solution (Python):
import sys




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


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


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


def read_str():
    return input()


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

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


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


main()

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

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


def next_predict(before_state,current_state,output):
    if current_state == 1:
        if output == True:
            if before_state == 1:
                return 1
            else:
                return 0
        else:
            if before_state == 1:
                return 0
            else:
                return 1
    else:
        return next_predict(before_state,1,not(output))

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

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

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

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

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

print(-1)

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

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

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

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

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

print(-1)

Solution (Python):
N = int(raw_input())
s = raw_input()


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

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

print run()

Solution (Python):

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

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



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

    return ans

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

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

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

Solution (Python):


import sys
import os


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

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

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

    
    is_consistent = True
    for i in range(N):
        left = X[i-1]
        if i+1 <= N-1:
            right = X[i + 1]
        else:
            right = X[i + 1 - N]

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



X_list = [[0, 0], [0, 1], [1, 0], [1, 1]]

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

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

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

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

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

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

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

a = False
b = False

sw = [None] * n

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

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

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

flag = True

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

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

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

if flag:
    print(-1)