Hello 2018 E

The meaning of problems

Tell you x Y from x、y、z , and ! & ! 、|、\& Elements with operators and parentheses, composed of eight binary string you want.

answer

I first met with s t r i n g string as a transfer array, up knowledge.
Consider priority.
For an operator, the number should be greater than two priorities.
So we set up three arrays, representing the lowest priority that is | , Medium & \& , the best ! ! And parentheses.
x Y from x、y、z may be.
Is the meaning of the expression array, the last time calculation is based on what the end.
Because only high-priority operations in low-priority character operation when you need to add brackets, so ! ! Can be seen as a parenthesis.

When the transfer, because of the uncertainty the order, so we transferred several times, until the transfer of knowledge can not be effective.
Write a simple online version, that is, the transfer process continues to take minimum.
I wrote a shift relatively violence, is simply three cases, violent shift.
Apparently the lowest priority can be freely assigned high priority, it is 9 9 Zhong transfer.
Medium is 4 4 species.
The higher turn their own 1 1 species.
The brackets are lower 2 2 Zhong

Since the minimum required length after lexicographic guaranteed minimum, it is not directly compare string.

#include<bits/stdc++.h>
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define sf(x) scanf("%d",&x)
typedef long long ll;
using namespace std;

const ll mod = 998244353;
const int maxn = 2e5+500;

int n;
string E[256],T[256],F[256];//|| & !

bool flag=true;

void update(string &A,string B){
    if(A=="")A=B,flag=1;
    else{
        if(A.size()>B.size())A=B,flag=1;
        else if(A.size()==B.size()&&A>B)A=B,flag=1;
    }
}

void slove(){
    int x=0,y=0,z=0;
    for(int i=0;i<8;i++)if(i/4)x|=1<<i;F[x]="x";
    for(int i=0;i<8;i++)if((i/2)%2)y|=1<<i;F[y]="y";
    for(int i=0;i<8;i++)if(i%2)z|=1<<i;F[z]="z";
    while(flag){
        flag=0;
        for(int i=0;i<256;i++){
            for(int j=0;j<256;j++){
                if(E[i].size()&&E[j].size())update(E[i|j],E[i]+'|'+E[j]);
                if(E[i].size()&&T[j].size())update(E[i|j],E[i]+'|'+T[j]);
                if(T[i].size()&&E[j].size())update(E[i|j],T[i]+'|'+E[j]);
                if(E[i].size()&&F[j].size())update(E[i|j],E[i]+'|'+F[j]);
                if(F[i].size()&&E[j].size())update(E[i|j],F[i]+'|'+E[j]);
                if(T[i].size()&&T[j].size())update(E[i|j],T[i]+'|'+T[j]);
                if(F[i].size()&&T[j].size())update(E[i|j],F[i]+'|'+T[j]);
                if(T[i].size()&&F[j].size())update(E[i|j],T[i]+'|'+F[j]);
                if(F[i].size()&&F[j].size())update(E[i|j],F[i]+'|'+F[j]);
            }
        }
       // cout<<E[248]<<"->"<<F[248]<<"->"<<T[248]<<endl;
        for(int i=0;i<256;i++){
            for(int j=0;j<256;j++){
                if(T[i].size()&&T[j].size())update(T[i&j],T[i]+'&'+T[j]);
                if(F[i].size()&&T[j].size())update(T[i&j],F[i]+'&'+T[j]);
                if(T[i].size()&&F[j].size())update(T[i&j],T[i]+'&'+F[j]);
                if(F[i].size()&&F[j].size())update(T[i&j],F[i]+'&'+F[j]);
            }
        }
        //cout<<E[248]<<"->"<<F[248]<<"->"<<T[248]<<endl;
        for(int i=0;i<256;i++)if(E[i]!="")update(F[i],'('+E[i]+')');//变为最高优先级
        for(int i=0;i<256;i++)if(T[i]!="")update(F[i],'('+T[i]+')');//变为最高优先级
        for(int i=0;i<256;i++)if(F[i]!="")update(F[i^255],'!'+F[i]);//运算的字符串的优先级必须大于等于运算符
        //for(int i=0;i<256;i++)cout<<i<<" "<<E[i]<<" "<<T[i]<<" "<<F[i]<<endl;
        //puts("?");
        if(!flag)break;
    }
}

int main(){
    slove();
    cin>>n;
    FOR(i,1,n){
        string st;cin>>st;
        int now=0;
        for(int j=0;j<8;j++){
            if(st[j]=='1')now|=1<<j;
        }
        string str;
       //cout<<now<<endl;
        update(str,E[now]);
        update(str,F[now]);
        update(str,T[now]);
        cout<<str<<endl;
    }
}

Published 203 original articles · won praise 17 · views 20000 +

Guess you like

Origin blog.csdn.net/mxYlulu/article/details/104272216