[模板]01trie,維護異或最大值

// 查詢異或最大值,每次插入和查詢時間都是log(C)
template<class T>
class trie01 {
    vector<vector<T>> tree;
public:
    trie01() : tree(1, vector<T>(2, 0)) {}

    // 插入待檢查的數字
    void insert (T x)
    {
        int p = 0;
        for(int i = sizeof(x)*8-2; i >= 0; --i)
        {
            bool o = (x&((T)1<<i));
            if(tree[p][o] == 0)
            {
                tree[p][o] = tree.size();
                tree.emplace_back(2,0);
            }
            p = tree[p][o];
        }
    }

    // 查詢在插入的數字中,返回與x異或後的最大值
    T match (T x)
    {
        T t = x, p = 0;
        // 從高位到低位
        for(int i = sizeof(x)*8-2; i >= 0; --i)
        {
            bool o = (x&((T)1<<i));
            if(tree[p][!o])
                p = tree[p][!o], t = t|((T)1<<i);
            else if(tree[p][o])
                p = tree[p][o], t = t&(~((T)1<<i));
        }
        return t;
    }
};

已通過 [數組中兩個數的最大異或值](421. 數組中兩個數的最大異或值 - 力扣(Leetcode))

template<class T>
class trie01 {
    vector<vector<T>> tree;
public:
    trie01() : tree(1, vector<T>(2, 0)) {}

    void insert (T x)
    {
        int p = 0;
        for(int i = sizeof(x)*8-2; i >= 0; --i)
        {
            bool o = (x&((T)1<<i));
            if(tree[p][o] == 0)
            {
                tree[p][o] = tree.size();
                tree.emplace_back(2,0);
            }
            p = tree[p][o];
        }
    }

    T match (T x)
    {
        T t = x, p = 0;
        // 從高位到低位
        for(int i = sizeof(x)*8-2; i >= 0; --i)
        {
            bool o = (x&((T)1<<i));
            if(tree[p][!o])
                p = tree[p][!o], t = t|((T)1<<i);
            else if(tree[p][o])
                p = tree[p][o], t = t&(~((T)1<<i));
        }
        return t;
    }
};
class Solution {
public:
    int findMaximumXOR(vector<int>& nums) {
        trie01<int> trie;
        trie.insert(nums[0]);
        int res = 0;
        for(auto &a : nums)
            res = max(res, trie.match(a)), trie.insert(a);
        return res;
    }
};

已通過 [E. Sausage Maximization](Problem - 282E - Codeforces)

#include <iostream>
#include <vector>
using namespace std;

using ll = long long;
template<class T>
class trie01 {
    vector<vector<T>> tree;
public:
    trie01() : tree(1, vector<T>(2, 0)) {}

    void insert (T x)
    {
        int p = 0;
        for(int i = sizeof(x)*8-2; i >= 0; --i)
        {
            bool o = (x&((T)1<<i));
            if(tree[p][o] == 0)
            {
                tree[p][o] = tree.size();
                tree.emplace_back(2,0);
            }
            p = tree[p][o];
        }
    }

    T match (T x)
    {
        T t = x, p = 0;
        // 從高位到低位
        for(int i = sizeof(x)*8-2; i >= 0; --i)
        {
            bool o = (x&((T)1<<i));
            if(tree[p][!o])
                p = tree[p][!o], t = t|((T)1<<i);
            else if(tree[p][o])
                p = tree[p][o], t = t&(~((T)1<<i));
        }
        return t;
    }
};

int main()
{
    
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    trie01<ll> trie;

    int n; cin>>n;
    vector<ll> arr(n);
    ll sum = 0, res = 0;
    for(auto &a : arr) {
        cin >> a;
        sum ^= a;
        res = max(res, a);
        trie.insert(sum);
    }
 
    sum = 0;
    for(int i = n-1; i >= 0; --i)
    {
        sum ^= arr[i];
        res = max(res, trie.match(sum));
    }
    cout << res << endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章