通行证
- 积分
- 606
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-10-5
- 最后登录
- 1970-1-1
|
- #include <iostream>
- #include <vector>
- #include <stack>
- #include <queue>
- #include <unordered_map>
- #include <unordered_set>
- #include <sstream>
- using namespace std;
- // Trie node
- class Node{
- public:
- int label; // 0 or 1
- int level; // depth of the node in this trie tree
- Node *p0,*p1; // child 0 or child 1
- Node(int l, int i):label(l),level(i),p0(NULL),p1(NULL){};
- };
- class Solution{
- private:
- // add number into the Trie tree
- void addNumberUtil(Node* root, vector<int>& vec, int pos){
- if(pos==-1){
- return;
- }
- if(vec[pos]==0){
- if(root->p0==NULL){
- root->p0 = new Node(0,pos-1);
- }
- addNumberUtil(root->p0,vec,pos-1);
- } else if (vec[pos]==1){
- if(root->p1==NULL){
- root->p1 = new Node(1,pos-1);
- }
- addNumberUtil(root->p1,vec,pos-1);
- }
- return;
- }
- // find the max Xor in the Trie tree starting with root
- int findMax(Node* root){
- if(root->p0==NULL && root->p1==NULL){
- return 0;
- } else if (root->p0==NULL){
- return findMaxUtil(root->p1,root->p1);
- } else if (root->p1==NULL){
- return findMaxUtil(root->p0,root->p0);
- }
-
- return pow(2,root->level) + findMaxUtil(root->p0,root->p1);
-
- }
- // recursively find the max Xor
- int findMaxUtil(Node* root1, Node* root2){
- if(root1->p0==NULL && root1->p1==NULL && root2->p0==NULL && root2->p1==NULL){//leaf node
- return 0;
- } else if(root1->p0==NULL && root2->p0==NULL){
- return findMaxUtil(root1->p1,root2->p1);
- } else if(root1->p1==NULL && root2->p1==NULL){
- return findMaxUtil(root1->p0,root2->p0);
- } else if(root1->p0==NULL && root2->p1==NULL){
- return pow(2,root1->level) + findMaxUtil(root1->p1,root2->p0);
- } else if(root1->p1==NULL && root2->p0==NULL){
- return pow(2,root1->level) + findMaxUtil(root1->p0,root2->p1);
- } else if (root1->p0==NULL){
- return pow(2,root1->level) + findMaxUtil(root1->p1,root2->p0);
- } else if (root1->p1==NULL){
- return pow(2,root1->level) + findMaxUtil(root1->p0,root2->p1);
- } else if (root2->p0==NULL){
- return pow(2,root1->level) + findMaxUtil(root1->p0,root2->p1);
- } else if (root2->p1==NULL){
- return pow(2,root1->level) + findMaxUtil(root1->p1,root2->p0);
- }
-
- // each node has both 0 and 1 child
- int t1 = findMaxUtil(root1->p0, root2->p1);
- int t2 = findMaxUtil(root1->p1, root2->p0);
- return pow(2,root1->level) + max(t1,t2);
- }
- public:
- int maxXor(vector<int>& vec){
- // vector to store the binary 0/1 of a number
- vector<vector<int>> binaryI(vec.size(),vector<int>{});
- int maxSize = 0;
- for(int i = 0; i < vec.size(); ++i){
- int temp = vec[i];
- while(temp>0){
- binaryI[i].push_back(temp%2);
- temp /= 2;
- }
- int size = binaryI[i].size();
- maxSize = max(maxSize,size);
- }
- // so that each binary representation has the same length
- for(auto& vec : binaryI){
- while(vec.size()<maxSize){
- vec.push_back(0);
- }
- }
- // build tree
- Node *root = new Node(-1,maxSize-1);
- for(auto vec : binaryI){
- addNumberUtil(root,vec,maxSize-1);
- }
- // find max Xor
- return findMax(root);
- }
- };
- int main(int argc, char** argv) {
- vector<int> a{1,2,3,5,7,8,10,14,9,12,6,20};
- // find answer using trie
- Solution su;
- auto p = su.maxXor(a);
- cout<<"max = "<<p<<" using 0-1 trie."<<endl;
- // find answer using brut-force method
- int maxValue = numeric_limits<int>::min();
- int ans1, ans2;
- for(int i = 0; i < a.size()-1; ++i){
- for(int j = i+1; j < a.size(); ++j){
- if(maxValue < (a[i] ^ a[j])){
- maxValue = a[i] ^ a[j];
- ans1 = a[i]; ans2 = a[j];
- }
- }
- }
- cout<<"max = "<<(ans1^ans2)<<" using brut-force."<<endl;
- }
复制代码 |
|