Advertisement
vkazar

Untitled

Oct 30th, 2023
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.63 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<String, MutableMap<Int, Int>> {
  11.         for (i in 1..docCount) {
  12.             val tokenizer = StringTokenizer(buffer.readLine())
  13.             while (tokenizer.hasMoreElements()) {
  14.                 val word = tokenizer.nextToken()
  15.                 putIfAbsent(word, mutableMapOf())
  16.                 val docs = this[word]!!
  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<String>> = List(buffer.readLine().toInt()) {
  25.         val tokenizer = StringTokenizer(buffer.readLine())
  26.         buildSet {
  27.             while (tokenizer.hasMoreElements())
  28.                 add(tokenizer.nextToken())
  29.         }
  30.     }
  31.     queries.joinToString("\n") { query: Set<String> ->
  32.  
  33.         buildMap<Int, Int> {
  34.             query.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