結果

問題 No.2283 Prohibit Three Consecutive
ユーザー lddlinanlddlinan
提出日時 2023-04-28 22:17:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 3,584 bytes
コンパイル時間 755 ms
コンパイル使用メモリ 87,256 KB
実行使用メモリ 8,192 KB
最終ジャッジ日時 2024-04-28 23:49:31
合計ジャッジ時間 1,448 ms
ジャッジサーバーID
(参考情報)
judge4 / 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 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 7 ms
5,376 KB
testcase_06 AC 7 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 7 ms
8,064 KB
testcase_09 AC 7 ms
8,064 KB
testcase_10 AC 6 ms
8,192 KB
testcase_11 AC 6 ms
8,192 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 8 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <algorithm>
#include <array>
#include <unordered_set>
#include <unordered_map>
#include <string>
using namespace std;

bool rcmp(int a, int b) { return a>b; }
typedef long long LL;
class mypcmp {
public:
    bool operator()(const int& a, const int& b) {
        return a<b;
    }
};



char ib[100004];
char dp[4][100004];
int gm, gn;
char xk[4][4];
char nx[4][2];
char dfs(int m, int i) {
    if (i==gn) {
        return xk[m][gm];
    }
    if (dp[m][i]!=-1) return dp[m][i];
    char r=0, nm;
    if (ib[i]=='0') {
        nm = nx[m][0];
        if (nm>=0) r=dfs(nm, i+1);
    } else if (ib[i]=='1') {
        nm = nx[m][1];
        if (nm>=0) r=dfs(nm, i+1);
    } else {
        nm = nx[m][0];
        if (nm>=0) r=dfs(nm, i+1);
        if (r==0) {
            nm = nx[m][1];
            if (nm>=0) r=dfs(nm, i+1);
        }
    }
    dp[m][i]=r;
    return r;
}
int main() {
    int n, i, j;
    for (i=0; i<4; i++) for (j=0; j<4; j++) xk[i][j]=1;
    for (i=0; i<4; i++) nx[i][0]=nx[i][1]=-1;
    xk[0][0]=0;
    xk[0][1]=0;
    xk[1][3]=0;
    xk[2][0]=0;
    xk[3][2]=0;
    xk[3][3]=0;

    nx[0][1]=1;
    nx[1][0]=2;
    nx[1][1]=3;
    nx[2][0]=0;
    nx[2][1]=1;
    nx[3][0]=2;
    
    
    int tc; scanf("%d", &tc); while(tc) { tc--;
        scanf("%d %s", &n, ib); gn=n;
        if (ib[0]=='?'&&ib[1]=='?') {
            for (i=0; i<n; i++) for (j=0; j<4; j++) dp[j][i]=-1;
            gm=0; if (dfs(0, 2)) { printf("Yes\n"); continue; }
            for (i=0; i<n; i++) for (j=0; j<4; j++) dp[j][i]=-1;
            gm=1; if (dfs(1, 2)) { printf("Yes\n"); continue; }
            for (i=0; i<n; i++) for (j=0; j<4; j++) dp[j][i]=-1;
            gm=2; if (dfs(2, 2)) { printf("Yes\n"); continue; }
            for (i=0; i<n; i++) for (j=0; j<4; j++) dp[j][i]=-1;
            gm=3; if (dfs(3, 2)) { printf("Yes\n"); continue; }
        } else if (ib[0]=='?') {
            if (ib[1]=='0') {
                for (i=0; i<n; i++) for (j=0; j<4; j++) dp[j][i]=-1;
                gm=0; if (dfs(0, 2)) { printf("Yes\n"); continue; }
                for (i=0; i<n; i++) for (j=0; j<4; j++) dp[j][i]=-1;
                gm=2; if (dfs(2, 2)) { printf("Yes\n"); continue; }
            } else {
                for (i=0; i<n; i++) for (j=0; j<4; j++) dp[j][i]=-1;
                gm=1; if (dfs(1, 2)) { printf("Yes\n"); continue; }
                for (i=0; i<n; i++) for (j=0; j<4; j++) dp[j][i]=-1;
                gm=3; if (dfs(3, 2)) { printf("Yes\n"); continue; }
            }
        } else if (ib[1]=='?') {
            if (ib[0]=='0') {
                for (i=0; i<n; i++) for (j=0; j<4; j++) dp[j][i]=-1;
                gm=0; if (dfs(0, 2)) { printf("Yes\n"); continue; }
                for (i=0; i<n; i++) for (j=0; j<4; j++) dp[j][i]=-1;
                gm=1; if (dfs(1, 2)) { printf("Yes\n"); continue; }
            } else {
                for (i=0; i<n; i++) for (j=0; j<4; j++) dp[j][i]=-1;
                gm=2; if (dfs(2, 2)) { printf("Yes\n"); continue; }
                for (i=0; i<n; i++) for (j=0; j<4; j++) dp[j][i]=-1;
                gm=3; if (dfs(3, 2)) { printf("Yes\n"); continue; }
            }
        } else {
            for (i=0; i<n; i++) for (j=0; j<4; j++) dp[j][i]=-1;
            gm=0;
            if (ib[0]=='1') gm|=2;
            if (ib[1]=='1') gm|=1;
            if (dfs(gm, 2)) { printf("Yes\n"); continue; }
        }
        printf("No\n");
    }
    return 0;
}
0