Skip to content

Instantly share code, notes, and snippets.

@bongster
Created December 5, 2014 13:16
Show Gist options
  • Save bongster/7c599f3953fdc68f635a to your computer and use it in GitHub Desktop.
Save bongster/7c599f3953fdc68f635a to your computer and use it in GitHub Desktop.
import java.io.{FileInputStream, FileOutputStream}
/**
* Created by bongster on 14. 12. 5..
*/
object main extends App{
//val testSentence = "elcomew elcome to code jam"
val correctSentence = "welcome to code jam"
Console.setIn(new FileInputStream("C-small-practice.in"))
Console.setOut(new FileOutputStream("C-small-practice.out"))
def solve(testSentence: String) : Int = {
val test = Array.fill(correctSentence.length + 1, testSentence.length + 1)(0)
test(0) = Array.fill(testSentence.length + 1)(1)
for{
p <- 1 to correctSentence.length
q <- 1 to testSentence.length}{
// println(s"${correctSentence(p -1 )} == ${testSentence(q -1 )} == ${correctSentence(p -1 ) == testSentence(q -1 )}")
correctSentence(p -1) == testSentence(q -1) match {
case true => test(p)(q) = test(p - 1)(q) + test(p)(q - 1)
case false => test(p)(q) = test(p)(q-1)
}
}
test(correctSentence.length)(testSentence.length)
}
val cases = readLine().toInt
(1 to cases) foreach {n =>
var testSentence = readLine()
Console println f"Case #$n: ${solve(testSentence)}%04d"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment