요깨비's LAB

[백준, 문자열, Kotlin] P.17413 단어 뒤집기2 본문

알고리즘(Kotlin)/문자열처리

[백준, 문자열, Kotlin] P.17413 단어 뒤집기2

요깨비 2021. 9. 3. 01:00

import java.util.*

fun main() {
    val scr = Scanner(System.`in`)

    var str = scr.nextLine();

    var len = str.length
    var idx = 0
    var startIdx = 0
    var endIdx = 0

    var result = ""
    var substr = ""
    var findIdx = 0

    while(idx < len) {

        if(str[0] == '<') {
            findIdx = str.indexOf('>')
            substr = str.substring(0, findIdx + 1)

            result += substr
            str = str.substring(findIdx + 1)
            len = str.length
        }
        else {
            findIdx = str.indexOfFirst {
                it == '<'
            }
            var splitedStrs = emptyList<String>()

            if(findIdx == -1) {
                idx = len

                substr = str.substring(0, len)
                splitedStrs = substr.split(" ")

                for(s in splitedStrs) {
                    result += s.reversed() + " "
                }

                val last = result.lastIndexOf(" ")
                result = result.substring(0, last)
            }else {
                idx = findIdx
                substr = str.substring(0, findIdx)

                splitedStrs = substr.split(" ")

                for(s in splitedStrs) {
                    result += s.reversed() + " "
                }

                val last = result.lastIndexOf(" ")
                result = result.substring(0, last)
                str = str.substring(findIdx)
                len = str.length
                idx = 0 // 요거 다시 생각
            }
        }
    }

    println(result)
}
Comments