Created
October 31, 2014 13:04
-
-
Save pr-lawrence/d41b47077982e11d7391 to your computer and use it in GitHub Desktop.
Saving the universe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.PrintWriter | |
import scala.io.Source | |
import java.io.File | |
object Main { | |
val in = Source.fromFile("large.in").getLines | |
val printer = new PrintWriter(new File("large.out")) | |
def getMaxIdx(searchEngines: List[String], queries: List[String]): Int = { | |
val queiresWithIndex = queries.zipWithIndex | |
searchEngines.foldLeft(0)((a, f) => { | |
val idx = getFirstIdx(f, queries) | |
if (idx > a) | |
idx | |
else | |
a | |
}) | |
} | |
def getFirstIdx(searchEngine: String, queries: List[String]): Int = { | |
queries.zipWithIndex.find(p => p._1 == searchEngine) match { | |
case Some((_, i)) => i | |
case _ => queries.size | |
} | |
} | |
def solve(searchEngines: List[String], queries: List[String], acc: Int): Int = { | |
if (searchEngines.toSet.size > queries.toSet.size) acc | |
else if (queries.size == 0) acc | |
else { | |
var maxIdx = getMaxIdx(searchEngines, queries) | |
if (queries.size == maxIdx) { | |
acc | |
} else { | |
solve(searchEngines, queries.takeRight(queries.size - maxIdx), acc + 1) | |
} | |
} | |
} | |
def main(args: Array[String]) { | |
process() | |
} | |
def process() { | |
for { i <- 1 to in.next.toInt } { | |
var searchEngines = (for { i <- 0 until in.next.toInt } yield { in.next.toString }).toList | |
var quries = (for { i <- 0 until in.next.toInt } yield { in.next.toString }).toList | |
printer.println(s"Case #$i: ${solve(searchEngines, quries, 0)}") | |
} | |
printer.flush | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment