結果

問題 No.2283 Prohibit Three Consecutive
ユーザー umimelumimel
提出日時 2023-04-28 21:56:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 3,973 bytes
コンパイル時間 2,699 ms
コンパイル使用メモリ 180,204 KB
実行使用メモリ 10,732 KB
最終ジャッジ日時 2024-04-28 23:32:47
合計ジャッジ時間 2,969 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 35 ms
7,724 KB
testcase_04 AC 43 ms
6,856 KB
testcase_05 AC 37 ms
5,376 KB
testcase_06 AC 38 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 42 ms
10,600 KB
testcase_09 AC 25 ms
10,732 KB
testcase_10 AC 24 ms
10,728 KB
testcase_11 AC 25 ms
10,724 KB
testcase_12 AC 14 ms
5,376 KB
testcase_13 AC 29 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second

const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll INF = 1LL << 60;
const ll MAX_N = 2e5;

void solve(){
    ll n; cin >> n;
    string s; cin >> s;

    //初期化
    bool ok = false;
    if(s[0]=='?' && s[1]=='?'){
        rep(a, 2) rep(b, 2){
            string t="";
            t+=(char)('0'+a);
            t+=(char)('0'+b);
            for(ll i=2; i<n; i++) t+=s[i];
            t+=(char)('0'+a);
            t+=(char)('0'+b);

            vector<vector<bool>> dp(n+2, vector<bool>(4, false));
            dp[1][2*a+b]=true;
            for(ll i=2; i<n+2; i++){
                if(t[i]=='0' || t[i]=='?'){ //0とする
                    for(ll j=1; j<4; j++){
                        dp[i][(j%2)*2] = dp[i][(j%2)*2] | dp[i-1][j];
                    }
                }

                if(t[i]=='1' || t[i]=='?'){ //1とする
                    for(ll j=0; j<3; j++){
                        dp[i][(j%2)*2+1] = dp[i][(j%2)*2+1] | dp[i-1][j];
                    }
                }
            }

            rep(j, 4) ok = ok | dp[n+1][j];
        }
    }else if(s[0]=='?'){
        rep(a, 2){
            string t="";
            t+=(char)('0'+a);
            t+=s[1];
            for(ll i=2; i<n; i++) t+=s[i];
            t+=(char)('0'+a);
            t+=s[1];

            vector<vector<bool>> dp(n+2, vector<bool>(4, false));
            dp[1][2*a+(s[1]=='1')]=true;
            for(ll i=2; i<n+2; i++){
                if(t[i]=='0' || t[i]=='?'){ //0とする
                    for(ll j=1; j<4; j++){
                        dp[i][(j%2)*2] = dp[i][(j%2)*2] | dp[i-1][j];
                    }
                }

                if(t[i]=='1' || t[i]=='?'){ //1とする
                    for(ll j=0; j<3; j++){
                        dp[i][(j%2)*2+1] = dp[i][(j%2)*2+1] | dp[i-1][j];
                    }
                }
            }

            rep(j, 4) ok = ok | dp[n+1][j];
        }
    }else if(s[1]=='?'){
        rep(b, 2){
            string t="";
            t+=s[0];
            t+=(char)('0'+b);
            for(ll i=2; i<n; i++) t+=s[i];
            t+=s[0];
            t+=(char)('0'+b);

            vector<vector<bool>> dp(n+2, vector<bool>(4, false));
            dp[1][2*(s[0]=='1')+b]=true;
            for(ll i=2; i<n+2; i++){
                if(t[i]=='0' || t[i]=='?'){ //0とする
                    for(ll j=1; j<4; j++){
                        dp[i][(j%2)*2] = dp[i][(j%2)*2] | dp[i-1][j];
                    }
                }

                if(t[i]=='1' || t[i]=='?'){ //1とする
                    for(ll j=0; j<3; j++){
                        dp[i][(j%2)*2+1] = dp[i][(j%2)*2+1] | dp[i-1][j];
                    }
                }
            }

            rep(j, 4) ok = ok | dp[n+1][j];
        }
    }else{
        string t="";
        t+=s[0];
        t+=s[1];
        for(ll i=2; i<n; i++) t+=s[i];
        t+=s[0];
        t+=s[1];

        vector<vector<bool>> dp(n+2, vector<bool>(4, false));
        dp[1][2*(s[0]=='1')+(s[1]=='1')]=true;
        for(ll i=2; i<n+2; i++){
            if(t[i]=='0' || t[i]=='?'){ //0とする
                for(ll j=1; j<4; j++){
                    dp[i][(j%2)*2] = dp[i][(j%2)*2] | dp[i-1][j];
                }
            }

            if(t[i]=='1' || t[i]=='?'){ //1とする
                for(ll j=0; j<3; j++){
                    dp[i][(j%2)*2+1] = dp[i][(j%2)*2+1] | dp[i-1][j];
                }
            }
        }

        rep(j, 4) ok = ok | dp[n+1][j];
    }

    if(ok) cout << "Yes" << '\n';
    else cout << "No" << '\n';
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    ll t; cin >> t;
    rep(i, t) solve();
}
0