Skip to content

Instantly share code, notes, and snippets.

@theodoreLee
Created July 31, 2015 13:28
Show Gist options
  • Save theodoreLee/51781c5e90603efea295 to your computer and use it in GitHub Desktop.
Save theodoreLee/51781c5e90603efea295 to your computer and use it in GitHub Desktop.
import java.io.FileOutputStream
import scala.io.Source
object Bilingual {
val INPUT = "test.in"
val OUTPUT = INPUT.takeWhile(_ != '.') + ".out"
val isConsole = true
def main(args: Array[String]): Unit = {
val itr = Source.fromFile(INPUT).getLines()
val stream = if (isConsole) Console.out else new FileOutputStream(OUTPUT)
try {
Console.withOut(stream) {
val sets = itr.next().toInt
(1 to sets).foreach { set =>
val sentences = (1 to itr.next().toInt).toList.map {_ =>
itr.next().split(' ').toList
}
println(f"Case #$set: ${minimumNumberOfWords(sentences)}")
}
}
} finally {
stream.flush()
if (!isConsole) stream.close()
}
}
def minimumNumberOfWords(sentences:List[List[String]]) = {
val List(en,fr) = sentences.take(2)
val bilingualWords = en intersect fr
val dOne = sentences.map(sentence => sentence.filterNot(bilingualWords.contains(_)))
val words = sentences.flatten.toSet
val paths =words.map { word =>
val list = for {
i <- sentences.indices.toList
if i != 0 && sentences(i).contains(word)
} yield i
(word, list)
}.toMap
search(dOne.head.map(List(_)), paths, bilingualWords.length)
def search(targets:List[List[String]], map:Map[String,List[Int]], accr:Int):Int = {
println(targets.mkString("="*50,"\n","-"*50))
targets match {
case Nil => accr
case x :: xs =>
val list = map.getOrElse(x.head, Nil)
if (list.contains(1)) {// remove paths... 0
println(x.mkString(","))
//targets에 xs를 넘겨줄 것.
???
}
else {
val ret:List[List[String]] = for {
sentenceId <- list
if !x.contains(sentenceId.toString)
word <- sentences(sentenceId)
if !x.contains(word)
} yield {
word :: sentenceId.toString :: x
}
search(xs ++ ret, map, accr)
}
}
}
def remove(removeable:List[String], targets:List[List[String]], map:Map[String,List[Int]]):(List[List[String]], Map[String[List[Int]]]) = removeable match {
case x :: Nil => (targets, map)
case x :: y :: xs =>
val newTarget = targets.filterNot(list => list.drop(list.indexOf(x) + 1).headOption.contains(y))
// map.getOrElse(x,Nil)
}
1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment