URL : https://www.acmicpc.net/problem/14725
[ RESULT ]
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.InputStreamReader
import java.io.OutputStreamWriter
val br = BufferedReader(InputStreamReader(System.`in`))
val bw = BufferedWriter(OutputStreamWriter(System.out))
class TrieNode {
val children = mutableMapOf<String, TrieNode>()
var isEndOfWord = false
}
fun insert(root: TrieNode, words: List<String>) {
var node = root
for (word in words) {
node = node.children.getOrPut(word) { TrieNode() }
node.isEndOfWord = true
}
}
fun printAntHill(root: TrieNode, depth: Int = 0) {
val sortedKeys = root.children.keys.sorted()
for (key in sortedKeys) {
val node = root.children[key]!!
repeat(depth) { bw.write("--") }
bw.write("$key\n")
printAntHill(node, depth + 1)
}
}
fun main() {
val root = TrieNode()
val N = br.readLine()!!.toInt()
for (i in 0 until N) {
val input = br.readLine()!!.split(" ").drop(1)
insert(root, input)
}
printAntHill(root)
br.close()
bw.flush()
bw.close()
}
아무리 생각해도 풀이하기가 너무 어려워서 어려 블로그를 보고, 자료구조인 "트라이"를 공부하고 이를 적용시켰다.
Baekjoon(Kotlin) - 1920. 수 찾기 (0) | 2023.09.16 |
---|---|
Baekjoon(Kotlin) - 1931. 회의실 배정 (0) | 2023.09.15 |
Baekjoon(Kotlin) - Num. ProblemName (0) | 2023.09.11 |
Baekjoon(Kotlin) - 10986. 나머지 합 (0) | 2023.09.10 |
Baekjoon(Kotlin) - 11659. 구간 합 구하기 4 (0) | 2023.09.09 |