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 in Kotlin (1.0.0) (judge verdict: Accepted)

import java.io.PrintWriter
import java.util.*
import java.math.*
import java.lang.*
import kotlin.comparisons.*

val pw = PrintWriter(System.out)
val MOD: Long = (1e+9 + 7).toLong()

fun getNextAnimal(t1: Char, t2: Char, cs: Char): Char {
	return if(t2 == 'S' && t1 == 'S') {
		if(cs == 'o') 'S' else 'W'
	} else if(t2 == 'S' && t1 == 'W') {
		if(cs == 'o') 'W' else 'S'
	} else if(t2 == 'W' && t1 == 'W') {
		if(cs == 'o') 'S' else 'W'
	} else {
		if(cs == 'o') 'W' else 'S'
	}
}

fun solve(n: Int, s: CharArray): String {
	// 0,1が羊と仮定
	val t1 = CharArray(n) { 'S' }.apply { this[1] = 'S' }
	// 0: 羊 1: 狼
	val t2 = CharArray(n) { 'S' }.apply { this[1] = 'W' }
	val t3 = CharArray(n) { 'W' }.apply { this[1] = 'W' }
	val t4 = CharArray(n) { 'W' }.apply { this[1] = 'S' }
	val tList = listOf(t1, t2, t3, t4)
	tList.forEach { t ->
		for(j in 1 until n-1) {
			t[j+1] = getNextAnimal(t[j-1], t[j], s[j])
		}

		val first = getNextAnimal(t[n-2], t[n-1], s[n-1])
		val second = getNextAnimal(t[n-1], t[0], s[0])
		if(first == t[0] && second == t[1]) return t.joinToString("")
	}
	return "-1"
}

fun main(args: Array<String>) {
	val n = readInt()
	val s = read().toCharArray()

	println(solve(n, s))
	pw.flush()
}

/****** Declared Functions and Data Structures ******/
// IO
fun read() = readLine()!!
fun readInt() = read().toInt()
fun readLong() = read().toLong()
fun readDouble() = read().toDouble()
fun readListOfString() = read().split(" ")
fun readListOfInt() = readListOfString().map { it.toInt() }
fun readListOfLong() = readListOfString().map { it.toLong() }
fun readListOfDouble() = readListOfString().map { it.toDouble() }

fun Double.format(digits: Int) = String.format("%.${digits}f", this)
fun Float.format(digits: Int) = String.format("%.${digits}f", this)

fun print(value: Any) { pw.print(value) }
fun println(value : Any) { pw.println(value) }

// Extensions
fun<T> List<T>.toBuckets(): Map<T, Int>
	= this.groupBy { it }.mapValues { it.value.size }

fun<T: Comparable<T>> List<T>.upperBound(element: T, fromIndex: Int=0, toIndex: Int=this.size): Int {
	var l = fromIndex
	var r = toIndex
	while(l < r) {
		val mid = (l+r) / 2
		if(element >= this[mid]) { 
			l = mid + 1
		} else {
			r = mid
		}
	}
	return l
}

fun<T: Comparable<T>> List<T>.lowerBound(element: T, fromIndex: Int=0, toIndex: Int=this.size): Int {
	var l = fromIndex
	var r = toIndex
	while(l < r) {
		val mid = (l+r) / 2
		if(element <= this[mid]) { 
			r = mid
		} else {
			l = mid + 1
		}
	}
	return r 
}

fun <K, V> Map<K, V>.toMutableMap(): MutableMap<K, V> = HashMap(this)

// Util Functions
fun Long.makeDivisors(): List<Long> {
	val divisors = mutableListOf<Long>()
	for(i in 1L .. Math.sqrt(this.toDouble()).toLong()+1L) {
		if(this % i == 0L) {
			divisors.add(i)
			if(i != this/i) {
				divisors.add(this/i)
			}
		}
	}
	return divisors.distinct().sorted()
}

fun Long.isPrime(): Boolean {
	if(this==1L) return false
	for(i in 2L .. Math.sqrt(this.toDouble()).toLong() + 1L) {
		if(this % i == 0L && this != i) {
			return false
		}
	}
	return true
}


fun<T> permutations(src: List<T>): List<List<T>> {
	if(src.size == 1) return listOf(src)
	val perms = mutableListOf<List<T>>()
	val insertElement = src[0]
	permutations(src.drop(1)).forEach { perm ->
		for(i in 0..perm.size) {
			val newPerm = perm.toMutableList()
			newPerm.add(i, insertElement)
			perms.add(newPerm.toList())
		}
	}
	return perms
}

// return nCr, if you want nCr, please access v[n][r]
fun combinations(n: Long): List<List<Long>> {
	val v = (0..n).map { (0..n).map{0L}.toMutableList() }.toMutableList()
	for(i in 0 until v.size) {
		v[i][0] = 1L
		v[i][i] = 1L
	}
	for(i in 0 until v.size) {
		for(j in 1 until i) {
			v[i][j] = (v[i-1][j-1] + v[i-1][j]) % MOD
		}
	}
	return v.map { it.toList() }.toList()
}

fun repeatedModPow(a: Long, n: Long, mod: Long): Long = 
	when {
		n == 1L -> a % mod
		n % 2L == 1L -> (a * repeatedModPow(a, n-1L, mod)) % mod
		else -> {
			val tmp = repeatedModPow(a, n / 2L, mod)
			(tmp * tmp) % mod
		}
	}

// return nCr % MOD
fun modnCkWithoutArray(n: Long, k: Long, mod: Long): Long {
	var ret = 1L
	val min = Math.min(k, n-k)
	// まず分母の計算
	for(i in n downTo (n - min + 1)) {
		ret = ret * i % mod
	}
	for(i in min downTo 1) {
		ret = ret * repeatedModPow(i, (mod - 2L), mod) % mod
	}
	return ret
}

// return nCr % MOD
fun modnCk(n: Int, k: Int, mod: Long): Long {
	val factorials = Array(n+1) { 0L }.apply {
		this[0] = 1L
		if(this.size > 1) this[1] = 1L
	}
	val inverses = Array(n+1) { 0L }.apply {
		this[0] = 1L
		if(this.size > 1) this[1] = 1L
	}
	val factorialInverses = Array(n+1) { 0L }.apply {
		this[0] = 1L
		if(this.size > 1) this[1] = 1L
	}
	for(i in 2 .. n) {
		factorials[i] = (factorials[i-1] * i.toLong() % mod)
		inverses[i] = (mod - inverses[mod.toInt() % i] * (mod / i.toLong()) % mod)
		factorialInverses[i] = (factorialInverses[i-1] * inverses[i] % mod)
	}

	return factorials[n] % mod * (factorialInverses[k] * factorialInverses[n-k] % mod) % mod
}

// should be a >= b
fun gcd(a: Long, b: Long): Long = 
	if(b == 0L) a else gcd(b, a % b)

// shoud be a >= b
fun lcm(a: Long, b: Long): Long = 
	a / gcd(a, b) * b

fun sumDigits(num_: Long): Long {
	var num = num_
	var rtn: Long = 0
	while(num != 0.toLong()) {
		val tmp = num % 10
		rtn += tmp
		num /= 10
	}
	return rtn
}

fun Double.isDecimal(): Boolean = ((this - Math.floor(this)) != 0.0)

// Data Structures

class UnionFind(private val n: Int) {
	private val parent: Array<Int> = Array(n) { 0 }
	private val rank: Array<Int> = Array(n) { 0 }
	private val size: Array<Int> = Array(n) { 0 }

	init {
		for(i in 0 until n) {
			parent[i] = i
			rank[i] = 1
			size[i] = 1
		}
	}

	fun union(u: Int, v: Int) {
		var x = root(u)
		var y = root(v)
		if(x != y) {
			if(rank[y] < rank[x]) {
				val tmp = y 
				y = x
				x = tmp 
			}
			parent[x] = y
			rank[y] += rank[x]
			rank[x] = -1
			val totalSize = size[x] + size[y]
			size[x] = totalSize
			size[y] = totalSize
		}
	}

	fun root(u: Int): Int {
		var x = u
		while(parent[x] != x) {
			x = parent[x]
		}
		return x 
	}

	fun size(u: Int): Int = size[root(u)]
}