From cf227e1600bb3b1d239ce43a3f38412d89498fd8 Mon Sep 17 00:00:00 2001 From: soopeach Date: Thu, 14 Mar 2024 18:23:53 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=AC=B8=EC=9E=90=EC=97=B4=20=EA=B2=8C?= =?UTF-8?q?=EC=9E=842=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...354\227\264 \352\262\214\354\236\204 2.kt" | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 "src/main/kotlin/hyunsoo/54week/\353\254\270\354\236\220\354\227\264 \352\262\214\354\236\204 2.kt" diff --git "a/src/main/kotlin/hyunsoo/54week/\353\254\270\354\236\220\354\227\264 \352\262\214\354\236\204 2.kt" "b/src/main/kotlin/hyunsoo/54week/\353\254\270\354\236\220\354\227\264 \352\262\214\354\236\204 2.kt" new file mode 100644 index 00000000..c9bb341f --- /dev/null +++ "b/src/main/kotlin/hyunsoo/54week/\353\254\270\354\236\220\354\227\264 \352\262\214\354\236\204 2.kt" @@ -0,0 +1,98 @@ +package hyunsoo.`54week` + +/** + * + * <문제> + * [문자열 게임 2](https://www.acmicpc.net/problem/20437) + * + * - 아이디어 + * + * - 트러블 슈팅 + * + */ +class `전현수_문자열_게임_2` { + + val br = System.`in`.bufferedReader() + + private data class CharWithIndex(val char: Char, val index: Int) + + fun solution() { + + val t = br.readLine().toInt() + + repeat(t) { + + var firstConditionResult = Int.MAX_VALUE + var secondConditionResult = -1 + + val w = br.readLine() + val k = br.readLine().toInt() + + val alphabetCounter = IntArray(26) + val validCharList = mutableSetOf() + + val validCharInfoList = mutableListOf() + + if (k == 1) { + println("1 1") + return@repeat + } + + w.forEachIndexed { index, char -> + val curCharCnt = ++alphabetCounter[char.toSequence()] + if (k <= curCharCnt) + validCharList.add(char) + } + + w.forEachIndexed { index, char -> + if (validCharList.contains(char)) { + validCharInfoList.add(CharWithIndex(char, index)) + } + } + + val sortedValidCharInfoList = validCharInfoList + .sortedWith( + compareBy { it.char } + .thenBy { it.index } + ) + + for (i in sortedValidCharInfoList.indices) { + + val (target, startIndex) = sortedValidCharInfoList[i] + var targetIncludedCnt = 1 + + for (j in i + 1 until sortedValidCharInfoList.size) { + + val (endTarget, endIndex) = sortedValidCharInfoList[j] + if (target != endTarget) { + break + } + targetIncludedCnt++ + if (k != targetIncludedCnt) continue + + val curLength = endIndex - startIndex + 1 + + if (curLength < firstConditionResult) firstConditionResult = curLength + if (secondConditionResult < curLength) secondConditionResult = curLength + + } + } + + if (firstConditionResult == Int.MAX_VALUE || secondConditionResult == -1) { + println(-1) + } else { + println("$firstConditionResult $secondConditionResult") + } + + } + } + + private fun Char.toSequence(): Int { + return this.code - 97 + } + +} + +fun main() { + 전현수_문자열_게임_2().solution() +} \ No newline at end of file