最大异或对【字典树 Trie】

最大异或对【字典树 Trie】

CH1602ACwing143

#include<bits/stdc++.h>
using namespace std;

#define int long long
const int N = 1e7;

int n,a,ans,num;

struct Node{
    
    
    int p;
    int son[2];
    int flag;
} tree[N + 5];

void build(){
    
    
    int now = 0;
    for(int i = 30;i >= 0;--i){
    
    
        int &x = tree[now].son[a >> i & 1];
        if(!x)
            x = ++num;
        now = x;
    }
}

int find(){
    
    
    int now = 0;
    int mx = 0;
    for(int i = 30;i >= 0;--i){
    
    
        int x = a >> i & 1;
        if(tree[now].son[!x])
            now = tree[now].son[!x],mx += 1 << i;
        else
            now = tree[now].son[x];
    }
    return mx;
}

signed main(){
    
    
    cin>>n;
    for(int i = 0;i < n;++i){
    
    
        cin>>a;
        if(i)
            ans = max(ans,find());
        build();
    }
    cout<<ans;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45985728/article/details/123610042