Last active
August 29, 2015 14:08
-
-
Save waynejo/eda1bbfe3efcddcba4a0 to your computer and use it in GitHub Desktop.
This file contains 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
object Main { | |
def solve(searchEngines:List[String], keywords:List[String]): Integer = { | |
val distance = searchEngines.map(x => (x, keywords.indexOf(x))) | |
.map(_._2).maxBy(x => if (-1 == x) Integer.MAX_VALUE else x) | |
if (-1 == distance) 0 | |
else solve(searchEngines, keywords.drop(distance)) + 1 | |
} | |
def main(args: Array[String]) { | |
val writer = new java.io.PrintWriter("a-large.out") | |
try { | |
process(io.Source.fromFile("A-large-practice (3).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 searchEngineNum = lineIn.next().toInt | |
val searchEngines = (for (i <- 1 to searchEngineNum) yield lineIn.next()).toList | |
val keywordNum = lineIn.next().toInt | |
val keywords = (for (i <- 1 to keywordNum) yield lineIn.next()).toList | |
lineOut(s"Case #$i: ${solve(searchEngines, keywords)}") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment