#include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; const ll INF=1LL<<60; typedef pair P; typedef pair PP; const ll MOD=998244353; const double PI=acos(-1); //bool dp[100000+5][1<<3][1<<3]; bool solve(int N,string str){ vector dp(N+1,vector>(1<<3,vector(1<<3,false))); //初期化 vector prefix={0}; for(int i=0;i<3;i++){ vector nx; for(int c:prefix){ if(str[i]=='?'){ nx.push_back(2*c+1); nx.push_back(2*c+0); }else if(str[i]=='1'){ nx.push_back(2*c+1); }else if(str[i]=='0'){ nx.push_back(2*c+0); } } prefix=nx; } set prefixst(prefix.begin(),prefix.end()); for(int c:prefixst){ //printf("c=%d\n",c); for(int S=0;S<(1<<3);S++){ dp[2][c][S]=true; } } for(int S=0;S<(1<<3);S++){ dp[2][S][0]=false; dp[2][S][(1<<3)-1]=false; dp[2][0][S]=false; dp[2][(1<<3)-1][S]=false; } for(int S1=0;S1<(1<<3);S1++){ for(int S2=0;S2<(1<<3);S2++){ if( ((S1>>2)&1)==0b1 && (S2&0b11)==0b11 ){ //S1の上一桁が1, S2の下2桁が0b11 dp[2][S1][S2]=false; } if( ((S1>>1)&0b11)==0b11 && (S2&1)==0b1 ){ //S1の上2桁が0b11, S2の下1桁が1 dp[2][S1][S2]=false; } if( ((S1>>2)&1)==0b0 && (S2&0b11)==0b00 ){ //S1の上一桁が0, S2の下2桁が0b00 dp[2][S1][S2]=false; } if( ((S1>>1)&0b11)==0b00 && (S2&1)==0b0 ){ //S1の上2桁が0b00, S2の下1桁が0 dp[2][S1][S2]=false; } /* if( ((S1>>2)&1)==1 && ((S2>>1)&3)==3 ){ //S1の下一桁が1, S2の下2桁が11 dp[2][S1][S2]=false; } if( ((S2>>2)&1)==1 && ((S1>>1)&3)==3 ){ //S2の下1桁が1, S1の下2桁が11 dp[2][S1][S2]=false; } if( ((S1&1)==0) && ((S2>>1)&3)==0 ){ dp[2][S1][S2]=false; } if( ((S2>>1)&1)==0 && ((S1>>2)&1)==0 ){ dp[2][S1][S2]=false; } */ } } for(int i=2;i>1)<<1); if(str[i]=='1'){ int to=tmp|0b001; if(to!=0b111 && to!=0){ dp[i+1][to][S2]=true; } } else if(str[i]=='0'){ int to=tmp|0b000; if(to!=0b111 && to!=0){ dp[i+1][to][S2]=true; } }else{ int to1=tmp|0b001; int to0=tmp|0b000; if(to1!=0b111 && to1!=0){ dp[i+1][to1][S2]=true; } if(to0!=0b111 && to0!=0){ dp[i+1][to0][S2]=true; } } } } } vector suffix={0}; for(int i=N-3;i nx; for(int c:suffix){ if(str[i]=='?'){ nx.push_back(2*c+1); nx.push_back(2*c+0); }else if(str[i]=='1'){ nx.push_back(2*c+1); }else if(str[i]=='0'){ nx.push_back(2*c+0); } } suffix=nx; } set suffixst(suffix.begin(),suffix.end()); bool res=false; for(int S=0;S<(1<<3);S++){ if(dp[N][S][S] && suffixst.count(S)){ if(S==0b000 || S==0b111 ) continue; //cout<<"S="<>2)&1)==1 && ((S>>1)&3)==3 ){ //Sの下一桁が1, Sの下2桁が11 continue; } if( ((S>>1)&1)==3 && ((S>>2)&1)==1 ){ continue; } if( ((S>>2)&1)==0 && ((S>>1)&3)==0 ){ //Sの下一桁が1, Sの下2桁が11 continue; } if( ((S>>1)&1)==0 && ((S>>2)&1)==0 ){ continue; } */ res=true; } } auto printb=[](int n){ string res=""; if(n==0){ //res+="0"; res+='0'; return res; } while(n>0){ if(n&1){ //res+="1"; res+='1'; }else{ //res+="0"; res+='0'; } n/=2; } reverse(res.begin(),res.end()); return res; }; /* for(int i=2;i<=N;i++){ for(int S2=0;S2<(1<<3);S2++){ for(int S1=0;S1<(1<<3);S1++){ cout<<"dp["<>T; for(int t=0;t>N; string S; cin>>S; bool flag=solve(N,S); cout<<(flag?"Yes":"No")<>(2,vector(2,false))); for(int t1=0;t1<2;t1++){ for(int t2=0;t2<2;t2++){ dp[0][t1][t2]=true; } } for(int i=0;i