Created
September 20, 2012 04:56
-
-
Save alts/3754046 to your computer and use it in GitHub Desktop.
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 scala.collection.mutable.Set | |
object SelfSearch { | |
def arrayToSet(a : Array[String]) = { | |
Set(a: _*) | |
} | |
def solveCase(engines: Array[String], queries: Array[String]): Int = { | |
var enginesLeft = arrayToSet(engines) | |
var switches = 0 | |
for (query: String <- queries) { | |
enginesLeft -= query | |
if (enginesLeft.isEmpty) { | |
enginesLeft = arrayToSet(engines) | |
enginesLeft -= query | |
switches += 1 | |
} | |
} | |
switches | |
} | |
def main(args: Array[String]) { | |
val testCount = readLine().toInt | |
for (i <- 0 until testCount) { | |
// test case parameters | |
val engineCount = readLine().toInt | |
val engines = new Array [String] (engineCount) | |
for (j <- 0 until engineCount) { | |
engines(j) = readLine() | |
} | |
val queryCount = readLine().toInt | |
val queries = new Array [String] (queryCount) | |
for (j <- 0 until queryCount) { | |
queries(j) = readLine() | |
} | |
val switches = solveCase(engines, queries) | |
println("Case #%d: %d".format(i + 1, switches)) | |
} | |
} | |
} | |
// time cat large.in | scala-2.9 -classpath . SelfSearch > large.out | |
// real 0m0.534s | |
// user 0m0.673s | |
// sys 0m0.069s |
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
(use extras) | |
(define read-lines | |
(lambda () | |
(let ((line (read-line))) | |
(cond | |
((eof-object? line) '()) | |
(else (cons line (read-lines))))))) | |
(define join | |
(lambda (lines) | |
(apply string-append lines))) | |
(define char-as-int | |
(lambda (char) | |
;; ord(0) -> 48 | |
(- (char->integer char) 48))) | |
;; srfi-1 take is strict about list length | |
(define take | |
(lambda (l k) | |
(cond | |
((null? l) '()) | |
((<= k 0) '()) | |
(else (cons (car l) | |
(take (cdr l) (- k 1))))))) | |
(define quintets | |
(lambda (l) | |
(cond | |
((null? l) '()) | |
(else (cons (take l 5) | |
(quintets (cdr l))))))) | |
(define product | |
(lambda (l) | |
(apply * l))) | |
(let* ((lines (join (read-lines))) | |
(num-list (map char-as-int (string->list lines))) | |
(products (map product (quintets num-list)))) | |
(print (apply max products))) | |
;real 0m0.013s | |
;user 0m0.007s | |
;sys 0m0.007s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment