백준 4821번
https://www.acmicpc.net/problem/4821
import java.io.*
import java.util.*
// input
private lateinit var br: BufferedReader
// variables
private var N = 0
private var str = ""
private lateinit var isVisited: BooleanArray
fun main() {
br = BufferedReader(InputStreamReader(System.`in`))
val bw = BufferedWriter(OutputStreamWriter(System.`out`))
val sb = StringBuilder()
while (true) {
if (!input()) break
solve()
sb.append(check()).append('\n')
}
bw.write(sb.toString())
bw.close()
} // End of main
private fun solve() {
val commaSt = StringTokenizer(str, ",")
while (commaSt.hasMoreTokens()) {
val temp = commaSt.nextToken()
val hypenSt = StringTokenizer(temp, "-")
val low = hypenSt.nextToken()
if (low.length > N.toString().length) {
continue
}
val lowNum = low.toInt()
if (lowNum > N) {
continue
}
if (!hypenSt.hasMoreTokens()) {
if (!isVisited[lowNum]) {
isVisited[lowNum] = true
}
continue
}
var high = hypenSt.nextToken()
if (high.length > N.toString().length) {
high = N.toString()
}
var highNum = high.toInt()
if (highNum > N) {
highNum = N
}
if (lowNum > highNum) {
continue
}
for (i in lowNum..highNum) {
if (!isVisited[i]) {
isVisited[i] = true
}
}
}
} // End of solve
private fun check(): Int {
var page = 0
for (i in 1..N) {
if (isVisited[i]) {
page++
}
}
return page
} // End of check
private fun input(): Boolean {
N = br.readLine().toInt()
if (N == 0) {
return false
}
str = br.readLine()
isVisited = BooleanArray(1_001)
return true
} // End of input