AcWing 143. 最大异或对

2021-06-05 23:04

阅读:580

标签:http   search   amp   break   run   out   node   include   namespace   

https://www.acwing.com/problem/content/145

#include 
#include 
using namespace std;

const int N = 100010;
int n;
int a[N];

class TrieNode {
public:
    TrieNode() {};
    TrieNode* children[2];
};

class Trie {
public:
    Trie() {
        root = new TrieNode;
    }
    
    void insert(int x) {
        TrieNode* cur = root;
        for (int i = 30; i >= 0; i--) {
            int c = x >> i & 1;
            if (!cur->children[c]) {
                cur->children[c] = new TrieNode;
            }
            cur = cur->children[c];
        }
    }

    int search(int x) {
        TrieNode* cur = root;
        
        int res = 0;
        for (int i = 30; i >= 0; i--) {
            int c = x >> i & 1;
            if (cur->children[!c]) {
                res += 1  i;
                cur = cur->children[!c];
            } else if (cur->children[c]) {
                cur = cur->children[c];
            } else {
                break;
            }
        }
        return res;
    }
private:
    TrieNode* root;
};

int run() {
    Trie trie;
    int res = 0;
    for (int i = 0; i ) {
        trie.insert(a[i]);
        res = max(res, trie.search(a[i]));
    }

    return res;
}

int main()
{
    cin >> n;
    for (int i = 0; i ) {
        cin >> a[i];
    }

    cout  endl;
    return 0;
}

 

AcWing 143. 最大异或对

标签:http   search   amp   break   run   out   node   include   namespace   

原文地址:https://www.cnblogs.com/polystyrene/p/10796811.html


评论


亲,登录后才可以留言!