結果

問題 No.204 ゴールデン・ウィーク(2)
ユーザー rutilicus
提出日時 2020-10-16 01:25:06
言語 Kotlin
(2.1.0)
結果
WA  
実行時間 -
コード長 1,634 bytes
コンパイル時間 13,026 ms
コンパイル使用メモリ 447,684 KB
実行使用メモリ 51,956 KB
最終ジャッジ日時 2024-07-20 20:14:25
合計ジャッジ時間 28,044 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45 WA * 1
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:42:13: warning: 'appendln(Int): kotlin.text.StringBuilder /* = java.lang.StringBuilder */' is deprecated. Use appendLine instead. Note that the new method always appends the line feed character '\n' regardless of the system line separator.
    builder.appendln(ans)
            ^

ソースコード

diff #

import kotlin.math.max

fun main() {
    val builder = StringBuilder()

    val d = readInputLine().toInt()

    val c = readInputLine() + readInputLine()

    val runLength = runLength(c).toMutableList()

    if (runLength[0].first == 'x') {
        runLength[0] = runLength[0].copy(second = 15)
    } else {
        runLength.add(0, Pair('x', 15))
    }

    if (runLength[runLength.lastIndex].first == 'x') {
        runLength[runLength.lastIndex] = runLength[runLength.lastIndex].copy(second = 15)
    } else {
        runLength.add(Pair('x', 15))
    }

    var ans = 0

    val arr = runLength.toTypedArray()

    for ((i, p) in arr.withIndex()) {
        if (p.first != 'o') {
            continue
        }

        ans = max(ans, when(arr[i - 1].second - d) {
            in Int.MIN_VALUE..0 -> p.second + arr[i - 1].second + arr[i - 2].second
            else -> p.second + d } )

        ans = max(ans, when(arr[i + 1].second - d) {
            in Int.MIN_VALUE..0 -> p.second + arr[i + 1].second + arr[i + 2].second
            else -> p.second + d } )
    }

    builder.appendln(ans)

    print(builder.toString())
}

fun readInputLine(): String {
    return readLine()!!
}

fun runLength(s: String): List<Pair<Char, Int>> {
    val ret = mutableListOf<Pair<Char, Int>>()

    if (s.isNotEmpty()) {
        var prev = s[0]
        var cnt = 0

        for (c in s) {
            if (c == prev) {
                cnt++
            } else {
                ret.add(Pair(prev, cnt))
                prev = c
                cnt = 1
            }
        }

        ret.add(Pair(prev, cnt))
    }

    return ret.toList()
}
0