The XOR Largest Path

The XOR Largest Path

The XOR Longest Path
题意:给定n个数Ai,问两个数A进行XOR后的最大值

倒叙建trie,每次到p的时候对相对位置进行判断,如果有就从相对位置出发

int n;
int trie[maxn*32+5][2],tot=1;
void inser(LL a){
    
    //trie插入
    int p=1;
    LL ch=1ll;
    for(LL i=32;i>=0;i--){
    
    
        ch=a>>i&1ll;
        if(trie[p][ch]==0)trie[p][ch]=++tot;
        p=trie[p][ch];
    }
}
LL sear(LL a){
    
    
    int p=1;
    LL ch=1ll,ans=0ll;
    for(LL i=32;i>=0;i--){
    
    
        ch=a>>i&1ll;
        if(trie[p][ch^1]){
    
    //相对位置
            p=trie[p][ch^1];
            ans|=1<<i;
        }else{
    
    
            p=trie[p][ch];
        }
    }
    return ans;
}
LL A[maxn];
int main(){
    
    
    int n;
    sci(n);
    LL res=0;
    for(int i=1;i<=n;i++){
    
    
        scl(A[i]);
        inser(A[i]);
        res=max(res,sear(A[i]));
    }
    cout<<res<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44986601/article/details/105629253