結果
問題 | No.2283 Prohibit Three Consecutive |
ユーザー |
|
提出日時 | 2023-04-23 18:47:45 |
言語 | Kotlin (2.1.0) |
結果 |
AC
|
実行時間 | 817 ms / 2,000 ms |
コード長 | 2,367 bytes |
コンパイル時間 | 15,328 ms |
コンパイル使用メモリ | 453,332 KB |
実行使用メモリ | 100,476 KB |
最終ジャッジ日時 | 2024-11-08 01:08:12 |
合計ジャッジ時間 | 24,257 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 13 |
ソースコード
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