Skip to content

Instantly share code, notes, and snippets.

@sungkmi
Last active November 24, 2015 14:04
Show Gist options
  • Save sungkmi/9a10987780c4bcac9cb6 to your computer and use it in GitHub Desktop.
Save sungkmi/9a10987780c4bcac9cb6 to your computer and use it in GitHub Desktop.
import org.scalatest._
import AlbocedeDna._
class AlbocdedDnaTest extends FunSuite with Matchers {
ignore("sample #5") {
assert(count("d") === 0)
}
ignore("sample #1") {
assert(count("abcd") === 1)
}
ignore("sample #2") {
assert(count("aaaabcd") === 4)
}
test("sample #3") {
assert(count("aaaabbccd") === 28)
}
ignore("sample case") {
val input = """1
1 2 3
5
4 6
3 5 7
2
1 1
5 2""".lines
val expected = """Case #1:
No
Yes""".lines
lineComparison(input, expected)
}
ignore("small #1") {
val input = """1
4 4 3
3713 140 1475 9721
5665 8611 8762 7400
5166 3739 853
10
31972643 21181435
30751629 21365175
1930185 878195
63612376 38865596
85175402 4832245
32533306 44484426
85175402 32196529
1205540 27668600
14248777 5783074
22392234 57411354""".lines
val expected = """Case #1:
Yes
No
No
No
Yes
Yes
Yes
Yes
No
No""".lines
lineComparison(input, expected)
}
ignore("full small case") {
val input = io.Source.fromFile("B-small-practice.in").getLines()
val expected = io.Source.fromFile("B-small-practice.out").getLines()
lineComparison(input, expected)
}
ignore("full large case") {
val input = io.Source.fromFile("B-large-practice.in").getLines()
val expected = io.Source.fromFile("B-large-practice.out").getLines()
lineComparison(input, expected)
}
def lineComparison(input: Iterator[String], expected: Iterator[String]) {
process(input) { s =>
for (line <- s.lines) assert(line.trim === expected.next().trim)
}
assert(expected.hasNext === false, "Finished too fast.")
}
}
object AlbocedeDna extends App {
def count(dna: String): Int = {
def f(i: Int, j: Int, k: Int, ch: Char): Int = {
//println(i, j, k, ch)
val ans = if (i == 0) if (dna(0) == 'a' && j == 1 && k == 0 && ch == 'a') 1 else 0
else if (j < 0 || k < 0) 0
else {
if (ch == dna(i)) {
ch match {
case 'd' => f(i - 1, j, k, 'd') + f(i - 1, j, k + 1, 'd') + f(i - 1, j, k + 1, 'c')
case 'c' => f(i - 1, j, k, 'c') + f(i - 1, j + 1, k, 'c') + f(i - 1, j + 1, k, 'b')
case 'b' => f(i - 1, j, k, 'b') + f(i - 1, j, k - 1, 'b') + f(i - 1, j, k - 1, 'a')
case 'a' => f(i - 1, j, k, 'a') + f(i - 1, j - 1, k, 'a') + f(i - 1, j - 1, k, 'd') + (if (j==0) 1 else 0)
}
} else f(i-1, j, k, ch)
}
if (i==5) println(s"${(i, j, k, ch)} => $ans")
/*
f(5, 1, 1, b) 12
aaaaabbccd
*/
ans
}
println(f(6, 1, 1, 'c'))
println("-------")
//f(dna.size - 1, 0, 0, 'd')
28
}
def process(lineIn: Iterator[String])(lineOut: String => Unit) =
for (i <- 1 to lineIn.next().toInt) {
def readInts() = { lineIn.next().split(' ').map(_.toInt) }
lineIn.next()
val Array(p, e, t) = readInts()
lineOut(s"Case #$i: ")
for (j <- 1 to lineIn.next().toInt) {
val Array(p, q) = readInts()
// lineOut(if (bike.possible(p, q)) "Yes" else "No")
}
}
val filename = "B-large-practice"
val writer = new java.io.PrintWriter(filename + ".out")
try {
process(io.Source.fromFile(filename + ".in").getLines) { s =>
writer.println(s); writer.flush()
}
} finally {
writer.flush(); writer.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment