https://coderun.yandex.ru/problem/substring-graph Легкая

Решение

import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import kotlin.collections.MutableMap
import kotlin.collections.MutableSet
import kotlin.collections.set
import kotlin.collections.getOrDefault
import kotlin.collections.iterator
import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.collections.forEach

fun main(args: Array<String>) {
    val reader = BufferedReader(InputStreamReader(System.`in`))
    val writer = BufferedWriter(OutputStreamWriter(System.out))

    val vertices: MutableSet<String> = mutableSetOf()
    val edges: MutableMap<Pair<String, String>, Int> = mutableMapOf()

    val t = reader.readLine().toInt()

    repeat(t) {
        val word = reader.readLine()
        if (word.length >= 4) {
            var prevSubstring = word.substring(0, 3)
            vertices.add(prevSubstring)

            for (i in 1..word.length - 3) {
                val currentSubstring = word.substring(i, i + 3)
                vertices.add(currentSubstring)

                val edge = Pair(prevSubstring, currentSubstring)
                edges[edge] = edges.getOrDefault(edge, 0) + 1

                prevSubstring = currentSubstring
            }
        }
    }

    writer.write("${vertices.size}\\n")
    writer.write("${edges.size}\\n")
    edges.forEach { (edgePair, weight) ->
        writer.write("${edgePair.first} ${edgePair.second} $weight\\n")
    }

    writer.flush()
    reader.close()
    writer.close()
}