B. Eliminaciones binarias (pensamiento + simulación)

https://codeforces.com/contest/1499/problem/B


Ideas:

La respuesta final es 00000, 11111 y 00000111 tres estados, y luego estos estados se verifican de acuerdo con la estructura.

El tercer punto intermedio de enumeración estatal i, [1, i] = 0, [i + 1, n] = 1;

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=200;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
char s[maxn];
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL t;cin>>t;
  while(t--){
    cin>>(s+1);
    LL len=strlen(s+1);
    bool flag1=1;bool flag2=1;bool flag3=0;
    ///全0
    LL pos=-1;
    for(LL i=1;i<=len;i++){
        if(s[i]=='1'&&i-pos>=2){
            pos=i;
        }
        else if(s[i]=='1'&&i-pos<2){
            flag1=0;
            break;
        }
    }
    ///全1
    pos=-1;
    for(LL i=1;i<=len;i++){
        if(s[i]=='0'&&i-pos>=2){
            pos=i;
        }
        else if(s[i]=='0'&&i-pos<2){
            flag2=0;
            break;
        }
    }
    ///前0后1
    for(LL i=1;i<=len;i++){///[1,i]为0,[i+1,len]为1
        LL p=-1;
        bool ok=1;
        for(LL j=1;j<=i;j++){
            if(s[j]=='1'&&j-p>=2){
                p=j;
            }
            else if(s[j]=='1'&&j-p<2){
                ok=0;
                break;
            }
        }
        if(ok==0) continue;
        for(LL j=i+1;j<=len;j++){
            if(s[j]=='0'&&j-p>=2){
                p=j;
            }
            else if(s[j]=='0'&&j-p<2){
                ok=0;
                break;
            }
        }
        if(ok==1){
            flag3=1;
            break;
        }
    }

    if(flag1||flag2||flag3){
        cout<<"YES"<<"\n";
    }
    else cout<<"NO"<<"\n";
  }
return 0;
}

 

Supongo que te gusta

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/115017320
Recomendado
Clasificación