1. 程式人生 > >異或最大值(01字典樹)

異或最大值(01字典樹)

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 5;        //集合中的數字個數
typedef long long LL;
int ch[32 * maxn][2];               //節點的邊資訊
LL value[32 * maxn];                //節點儲存的值
int node_cnt;                       //樹中當前節點個數

inline void init(){                 //樹清空
    node_cnt = 1;
    memset(ch[0],0,sizeof(ch));
}

inline void Insert(LL x){           //在字典樹中插入 X
                                    //和一般字典樹的操作相同 將X的二進位制插入到字典樹中
    int cur = 0;
    for(int i = 32;i >= 0;--i){
        int idx = (x >> i) & 1;
        if(!ch[cur][idx]){
            memset(ch[node_cnt],0,sizeof(ch[node_cnt]));
            ch[cur][idx] = node_cnt;
            value[node_cnt++] = 0;
        }
            cur = ch[cur][idx];
        }
        value[cur] = x;             //最後的節點插入value
}

inline LL Query(LL x){              //在字典樹中查詢和X異或的最大值的元素Y 返回Y的值
    int cur = 0;
    for(int i = 32;i >= 0;--i){
        int idx = (x >> i) & 1;
        if(ch[cur][idx ^ 1]) cur = ch[cur][idx ^ 1];
        else cur = ch[cur][idx];
    }
    return value[cur];
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        init();
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&value[i]);
            Insert(value[i]);
        }
        LL ans=0;
        for(int i=0;i<n;i++)
        {
           ans=max(ans,value[i]^Query(value[i]));
        }
        printf("%lld\n",ans);
    }
}