문제링크
import java.io.BufferedReader
import java.io.BufferedWriter
private lateinit var bufferedReader: BufferedReader
private lateinit var bufferedWriter: BufferedWriter
fun main() {
bufferedReader = System.`in`.bufferedReader()
bufferedWriter = System.out.bufferedWriter()
val (n, k) = bufferedReader
.readLine()
.split(" ")
.map { it.toInt() }
val hp = bufferedReader.readLine()
.split("")
.filter { it.isNotBlank() }
val people = mutableListOf<Int>()
val visited = Array(n) { false }
for (i in hp.indices) {
if (hp[i] == "P") {
people.add(i)
visited[i] = true
}
}
var answer = 0
for (personIndex in people) {
for (j in (personIndex - k)..(personIndex + k)) {
if (j !in 0 until n) continue
if (visited[j]) continue
visited[j] = true
answer++
break
}
}
bufferedWriter.write("$answer")
bufferedReader.close()
bufferedWriter.close()
}