AcWing 143 of the largest XOR

In a given \ (N \) integers \ (A1, A2 ...... AN \ ) in the election of two were xor (exclusive or) operation, the result is the largest number?

Input format
of the first line of the input integer N.

The second input line N integers \ (A1 ~ the AN \) .

Output format
output represents an integer answer.

data range

\(1≤N≤105,\)

\(0≤Ai<231\)

SAMPLE INPUT

3

1 2 3

Sample Output

3

Be a Trietree template of questions, summarize the Trieknowledge tree.

TrieTree for achieving fast retrieval character string, is a multiple-tree structure, each node has a plurality of child nodes. General actions are:

initialization

An empty Triecontains only a root node

int trie[SIZE][26], tot = 1;  //trie数组的第二维通常是已知的,这里只考虑小写字母,所以每个节点最多有26个子结点,tot是节点指针,对应于第一维

insert

void insert(char* str){
    int len = strlen(str), p = 1;
    for(int k = 0; k < len; k++){  //遍历str的每一个字符
        int ch = str[k]-'a';
        if(trie[p][ch] == 0) trie[p][ch] = ++tot; //如果不存在子结点,那么指向一个新的结点
        p = trie[p][ch]; //继续移动p指针
    }
    end[p] = true;   //结束标记
}

Retrieval

bool search(char* str){
    int len = strlen(str), p = 1;
    for(int k = 0; k < len; ++k){
        p = trie[p][str[k]-'a'];    
        if(p == 0) return false;  //如果这个节点不存在,那么说明trie树中没有存储这个字符串,结束检索并返回false;
    }
    return end[p];
}

Back to the question here is different statistical or right, then the child nodes of each node only 0 and 1 are two cases in which each node has at most two sons. We, all of it is stored in the trie tree binary string, and then retrieve that number trie tree for each digital input of a number of A, if it's a one-digit binary number is 0, then we retrieving see if trie tree child node corresponding to node 1, i.e., to search the current number of bitbits of the opposite node, if there is a record on, it does not continue searching along the same node, and record 0 .

Throughout the trietotal number of tree nodes (spatial complexity) should be the maximum number of string length son X

#include <iostream>

using namespace std;

const int maxn = 3000000;  //这里我之前取的是2<<31,然后数组大小就只有5...
const int maxm = 1e5;
int son[maxn][2];
int a[maxm];
int idx = 0;

void insert(int x){
    int p = 0, mask = 1 << 30;
    int val;
    for(int i = 0; i < 31; ++i){
        if(x & mask) val = 1; else val = 0; 
        if(!son[p][val]) { son[p][val] = ++idx;}
        p = son[p][val];
        mask = mask >> 1;
    }
}

int tofind(int x){
    int p = 0, res = 0, mask = 1 << 30, val;
    for(int i = 0; i < 31; ++i){
        if(x & mask) val = 1; else val = 0;
        if(son[p][1-val]){
            res += (1<<(30-i));
            p = son[p][1-val];
        }else 
            p = son[p][val];
        mask = mask >> 1;
    }
    return res;
}

int main(){
    int n; scanf("%d", &n);
    int ans = 0;
    for(int i = 0; i < n; ++i){
        scanf("%d", &a[i]);
        insert(a[i]);
        ans = max(tofind(a[i]), ans);
    }
    printf("%d", ans);
}

Guess you like

Origin www.cnblogs.com/patrolli/p/11795783.html
XOR