fun main() {
val bufferedReader = System.`in`.bufferedReader()
val bufferedWriter = System.out.bufferedWriter()
val startPositions = Array(26) { -1 }
val endPositions = Array(26) { -1 }
repeat(52) {
val position = bufferedReader.read() - 65
if (startPositions[position] == -1) {
startPositions[position] = it
} else {
endPositions[position] = it
}
}
var answer = 0
for (i in 0 until 26) {
for (j in 0 until 26) {
// A(s) B(s) A(e) B(e)
// 1) A(s) < B(s)
// 2) A(e) > B(s)
// 3) A(e) < B(e)
if (startPositions[i] < startPositions[j] && endPositions[i] > startPositions[j] && endPositions[i] < endPositions[j])
answer++
}
}
bufferedWriter.write("$answer")
bufferedReader.close()
bufferedWriter.close()
}
(이번 문제의 풀이는 생각보다 이해하는데 어려움이 있어서 주석을 추가해두었습니다.)