본문 바로가기
  • 개발하는 곰돌이
Algorithm/BOJ

[Kotlin] 백준 1599 : 민식어

by 개발하는 곰돌이 2023. 3. 18.

문제 링크

 

1599번: 민식어

무엇인가를 창조하는 것은 어렵다. 오민식은 지금까지 어려운 다른나라의 언어를 쓰면서 백성들이 고통에 받는 것을 슬퍼하고 새로운 언어를 만들고자 했다. 그는 창조의 고통에 시달리던 중에

www.acmicpc.net


문제 해설

주어진 문자열들을 민식어의 순서에 맞게 정렬해야한다. 주어진 민식어 단어들을 사전순으로 정렬하기 위해 아래와 같이 민식어 단어의 모든 알파벳을 대응하는 순서의 영어 알파벳으로 치환하는 메소드를 작성한다. 이 때, n과 g가 붙어있는 ng의 경우는 무조건 하나의 알파벳으로 생각한다는 조건이 있기 때문에 ng를 가장 먼저 치환한다.

private fun String.toMinsik() = this.replace("ng", "L")
    .replace("a", "A")
    .replace("b", "B")
    .replace("k", "C")
    .replace("d", "D")
    .replace("e", "E")
    .replace("g", "F")
    .replace("h", "G")
    .replace("i", "H")
    .replace("l", "I")
    .replace("m", "J")
    .replace("n", "K")
    .replace("o", "M")
    .replace("p", "N")
    .replace("r", "O")
    .replace("s", "P")
    .replace("t", "Q")
    .replace("u", "R")
    .replace("w", "S")
    .replace("y", "T")

이후에는 입력으로 주어지는 민식어 단어들을 TreeMap에 (key : 치환한 민식어 단어, value : 민식어 단어)로 저장한 후, 모든 value를 출력하면 된다.

Code

import java.util.TreeMap

fun main() = with(System.`in`.bufferedReader()) {
    val bw = System.out.bufferedWriter()
    TreeMap<String, String>().also { map ->
        repeat(readLine().toInt()) {
            val str = readLine()
            map[str.toMinsik()] = str
        }
        map.forEach { bw.write("${it.value}\n") }
    }
    bw.close()
}

private fun String.toMinsik() = this.replace("ng", "L")
    .replace("a", "A")
    .replace("b", "B")
    .replace("k", "C")
    .replace("d", "D")
    .replace("e", "E")
    .replace("g", "F")
    .replace("h", "G")
    .replace("i", "H")
    .replace("l", "I")
    .replace("m", "J")
    .replace("n", "K")
    .replace("o", "M")
    .replace("p", "N")
    .replace("r", "O")
    .replace("s", "P")
    .replace("t", "Q")
    .replace("u", "R")
    .replace("w", "S")
    .replace("y", "T")

댓글