๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ฏ | ๋ฐฑ์ค€/๐Ÿ˜ | Gold

[Kotlin, G4] ๋ฐฑ์ค€ 9935๋ฒˆ ๋ฌธ์ž์—ด ํญ๋ฐœ

by immgga 2024. 10. 1.

์ถœ์ฒ˜: unsplash.com

 

๋ฌธ์ž์—ด ํญ๋ฐœ(9935๋ฒˆ)

Gold 4

#์ž๋ฃŒ ๊ตฌ์กฐ #๋ฌธ์ž์—ด #์Šคํƒ

https://www.acmicpc.net/problem/9935

 

๋ฌธ์ œ ๋‚ด์šฉ

 

 

๋ฌธ์ œ ์ ‘๊ทผ

์ฒซ ๋ฒˆ์งธ ์ค„์˜ ๋ฌธ์ž์—ด์—์„œ ๋‘ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ๋•Œ๊นŒ์ง€ ๋ฌธ์ž์—ด์„ ์ œ๊ฑฐํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ.

๋ฌธ์ž์—ด์ด ์ œ๊ฑฐ๋˜๊ณ  ๋‚จ์€ ๋ฌธ์ž์—ด๋“ค์€ ์„œ๋กœ ๋ถ™์–ด์„œ ์ƒˆ ๋ฌธ์ž์—ด์ด ๋œ๋‹ค.

 

๋‹จ์ˆœํžˆ ๋ฌธ์ž์—ด์„ ํ™•์ธํ•ด ๋ณด๋ฉด์„œ ๋ฌธ์ž์—ด์„ ์ œ๊ฑฐํ•ด ๋‚˜๊ฐ€๋ฉด ๋˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์ด๋‹ค.

๊ทธ๋ž˜์„œ ์ผ๋‹จ์€ replace๋ฅผ ๋ฐ˜๋ณต์‹œ์ผœ์„œ ์ง„ํ–‰ํ•ด ๋ณด์•˜๋‹ค.

while (true) {
    val replaceRes = string.replace(removeStr, "")
    if (replaceRes != string) string = replaceRes
    else break
}

๊ฒฐ๊ณผ๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ, ์• ์ดˆ์— replace๋งŒ์œผ๋กœ ํ’€๋ฆฌ๋Š” ๊ฑธ ๊ธฐ๋Œ€ํ•˜์ง€๋„ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

 

๋‹ค์Œ์œผ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ถ„๋ฅ˜์— ์žˆ๋Š” ์Šคํƒ์„ ์‚ฌ์šฉํ•ด ๋ณด์•˜๋‹ค.

stack์— ๋ฌธ์ž๋ฅผ ํ•˜๋‚˜์”ฉ ๋„ฃ์œผ๋ฉด์„œ ํญ๋ฐœ์‹œ์ผœ์•ผ ํ•  ๋ฌธ์ž์—ด์ด ์žˆ์œผ๋ฉด stack์—์„œ ๊ทธ ๊ฐ’์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•ด ๋ณด์•˜๋‹ค.

val stack = Stack<String>()

for (i in string.indices) {
    stack.add(string[i])
    if (string[i] == boomString.last().toString() && stack.size >= boomString.length) {
        var subRes = ""
        for (j in stack.lastIndex downTo stack.lastIndex - boomString.lastIndex) {
            subRes += stack[j]
        }

        if (subRes == boomString.reversed()) {
            for (j in boomString.indices) stack.pop()
        }
    }
}

์ด ์ฝ”๋“œ๋„ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์œ„์˜ stack ๋ฐฉ์‹๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋ฉด์„œ ๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ๋„ ๊ฐ€๋Šฅํ•œ stringBuilder๋กœ ๊ตฌํ˜„์„ ์‹œ๋„ํ–ˆ๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ์ฝ”๋“œ

๋”๋ณด๊ธฐ
import java.io.BufferedReader
import java.io.InputStreamReader

fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {
    val string = readLine()
    val boomString = readLine()
    val sb = StringBuilder()
    var subRes: String

    for (i in string.indices) {
        sb.append(string[i])
        if (string[i] == boomString.last() && sb.length >= boomString.length) {
            subRes = sb.substring(sb.lastIndex - boomString.lastIndex, sb.length)
            if (subRes == boomString) {
                sb.delete(sb.lastIndex - boomString.lastIndex, sb.length)
            }
        }
    }

    if (sb.isEmpty()) println("FRULA")
    else println(sb)
}

 

๋ฌธ์ œ ํ’€์ด

์šฐ์„  ๊ณ„์† stringBuilder์— ๋ฌธ์ž๋ฅผ ์ง‘์–ด๋„ฃ๋Š”๋‹ค.

ํ˜„์žฌ stringBuilder์˜ ๊ธธ์ด๊ฐ€ ํญ๋ฐœ ๋ฌธ์ž์—ด๋ณด๋‹ค ๊ธธ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด์„œ ์ง‘์–ด๋„ฃ๋Š” ๋ฌธ์ž์—ด์ด ํญ๋ฐœ ๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž์™€ ๊ฐ™์œผ๋ฉด ํญ๋ฐœ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋งŒํผ ์•ž์˜ ๋ฌธ์ž๋“ค์„ ํ™•์ธํ•œ๋‹ค.

ํ™•์ธํ•œ ๋ฌธ์ž๊ฐ€ ํญ๋ฐœ ๋ฌธ์ž์—ด๊ณผ ๋™์ผํ•˜๋ฉด ๊ทธ ๋ฌธ์ž๋“ค์„ ์ง€์›Œ์ค€๋‹ค.

 

๊ทธ๋Ÿฌ๋ฉด ์œ„์™€ ๊ฐ™์€ ์ž…๋ ฅ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ,

aaaaabbbbb
ab

sb์—๋Š” a๊ฐ€ ๋‹ค ๋“ค์–ด๊ฐ€๊ณ  b๊ฐ€ ๋“ค์–ด์˜ฌ ์ฐจ๋ก€๊ฐ€ ๋œ๋‹ค๋ฉด ํญ๋ฐœ ๋ฌธ์ž์—ด์ธ ab์™€ ๋™์ผํ•œ ๊ฐ’๋“ค์„ ์‚ญ์ œํ•ด ๋‚˜๊ฐˆ ๊ฒƒ์ด๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์ •๋‹ต์€ ๋นˆ ๋ฌธ์ž์—ด์ด๊ธฐ ๋•Œ๋ฌธ์— FURLA๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •

2๊ฐ€์ง€์˜ ๋ฐฉ๋ฒ•์„ ์‹œ๋„ํ–ˆ๋‹ค.

replace๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์• ์ดˆ์— ๊ธฐ๋Œ€๋„ ์•ˆ ํ–ˆ๋‹ค. ๊ณจ๋“œ ๋ฌธ์ œ์ธ๋ฐ ์ด๋ฆฌ ์‰ฝ๊ฒŒ ํ’€๋ฆด ๋ฆฌ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์—ญ์‹œ๋‚˜ 40%๋Œ€์—์„œ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋‚˜์™”๋‹ค.

stack์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌธ์ž์—ด์ด ๊ธธ์–ด์ง€๋ฉด ์žก์•„๋จน๋Š” ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ปค์ ธ์„œ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋œจ๋Š” ๋“ฏํ–ˆ๋‹ค. ์ตœ์ ํ™”๋ฅผ ํ•œ๋‹ค๋ฉด ํ’€ ์ˆ˜ ์žˆ์„์ง€๋„ ๋ชจ๋ฅด๊ฒ ๋‹ค.

๋‚ด ๊ธฐ์ค€์— ์ฒด๊ฐ ๋‚œ์ด๋„๋Š” ์‹ค๋ฒ„ 2~์‹ค๋ฒ„ 1์ธ ๋“ฏํ•˜๋‹ค.

๊ตฌํ˜„์— ํฌ๊ฒŒ ์–ด๋ ค์›€์„ ๋Š๋ผ์ง€ ์•Š์•˜๋‹ค.

stack ๋Œ€์‹ ์— stringBuilder๋ฅผ ์“ฐ๋Š” ๊ฒŒ ๋” ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๊ฐ€ ํŽธํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์œผ๋‹ˆ ์ข‹๋‹ค.

728x90