Skip to content

Instantly share code, notes, and snippets.

@waynejo
Created December 12, 2014 13:14
Show Gist options
  • Save waynejo/f199df1dc8468769570d to your computer and use it in GitHub Desktop.
Save waynejo/f199df1dc8468769570d to your computer and use it in GitHub Desktop.
object Main {
def toDirectories(path:String):Set[String] = {
path.split("/").drop(1).
foldLeft(List[String](""))(
(acc:List[String], x:String) => {(acc.head + "/" + x) :: acc}
).init.toSet
}
def childSets(directories:Set[String]):Set[String] = {
directories.map(toDirectories).foldLeft(Set[String]()) (
(acc, x) => {x ++ acc}
)
}
def solve(exist:Set[String], needed:Set[String]) = {
(childSets(needed) -- childSets(exist)).size
}
def main(args: Array[String]) {
val writer = new java.io.PrintWriter("a-large.out")
try {
process(io.Source.fromFile("A-large-practice (4).in").getLines)(writer.println)
} finally {
writer.flush()
writer.close()
}
}
def process(lineIn: Iterator[String])(lineOut: String => Unit) = {
for (i <- 1 to lineIn.next().toInt) {
val Array(existNum, neededNum) = lineIn.next().split(" ").map(_.toInt)
val exist = for (i <- 0 until existNum) yield lineIn.next()
val needed = for (i <- 0 until neededNum) yield lineIn.next()
lineOut(s"Case #$i: ${solve(exist.toSet, needed.toSet)}")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment