結果

問題 No.2283 Prohibit Three Consecutive
ユーザー LyricalMaestro
提出日時 2025-01-18 20:13:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 241 ms / 2,000 ms
コード長 1,701 bytes
コンパイル時間 591 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 78,648 KB
最終ジャッジ日時 2025-01-18 20:13:43
合計ジャッジ時間 3,047 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

# https://yukicoder.me/problems/no/2283


def solve(N, S):

    # state = 0: (0, 0), 1 = (0, 1), 2 = (1, 0), 3 = (1, 1)
    state_tuples = ((0, 0), (0, 1), (1, 0), (1, 1))
    for init_state_index, init_state in enumerate(state_tuples):
        is_ok = True
        for i in range(2):
            s = S[i]
            if s not in (str(init_state[i]), "?"):
                is_ok = False
                break
        if not is_ok:
            continue

        dp = [False] * 4
        dp[init_state_index] = True
        for i in range(2, N):
            new_dp = [False] * 4
            s = S[i]
            if s in ("1", "?"):
                new_dp[1] |= dp[0]
                new_dp[3] |= dp[1]
                new_dp[1] |= dp[2]
            if s in ("0", "?"):
                new_dp[2] |= dp[1]
                new_dp[0] |= dp[2]
                new_dp[2] |= dp[3]
            dp = new_dp

        for j in range(4):
            if dp[j]:
                last_array = list(state_tuples[j]) + list(init_state)
                last_array = "".join(map(str, last_array))

                if last_array.startswith("000"):
                    continue
                if last_array.startswith("111"):
                    continue
                if last_array.endswith("000"):
                    continue
                if last_array.endswith("111"):
                    continue

                return "Yes"
        
    return "No"








def main():
    T = int(input())
    answers = []
    for _ in range(T):
        N = int(input())
        S = input()
        ans = solve(N, S)
        answers.append(ans)

    for ans in answers:
        print(ans)

if __name__ == "__main__":
    main()
0