Advertisement
vkazar

Untitled

Oct 29th, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.67 KB | None | 0 0
  1. package sprint4.final
  2.  
  3. import java.io.BufferedReader
  4. import java.io.InputStreamReader
  5. import java.util.*
  6.  
  7. fun main() {
  8.     val buffer = BufferedReader(InputStreamReader(System.`in`))
  9.     val docCount = buffer.readLine().toInt()
  10.     val dictionary = buildMap<Int, MutableMap<Int, Int>> {
  11.         for (i in 1..docCount) {
  12.             val tokenizer = StringTokenizer(buffer.readLine())
  13.             while (tokenizer.hasMoreElements()) {
  14.                 val wordHash = tokenizer.nextToken().hashCode()
  15.                 putIfAbsent(wordHash, mutableMapOf())
  16.                 val docs = this[wordHash]!!
  17.                 docs.putIfAbsent(i, 0)
  18.                 docs[i] = docs[i]!! + 1
  19.             }
  20.         }
  21.     }
  22.         .map { it.key to it.value.toMap() }.toMap()
  23.  
  24.     val queries: List<Set<Int>> = List(buffer.readLine().toInt()) {
  25.         val tokenizer = StringTokenizer(buffer.readLine())
  26.         buildSet {
  27.             while (tokenizer.hasMoreElements())
  28.                 add(tokenizer.nextToken().hashCode())
  29.         }
  30.     }
  31.     queries.joinToString("\n") { queryHashes: Set<Int> ->
  32.  
  33.         buildMap<Int, Int> {
  34.             queryHashes.forEach { word ->
  35.                 dictionary[word]
  36.                     ?.let { docs ->
  37.                         docs.forEach { (t, u) ->
  38.                             putIfAbsent(t, 0)
  39.                             this[t] = this[t]!! + u
  40.                         }
  41.                     }
  42.             }
  43.         }
  44.             .entries
  45.             .sortedWith(compareByDescending<Map.Entry<Int, Int>> { it.value }.thenBy { it.key })
  46.             .take(5)
  47.             .joinToString(" ") { it.key.toString() }
  48.     }
  49.         .let(::println)
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement