Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sprint4.final
- import java.io.BufferedReader
- import java.io.InputStreamReader
- import java.util.*
- fun main() {
- val buffer = BufferedReader(InputStreamReader(System.`in`))
- val docCount = buffer.readLine().toInt()
- val dictionary = buildMap<Int, MutableMap<Int, Int>> {
- for (i in 1..docCount) {
- val tokenizer = StringTokenizer(buffer.readLine())
- while (tokenizer.hasMoreElements()) {
- val wordHash = tokenizer.nextToken().hashCode()
- putIfAbsent(wordHash, mutableMapOf())
- val docs = this[wordHash]!!
- docs.putIfAbsent(i, 0)
- docs[i] = docs[i]!! + 1
- }
- }
- }
- .map { it.key to it.value.toMap() }.toMap()
- val queries: List<Set<Int>> = List(buffer.readLine().toInt()) {
- val tokenizer = StringTokenizer(buffer.readLine())
- buildSet {
- while (tokenizer.hasMoreElements())
- add(tokenizer.nextToken().hashCode())
- }
- }
- queries.joinToString("\n") { queryHashes: Set<Int> ->
- buildMap<Int, Int> {
- queryHashes.forEach { word ->
- dictionary[word]
- ?.let { docs ->
- docs.forEach { (t, u) ->
- putIfAbsent(t, 0)
- this[t] = this[t]!! + u
- }
- }
- }
- }
- .entries
- .sortedWith(compareByDescending<Map.Entry<Int, Int>> { it.value }.thenBy { it.key })
- .take(5)
- .joinToString(" ") { it.key.toString() }
- }
- .let(::println)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement