Skip to content

Instantly share code, notes, and snippets.

@waynejo
Created May 3, 2019 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waynejo/233d8eb9754ecdb1be461082c663c1d2 to your computer and use it in GitHub Desktop.
Save waynejo/233d8eb9754ecdb1be461082c663c1d2 to your computer and use it in GitHub Desktop.
import java.io.FileInputStream
import scala.io.StdIn
object Solution extends App {
Console.setIn(new FileInputStream("example.in"))
def unzip(initial: BigInt, pairs: List[BigInt]): List[BigInt] = pairs match {
case Nil => Nil
case x :: xs =>
val next = x / initial
next :: unzip(next, xs)
}
def solve(n: BigInt, cipherText: IndexedSeq[BigInt]): String = {
(0 until cipherText.size - 1).find(i => cipherText(i) != cipherText(i + 1)) match {
case Some(x) =>
val gcd = cipherText(x) gcd cipherText(x + 1)
val (front, back) = cipherText splitAt x + 1
val primeSeq = unzip(gcd, front.toList.reverse).reverse ::: gcd :: unzip(gcd, back.toList)
(primeSeq map (primeSeq.distinct.sorted zip ('A' to 'Z')).toMap).mkString
case None => throw new RuntimeException("None")
}
}
val cases = StdIn.readLine().toInt
(1 to cases) foreach { i => {
val Array(n, _) = StdIn.readLine().split(" ").map(BigInt(_))
val cipherText = StdIn.readLine().split(" ").map(BigInt(_))
val answer = solve(n, cipherText.toVector)
println(s"Case #$i: $answer")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment