https://www.acmicpc.net/problem/1920
import scala.io.StdIn
object Constants{
final val VERBOSE = false
}
object Main{
def main(args:Array[String]):Unit={
val N:Int = StdIn.readInt()
if(Constants.VERBOSE) println(N)
val Target:Vector[Int] = StdIn.readLine().split(" ").map(x => x.toInt).toVector
if(Constants.VERBOSE) println(Target)
val M:Int = StdIn.readInt()
if(Constants.VERBOSE) println(M)
val Search:Vector[Int] = StdIn.readLine().split(" ").map(x => x.toInt).toVector
if(Constants.VERBOSE) println(Search)
Search.foreach(x => println(if(Target.contains(x)) 1 else 0))
}
}
결과: 시간초과
Set 도입했다.
Scala 실행시간은 2988ms.
Python이 232ms, C가 72ms인데 너무나도너무나도너무나도 느린 속도다.
이게 맞냐...? 왜이렇게 느려.
import scala.io.StdIn
import scala.collection.mutable.Set
object Constants{
final val VERBOSE = false
}
object Main{
def main(args:Array[String]):Unit={
val N:Int = StdIn.readInt()
if(Constants.VERBOSE) println(N)
val Target:Vector[Int] = StdIn.readLine().split(" ").map(x => x.toInt).toVector
var setTarget:Set[Int] = Set[Int]()
for (t <- Target) setTarget += t
if(Constants.VERBOSE) println(Target)
val M:Int = StdIn.readInt()
if(Constants.VERBOSE) println(M)
val Search:Vector[Int] = StdIn.readLine().split(" ").map(x => x.toInt).toVector
if(Constants.VERBOSE) println(Search)
for(s <- Search) {
if(setTarget contains s) println(1) else println(0)
}
}
}
좀더 최적화해봄. 사실, Array[Int]를 Vector로 바꾸는 과정은 불필요함.
import scala.io.StdIn
import scala.collection.mutable.Set
object Constants{
final val VERBOSE = false
}
object Main{
def main(args:Array[String]):Unit={
val N:Int = StdIn.readInt()
if(Constants.VERBOSE) println(N)
val Target = StdIn.readLine().split(" ").map(x => x.toInt)
var setTarget:Set[Int] = Set[Int]()
for (t <- Target) setTarget += t
if(Constants.VERBOSE) println(Target.getClass)
val M:Int = StdIn.readInt()
if(Constants.VERBOSE) println(M)
val Search = StdIn.readLine().split(" ").map(x => x.toInt)
if(Constants.VERBOSE) println(Search.getClass)
for(s <- Search) {
if(setTarget contains s) println(1) else println(0)
}
}
}
Scala Collection 자료형 참고
https://groups.google.com/g/scala-korea/c/j7ucQeI7Lyg?pli=1