結果

問題 No.2283 Prohibit Three Consecutive
ユーザー 👑 箱
提出日時 2023-04-23 18:47:45
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 767 ms / 2,000 ms
コード長 2,367 bytes
コンパイル時間 15,372 ms
コンパイル使用メモリ 456,960 KB
実行使用メモリ 105,208 KB
最終ジャッジ日時 2024-04-25 14:05:00
合計ジャッジ時間 24,061 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 322 ms
59,264 KB
testcase_01 AC 320 ms
59,152 KB
testcase_02 AC 323 ms
59,264 KB
testcase_03 AC 742 ms
104,452 KB
testcase_04 AC 715 ms
105,208 KB
testcase_05 AC 620 ms
96,636 KB
testcase_06 AC 668 ms
97,020 KB
testcase_07 AC 319 ms
59,276 KB
testcase_08 AC 409 ms
102,424 KB
testcase_09 AC 410 ms
102,496 KB
testcase_10 AC 439 ms
103,744 KB
testcase_11 AC 433 ms
103,724 KB
testcase_12 AC 602 ms
99,248 KB
testcase_13 AC 767 ms
98,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import java.util.*
import kotlin.math.*

fun PrintWriter.solve() {
    val numCases = nextInt()
    case@ for (_i in 0 until numCases) {
        val n = nextInt()
        val s = nextLine()
        var t = s + s
        for (i in 0 until n - 1) {
            if (s[i] != '?' && s[i + 1] != '?') {
                val s0 = s.slice(i until n) + s.slice(0 until i)
                t = s0 + s0
                break
            }
        }
        if (s[0] != '?' && s[n - 1] != '?') {
            val s0 = s[n - 1] + s.slice(0 until n - 1)
            t = s0 + s0
        }
        val dp = Array(2 * n) { Array(2) { Array(2) { false } } }
        val x0 = if (t[0] == '0') arrayOf(0) else if (t[0] == '1') arrayOf(1) else arrayOf(0, 1)
        val x1 = if (t[1] == '0') arrayOf(0) else if (t[1] == '1') arrayOf(1) else arrayOf(0, 1)
        for (c0 in x0) {
            for (c1 in x1) {
                dp[1][c1][c0] = true
            }
        }
        for (i in 2 until 2 * n) {
            val x = if (t[i] == '0') arrayOf(0) else if (t[i] == '1') arrayOf(1) else arrayOf(0, 1)
            for (c0 in arrayOf(0, 1)) {
                for (c1 in arrayOf(0, 1)) {
                    if (dp[i - 1][c1][c0]) {
                        for (c in x) {
                            if (c0 == c1 && c1 == c) {
                                continue
                            }
                            dp[i][c][c1] = true
                        }
                    }
                }
            }
        }
        val b = dp[2 * n - 1][0][0] || dp[2 * n - 1][0][1] || dp[2 * n - 1][1][0] || dp[2 * n - 1][1][1]
        println(if (b) "Yes" else "No")
    }
}

fun main() {
    Thread(null, {
        val writer = PrintWriter(System.out, false)
        writer.solve()
        writer.flush()
    }, "solve", abs(1L.shl(26)))
        .apply { setUncaughtExceptionHandler { _, e -> e.printStackTrace(); kotlin.system.exitProcess(1) } }
        .apply { start() }.join()
}

// region Scanner
private var st = StringTokenizer("")
private val br = System.`in`.bufferedReader()

fun next(): String {
    while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())
    return st.nextToken()
}

fun nextInt() = next().toInt()
fun nextLong() = next().toLong()
fun nextLine() = br.readLine()
fun nextDouble() = next().toDouble()
// endregion
0