Skip to content

Instantly share code, notes, and snippets.

@alts
Created September 20, 2012 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alts/3754046 to your computer and use it in GitHub Desktop.
Save alts/3754046 to your computer and use it in GitHub Desktop.
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
(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