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:

def reset(n):
    a = [0 for i in range(n+2)]
    return a
    
n = int(input())
s = list(input())
s.append(s[0])

dt = [[1,1],[-1,1],[1,-1],[-1,-1]] #1→sheep,-1→wlf

ans = False
for i in range(4):
    li = reset(n)
    li[0] = dt[i][0]
    li[1] = dt[i][1]

    for j in range(1,len(s)):
        if s[j] =='o':
            li[j+1] = li[j]*li[j-1]
        elif s[j] =='x':
            li[j+1] = li[j]*li[j-1]*(-1)
    if li[0]==li[n] and li[1]==li[n+1]:
        ans = True
        break

if (ans):
    for i in range(n):
        if li[i] == 1:
            print('S',end="")
        else:
            print('W',end="")
    print('')

else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
SW_list = ["SS", "SW", "WS", "WW"]
check = False
for sw in SW_list:
    ans = [""] * N
    ans[0] = sw[0]
    ans[1] = sw[1]
    for i in range(1, N-1):
        if (ans[i] == "S" and s[i] == "o") or (ans[i] == "W" and s[i] == "x"):
            ans[i+1] = ans[i-1]
        else:
            ans[i+1] = "S" if ans[i-1] == "W" else "W"
    if (ans[0] == "S" and s[0] == "o") or (ans[0] == "W" and s[0] == "x"):
        if ans[1] == ans[-1]:
            check = True
    else:
        if ans[1] != ans[-1]:
            check = True
    if (ans[-1] == "S" and s[-1] == "o") or (ans[-1] == "W" and s[-1] == "x"):
        if ans[0] != ans[-2]:
            check = False
    else:
        if ans[0] == ans[-2]:
            check = False
    if check:
        print(*ans, sep="")
        break

if check == False:
    print(-1)

Submission status: Accepted

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


def main():
    n = int(input())
    s = list(input())

    # 1番目と2番目を決め打ち
    # SS，SW，WW，WS
    # 1文字目とn + 1文字目が一致するかどうか
    for pair in ['SS', 'SW', 'WW', 'WS']:
        ans = ['' for _ in range(n + 1)]

        for index, p in enumerate(pair):
            ans[index] = p

            for j in range(1, n):
                if s[j] == 'o':
                    if ans[j] == 'S':
                        ans[j + 1] = ans[j - 1]
                    else:
                        if ans[j - 1] == 'S':
                            ans[j + 1] = 'W'
                        else:
                            ans[j + 1] = 'S'
                else:
                    if ans[j] == 'S':
                        if ans[j - 1] == 'S':
                            ans[j + 1] = 'W'
                        else:
                            ans[j + 1] = 'S'
                    else:
                        ans[j + 1] = ans[j - 1]

        if ans[0] == ans[-1]:
            # 気がつけなかった点
            # 2文字目とN文字目が矛盾していないかチェック
            if ans[n - 1] == ans[1] and ((s[0] == 'o' and ans[0] == 'S') or (s[0] == 'x' and ans[0] == 'W')):
                print(''.join(ans[:-1]))
                break
            elif ans[n - 1] != ans[1] and ((s[0] == 'o' and ans[0] == 'W') or (s[0] == 'x' and ans[0] == 'S')):
                print(''.join(ans[:-1]))
                break
    else:
        print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def main():
    n = int(input())
    s = list(input())

    # 1番目と2番目を決め打ち
    # SS，SW，WW，WS
    # 1文字目とn + 1文字目が一致するかどうか
    for pair in ['SS', 'SW', 'WW', 'WS']:
        ans = ['' for _ in range(n + 1)]

        for index, p in enumerate(pair):
            ans[index] = p

            for j in range(1, n):
                if s[j] == 'o':
                    if ans[j] == 'S':
                        ans[j + 1] = ans[j - 1]
                    else:
                        if ans[j - 1] == 'S':
                            ans[j + 1] = 'W'
                        else:
                            ans[j + 1] = 'S'
                else:
                    if ans[j] == 'S':
                        if ans[j - 1] == 'S':
                            ans[j + 1] = 'W'
                        else:
                            ans[j + 1] = 'S'
                    else:
                        ans[j + 1] = ans[j - 1]

        if ans[0] == ans[-1]:
            if ans[n - 1] == ans[1] and ((s[0] == 'o' and ans[0] == 'S') or (s[0] == 'x' and ans[0] == 'W')):
                print(''.join(ans[:-1]))
                break
            elif ans[n - 1] != ans[1] and ((s[0] == 'o' and ans[0] == 'W') or (s[0] == 'x' and ans[0] == 'S')):
                print(''.join(ans[:-1]))
                break
    else:
        print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
list_pat = [["S", "S"], ["S", "W"], ["W", "S"], ["W", "W"]]
for one_s in s[1:]:
  if one_s == "o":
    for i, one_pat in enumerate(list_pat):
      if one_pat[-1] == "S":
        if one_pat[-2] == "S":
          list_pat[i].append("S")
        else:
          list_pat[i].append("W")
      else:
        if one_pat[-2] == "S":
          list_pat[i].append("W")
        else:
          list_pat[i].append("S")
  else:
    for i, one_pat in enumerate(list_pat):
      if one_pat[-1] == "S":
        if one_pat[-2] == "S":
          list_pat[i].append("W")
        else:
          list_pat[i].append("S")
      else:
        if one_pat[-2] == "S":
          list_pat[i].append("S")
        else:
          list_pat[i].append("W")

for one_pat in list_pat:
  if one_pat[0] == one_pat[-1]:
    if one_pat[0] == "S":
      if s[0] == "o" and one_pat[-2] == one_pat[1]:
        print("".join(one_pat[:-1]))
        exit()
      elif s[0] == "x" and one_pat[-2] != one_pat[1]:
        print("".join(one_pat[:-1]))
        exit()
    else:
      if s[0] == "o" and one_pat[-2] != one_pat[1]:
        print("".join(one_pat[:-1]))
        exit()
      elif s[0] == "x" and one_pat[-2] == one_pat[1]:
        print("".join(one_pat[:-1]))
        exit()
print("-1")

Submission status: Accepted

Solution code:
# 羊、狼をそれぞれ1,0と表記する
# 回答o,xをそれぞれ1,0と表記する

N = int(input())
s = list(input().replace("o", "1").replace("x", "0"))
s = list(map(int, s))

# 隣接する2匹の動物と右側の動物の回答を受けて、次の動物を返す
def next_animal(animal1, animal2, answer2):
    if (animal2 == 1 and answer2 == 1) or (animal2==0 and answer2==0):
        return animal1
    else:
        return 1^animal1

# 隣接する2匹の動物のパターン4通り×円順列に対するパターンN通りを試す
for first_two_animals in [[1,1],[1,0],[0,1],[0,0]]:
    animal_list = []
    animal_list.append(first_two_animals[0])
    animal_list.append(first_two_animals[1])

    for idx in range(2,N):
        animal_list.append(next_animal(animal_list[idx-2], animal_list[idx-1], s[idx-1]))

    # N番目の動物と回答が、状況と矛盾していないか確かめる
    if (s[0]==1 and animal_list[0]==1) or (s[0]==0 and animal_list[0]==0):
        last_animal = animal_list[1]
    else:
        last_animal = 1^animal_list[1]

    if (last_animal==1 and (animal_list[-2] == animal_list[0])) or (last_animal==0 and (animal_list[-2] != animal_list[0])):
        last_answer = 1
    else:
        last_answer = 0

    if last_animal == animal_list[-1] and last_answer == s[-1]:
        animal_list = list(map(str, animal_list))
        animal_list = [x.replace("1", "S").replace("0", "W") for x in animal_list]
        print("".join(animal_list))
        exit()

print(-1)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

n=int(input())
s=input()
tmp=["SS","WW","SW","WS"]
for j in range(4):
    ans=tmp[j]
    for i in range(n-1):
        if ans[i+1]=="S":
            if s[i+1]=="o":
                if ans[i]=="S":
                    ans+="S"
                else:
                    ans+="W"
            else:
                if ans[i]=="S":
                    ans+="W"
                else:
                    ans+="S"
        else:
            if s[i+1]=="o":
                if ans[i]=="S":
                    ans+="W"
                else:
                    ans+="S"
            else:
                if ans[i]=="S":
                    ans+="S"
                else:
                    ans+="W"
    if ans[0]==ans[-1]:
        if s[0]=="o":
            if ans[0]=="S" and ans[1]==ans[-2]:
                print(ans[:-1])
                quit()
            if ans[0]=="W" and ans[1]!=ans[-2]:
                print(ans[:-1])
                quit()
        else:
            if ans[0]=="S" and ans[1]!=ans[-2]:
                print(ans[:-1])
                quit()
            if ans[0]=="W" and ans[1]==ans[-2]:
                print(ans[:-1])
                quit()
print(-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_list = list(input())
def main():
  animal_list = [None] * n
  sonzai = False
  #SS
  if not sonzai:
    animal_list[0] = True
    animal_list[1] = True
    animal_list = create_animal_list(animal_list)
    sonzai = is_valid_list(animal_list)
  if not sonzai:
    animal_list[0] = True
    animal_list[1] = False
    animal_list = create_animal_list(animal_list)
    sonzai = is_valid_list(animal_list)
  if not sonzai:
    animal_list[0] = False
    animal_list[1] = False
    animal_list = create_animal_list(animal_list)
    sonzai = is_valid_list(animal_list)
  if not sonzai:
    animal_list[0] = False
    animal_list[1] = True
    animal_list = create_animal_list(animal_list)
    sonzai = is_valid_list(animal_list)
  if not sonzai:
    print(-1)
  else:
    animal_list2 = []
    for animal in animal_list:
      if animal:
        animal_list2.append("S")
      else:
        animal_list2.append("W")
    animal_list2 = "".join(animal_list2)
    print(animal_list2)
def is_valid_list(animal_list):
  return is_ok(animal_list, 0) and is_ok(animal_list,-1)
  
def is_ok(animal_list, index):
  if animal_list[index%n]:
    if s_list[index%n] == "o":
      return animal_list[(index+1)%n] == animal_list[index-1]
    else:
      return not (animal_list[(index+1)%n] == animal_list[index-1])
  else:
    if s_list[index%n] == "o":
      return not (animal_list[(index+1)%n] == animal_list[index-1])
    else:
      return (animal_list[(index+1)%n] == animal_list[index-1])
def create_animal_list(animal_list):
  for index in range(2, len(animal_list)):
    if animal_list[index-1]:
      if s_list[index-1] == "o":
        animal_list[index] = animal_list[index-2]
      else:
        animal_list[index] = not animal_list[index-2]
    else:
      if s_list[index-1] == "o":
        animal_list[index] = not animal_list[index-2]
      else:
        animal_list[index] = animal_list[index-2]
  return animal_list
main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = -1

for i in ['SS', 'SW', 'WW', 'WS']:
    narabi = ''
    narabi += i
    for k in range(n - 2):
        if narabi[-1] == 'S':
            if s[k + 1] == 'o':
                narabi += narabi[-2]
            else:
                if narabi[-2] == 'S':
                    narabi += 'W'
                else:
                    narabi += 'S'
        else:
            if s[k + 1] == 'o':
                if narabi[-2] == 'S':
                    narabi += 'W'
                else:
                    narabi += 'S'
            else:
                narabi += narabi[-2]
    kenshou = narabi[-2:]
    kenshou2 = kenshou + narabi[:2]
    kenshou_s = s[-1] + s[-0]
    for k in range(2):
        if kenshou[-1] == 'S':
            if kenshou_s[k] == 'o':
                kenshou += kenshou[-2]
            else:
                if kenshou[-2] == 'S':
                    kenshou += 'W'
                else:
                    kenshou += 'S'
        else:
            if kenshou_s[k] == 'o':
                if kenshou[-2] == 'S':
                    kenshou += 'W'
                else:
                    kenshou += 'S'
            else:
                kenshou += kenshou[-2]
    if kenshou == kenshou2:
        ans = narabi
        break
    # print(kenshou2)
    # print(kenshou)
    # print(narabi)
print(ans)

Submission status: Accepted

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

cs = 'SW'
for ss in [[0, 0], [0, 1], [1, 0], [1, 1]]:
    for i in range(1, n + 1):
        if (ss[i % n] == 0 and s[i % n] == 'o') or (ss[i % n] == 1
                                                    and s[i % n] == 'x'):
            ss.append(ss[i - 1])
        else:
            ss.append(1 - ss[i - 1])
    if ss[-2] == ss[0] and ss[-1] == ss[1]:
        res = ''.join([cs[i] for i in ss[:-2]])
        print(res)
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
s = list(input())
if s[1] == 'o':
    ans = [['S', 'S', 'S'], ['W', 'S', 'W'], ['S', 'W', 'W'], ['W', 'W', 'S']]
else:
    ans = [['S', 'S', 'W'], ['W', 'S', 'S'], ['S', 'W', 'S'], ['W', 'W', 'W']]

for i in range(4):
    for j in range(2, N-1):
        if s[j] == 'o':
            n0, n1 = 'S', 'W'
        else:
            n0, n1 = 'W', 'S'
        if ans[i][j-1] == 'S':
            if ans[i][j] == 'S':
                ans[i].append(n0)
            else:
                ans[i].append(n1)
        else:
            if ans[i][j] == 'S':
                ans[i].append(n1)
            else:
                ans[i].append(n0)
    
    Realize = True
    for j in [-1, 0]:
        if s[j] == 'o':
            if ans[i][j] == 'S':
                if ans[i][j-1] != ans[i][j+1]:
                    Realize = False
                    break
            else:
                if ans[i][j-1] == ans[i][j+1]:
                    Realize = False
                    break
        else:
            if ans[i][j] == 'S':
                if ans[i][j-1] == ans[i][j+1]:
                    Realize = False
                    break
            else:
                if ans[i][j-1] != ans[i][j+1]:
                    Realize = False
                    break
    if Realize:
        print(''.join(ans[i]))
        exit()
print(-1)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
def e(c):
    if c=="S":
        return "W"
    else:
        return "S"

def f(s1,sn,s2):
    ans=[""]*n
    ans[0]=s1
    ans[1]=s2
    for i in range(1,n-1):
        if s[i]=="o":
            if ans[i]=="S":
                ans[i+1]=ans[i-1]
            else:
                ans[i+1]=e(ans[i-1])
        else:
            if ans[i]=="W":
                ans[i+1]=ans[i-1]
            else:
                ans[i+1]=e(ans[i-1])
    if ans[-1]!=sn:
        return False
    elif s[-1]=="o":
        if ans[-1]=="S":
            if ans[-2]==ans[0]:
                return ans
        else:
            if ans[0]==e(ans[-2]):
                return ans
    else:
        if ans[-1]=="W":
            if ans[-2]==ans[0]:
                return ans
        else:
            if ans[0]==e(ans[-2]):
                return ans
    return False

if s[0]=="o":
    if f("S","S","S"):
        print(*f("S","S","S"),sep="")
    elif f("W","S","W"):
        print(*f("W","S","W"),sep="")
    elif f("W","W","S"):
        print(*f("W","W","S"),sep="")
    elif f("S","W","W"):
        print(*f("S","W","W"),sep="")
    else:
        print(-1)
else:
    if f("W","W","W"):
        print(*f("W","W","W"),sep="")
    elif f("W","S","S"):
        print(*f("W","S","S"),sep="")
    elif f("S","S","W"):
        print(*f("S","S","W"),sep="")
    elif f("S","W","S"):
        print(*f("S","W","S"),sep="")
    else:
        print(-1)

Submission status: Accepted

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

l = ['SS', 'SW', 'WS', 'WW']

for i in range(4):
  ans = l[i]
  for j in range(1, n):
    if ans[j] == 'S' and s[j] == 'o':
      if ans[j-1] == 'S':
        ans += 'S'
      else:
        ans += 'W'
    elif ans[j] == 'S' and s[j] == 'x':
      if ans[j-1] == 'S':
        ans += 'W'
      else:
        ans += 'S'
    elif ans[j] == 'W' and s[j] == 'o':
      if ans[j-1] == 'S':
        ans += 'W'
      else:
        ans += 'S'
    else:
      if ans[j-1] == 'S':
        ans += 'S'
      else:
        ans += 'W' 
  flag = 0
  if ans[-1] == ans[0]:
    if ans[0] == 'S' and s[0] == 'o':
      if ans[-2] == ans[1]:
        flag = 1
    elif ans[0] == 'W' and s[0] == 'o':
      if ans[-2] != ans[1]:
        flag = 1
    elif ans[0] == 'S' and s[0] == 'x':
      if ans[-2] != ans[1]:
        flag = 1
    else:
      if ans[-2] == ans[1]:
        flag = 1
  if flag == 1:
    print(ans[:-1])
    exit()
else:
  print('-1')

Submission status: Accepted

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

ok = 0

for ii in [[0,0], [0,1], [1,0], [1,1]]:
    A = [ii[0], ii[1]]
    # print(A)
    for i in range(1, n-1):
        A.append(int((A[-1] == 1) ^ (A[-2] == 1) ^ (s[i] == "x")))
    # print(A)
    if (A[-2] == 1) ^ (A[-1] == 1) ^ (A[0] == 1) ^ (s[-1] == "o") and \
        (A[-1] == 1) ^ (A[0] == 1) ^ (A[1] == 1) ^ (s[0] == "o"):
        ok = 1
        break

if ok == 0:
    print(-1)
else:
    ans = []
    for i in range(n):
        if A[i] == 0:
            ans.append("S")
        else:
            ans.append("W")
    print("".join(ans))

Submission status: Accepted

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


def check_truth(a_0, a_1):
    a = [True for i in range(N)]  # True: Sheep, False: Wolf
    a[0] = a_0
    a[-1] = a_1
    for i in range(N):
        if a[i]:
            if s[i] == 'o':
                a[(i+1)%N] = a[i-1]
            else:
                a[(i+1)%N] = not a[i-1]
        else:
            if s[i] == 'o':
                a[(i+1)%N] = not a[i-1]
            else:
                a[(i+1)%N] = a[i-1]
    if a[0] != a_0 or a[-1] != a_1:
        return -1
    else:
        return a


p = [True, False]
for i in range(2):
    for j in range(2):
        ans = check_truth(p[i], p[j])
        if ans != -1:
            for k in range(N):
                if ans[k]:
                    print('S', end='')
                else:
                    print('W', end='')
            print()
            exit()
print(-1)

Submission status: Accepted

Solution code:
def solve():
	n=int(input())
	s=[c=="o"for c in input()]
	for a,b in ([0,0],[0,1],[1,0],[1,1]):
		r=[a,b]
		for i in range(n):a,b=b,s[-~i%n]==(a==b);r+=[b]
		if r[:2]==r[-2:]:print("".join("WS"[i]for i in r[:-2]),sep="");exit()
	print(-1)
	
if __name__=="__main__":
	solve()

Submission status: Accepted

Solution code:
n,s=open(0);n=int(n)
for a,b in ([0,0],[0,1],[1,0],[1,1]):
	r=[a,b]
	for i in range(n):a,b=b,s[-~i%n]=="xo"[a==b];r+=[b]
	if r[:2]==r[-2:]:print(*("WS"[i]for i in r[:-2]),sep="");exit()
print(-1)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

    next_o = {"SS": "S", "SW": "W","WS": "W", "WW": "S"}
    next_x = {"SS": "W", "SW": "S","WS": "S", "WW": "W"}

    for ans in ["SS", "SW", "WS", "WW"]:
        for i in range(1, N):
            if s[i] == "o":
                ans += next_o[ans[-2:]]
            else:
                ans += next_x[ans[-2:]]
        
        if ans[0] == ans[-1]:
            if s[0] == "o":
                if ans[1] == next_o[ans[-2:]]:
                    print(ans[:-1])
                    return
            else:
                if ans[1] == next_x[ans[-2:]]:
                    print(ans[:-1])
                    return
    
    print(-1)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
s=[0+(m=="o") for m in input()]

#o:1,x:0
#Sheep:1,wolf:0
def nxt(a,b,m):
	if a==b:
		return m
	else:
		return 1-m

for a,b in ((1,1),(0,1),(1,0),(0,0)):
	c,d = a,b
	ans = [a,b]
	for i in range(n-2):
		a,b = b,nxt(a,b,s[i+1])
		ans.append(b)
	if nxt(a,b,s[-1])==c and nxt(b,c,s[0])==d:
		for x in ans:
			if x:
				print("S",end="")
			else:
				print("W",end="")
		exit()
print(-1)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
def calcnext(a, b, c):
  if b == "W":
    c = "x" if c == "o" else "o"
  n = a if c == "o" else "S" if a == "W" else "W"
  return n
for ab in ["SS", "SW", "WS", "WW"]:
  l = list(ab)
  for i in range(2, N+2):
    n = calcnext(l[i-2], l[i-1], s[(i-1)%N])
    l.append(n)
  #print(l)
  if l[N:N+2] == l[0:2]:
    print("".join(l[:N]))
    exit()
print(-1)

Submission status: Accepted

Solution code:
input();s=input();A=["SS","SW","WS","WW"]
for c in s:A=[t+"SW"[(t[-1]==t[-2])-(c=="o")]for t in A]
A=[t[1:-1]for t in A if t[:2]==t[-2:]];print(A[0]if A else-1)

Submission status: Accepted

Solution code:
# SS, SW, WS, WWのときについてそれぞれ調べればよい

N=int(input())
s=input()
A=["SS","SW","WS","WW"]
for i,c in enumerate(s):
    for j in range(4):
        if A[j][-2:]=="SS":
            if c=="o":A[j]+="S"
            else:A[j]+="W"
        elif A[j][-2:]=="SW":
            if c=="o":A[j]+="W"
            else:A[j]+="S"
        elif A[j][-2:]=="WS":
            if c=="o":A[j]+="W"
            else:A[j]+="S"
        else:
            if c=="o":A[j]+="S"
            else:A[j]+="W"
A=[t[1:-1]for t in A if t[:2]==t[-2:]]
print(A[0]if A else-1)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
T = [int(s == "o") for s in S]

def solve():
    for A in [[0, 0], [0, 1], [1, 0], [1, 1]]:
        for i in range(2, N):
            A.append(A[-2] ^ A[-1] ^ T[i - 1])
        if (T[-1] == (A[-2] ^ A[-1] ^ A[0]) and
            T[0] == (A[-1] ^ A[0] ^ A[1])):
            return A
    return []

ans = solve()
if ans:
    print("".join("S" if a else "W" for a in ans))
else:
    print(-1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC55 D

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

def check(t):
    for i in range(n):
        if t[i] == 'S':
            if s[i] == 'o':
                if i == n-1:
                    if t[i-1] != t[0]:
                        return False
                        break
                else:
                    if t[i-1] != t[i+1]:
                        return False
                        break
            else:
                if i == n-1:
                    if t[i-1] == t[0]:
                        return False
                        break
                else:
                    if t[i-1] == t[i+1]:
                        return False
                        break
        if t[i] == 'W':
            if s[i] == 'o':
                if i == n-1:
                    if t[i-1] == t[0]:
                        return False
                        break
                else:
                    if t[i-1] == t[i+1]:
                        return False
                        break
            else:
                if i == n-1:
                    if t[i-1] != t[0]:
                        return False
                        break
                else:
                    if t[i-1] != t[i+1]:
                        return False
    return True

n = I()
s = input()
tmp = ['SS','SW','WS','WW']
for x in tmp:
    for i in range(1,n-1):
        if s[i] == 'o':
            if x[-1] == 'S':
                if x[-2] == 'W':
                    x += 'W'
                else:
                    x += 'S'
            else:
                if x[-2] == 'W':
                    x += 'S'
                else:
                    x += 'W'
        else:
            if x[-1] == 'S':
                if x[-2] == 'W':
                    x += 'S'
                else:
                    x += 'W'
            else:
                if x[-2] == 'W':
                    x += 'W'
                else:
                    x += 'S'
    if check(x):
        print(x)
        quit()
print(-1)

Submission status: Accepted

Solution code:
n=int(input());s=input()
for i in range(4):
    a=[i%2,i//2];i=1
    while i<=n:a.append(a[-2]^a[-1]^(s[i%n]=='x'));i+=1
    if a[:2]==a[-2:]:
        for i in range(n):print('SW'[a[i]],end='')
        exit()
print(-1)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
for i in range(4):
    a=[i%2,i//2]+[1]*n
    for i in range(2,n+2):a[i]^=a[i-2]^a[i-1]^(s[(i-1)%n]=='o')
    if a[:2]==a[-2:]:
        for i in range(n):print('SW'[a[i]],end='')
        exit()
print(-1)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
s=s+s[0]
for a,b in [(0,0),(0,1),(1,0),(1,1)]:
    ans=[a,b]+[1]*n
    for i in range(2,n+2):
        ans[i]^=ans[i-2]^ans[i-1]^(s[i-1]=='o')
    if ans[:2]==ans[-2:]:
        for i in range(n):
            print('SW'[ans[i]],end='')
        break
else:
    print(-1)

Submission status: Accepted

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

# idx=0にN番目の動物、idx=N+1に1番目の動物
ansS = [""] * (N+2)
ansW = [""] * (N+2)
ansS[1] = ansS[N+1] = "S" # 1番目がSの場合
ansW[1] = ansW[N+1] = "W" # 1番目がWの場合
# N番目がSと仮定する
ansS[0] = ansS[N] = "S" 
ansW[0] = ansW[N] = "S" 
for i in range(N):
    
    if s[i] == "o": 
        ansS[i+2] = "S" if ((ansS[i+1] == "S" and ansS[i] == "S") or (ansS[i+1] == "W" and ansS[i] == "W")) else "W"
        ansW[i+2] = "S" if ((ansW[i+1] == "S" and ansW[i] == "S") or (ansW[i+1] == "W" and ansW[i] == "W")) else "W"
    else:
        ansS[i+2] = "S" if ((ansS[i+1] == "S" and ansS[i] == "W") or (ansS[i+1] == "W" and ansS[i] == "S")) else "W"
        ansW[i+2] = "S" if ((ansW[i+1] == "S" and ansW[i] == "W") or (ansW[i+1] == "W" and ansW[i] == "S")) else "W"

if ansS[0] == ansS[N] and ansS[1] == ansS[N+1]:
    print("".join(ansS[1:N+1]))
    exit()

if ansW[0] == ansW[N] and ansW[1] == ansW[N+1]:
    print("".join(ansW[1:N+1]))
    exit()


# idx=0にN番目の動物、idx=N+1に1番目の動物
ansS = [""] * (N+2)
ansW = [""] * (N+2)
ansS[1] = ansS[N+1] = "S" # 1番目がSの場合
ansW[1] = ansW[N+1] = "W" # 1番目がWの場合
# N番目がSと仮定する
ansS[0] = ansS[N] = "W" 
ansW[0] = ansW[N] = "W" 

for i in range(N):
    
    if s[i] == "o": 
        ansS[i+2] = "S" if ((ansS[i+1] == "S" and ansS[i] == "S") or (ansS[i+1] == "W" and ansS[i] == "W")) else "W"
        ansW[i+2] = "S" if ((ansW[i+1] == "S" and ansW[i] == "S") or (ansW[i+1] == "W" and ansW[i] == "W")) else "W"
    else:
        ansS[i+2] = "S" if ((ansS[i+1] == "S" and ansS[i] == "W") or (ansS[i+1] == "W" and ansS[i] == "S")) else "W"
        ansW[i+2] = "S" if ((ansW[i+1] == "S" and ansW[i] == "W") or (ansW[i+1] == "W" and ansW[i] == "S")) else "W"

if ansS[0] == ansS[N] and ansS[1] == ansS[N+1]:
    print("".join(ansS[1:N+1]))
    exit()

if ansW[0] == ansW[N] and ansW[1] == ansW[N+1]:
    print("".join(ansW[1:N+1]))
    exit()

print(-1)

Submission status: Accepted

Solution code:
def main():

    N = int(input())
    s = input()
    if s[0] == "o":
        cand = [["S", "S", "S"], ["W", "S", "W"], ["W", "W", "S"], ["S", "W", "W"]]
    else:
        cand = [["S", "S", "W"], ["W", "S", "S"], ["W", "W", "W"], ["S", "W", "S"]]

    for cd in cand:
        for i in range(1, N):
            if s[i] == "o" and cd[-1] == "S":
                if cd[-2] == "S": cd.append("S")
                else: cd.append("W")
            elif s[i] == "x" and cd[-1] == "S":
                if cd[-2] == "S": cd.append("W")
                else: cd.append("S")
            elif s[i] == "o" and cd[-1] == "W":
                if cd[-2] == "S": cd.append("W")
                else: cd.append("S")
            elif s[i] == "x" and cd[-1] == "W":
                if cd[-2] == "S": cd.append("S")
                else: cd.append("W")
        # print(cd)
        if cd[:2] == cd[-2:]:
            return "".join(cd[1:-1])
    return -1

if __name__ == '__main__':
    print(main())