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<String, MutableMap<Int, Int>> {
- for (i in 1..docCount) {
- val tokenizer = StringTokenizer(buffer.readLine())
- while (tokenizer.hasMoreElements()) {
- val word = tokenizer.nextToken()
- putIfAbsent(word, mutableMapOf())
- val docs = this[word]!!
- docs.putIfAbsent(i, 0)
- docs[i] = docs[i]!! + 1
- }
- }
- }
- .map { it.key to it.value.toMap() }.toMap()
- val queries: List<Set<String>> = List(buffer.readLine().toInt()) {
- val tokenizer = StringTokenizer(buffer.readLine())
- buildSet {
- while (tokenizer.hasMoreElements())
- add(tokenizer.nextToken())
- }
- }
- queries.joinToString("\n") { query: Set<String> ->
- buildMap<Int, Int> {
- query.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