-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxero.cpp
57 lines (54 loc) · 1.08 KB
/
maxero.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<bitset>
#include<algorithm>
#include<iostream>
#include<memory>
struct Node:std::enable_shared_from_this<Node>
{
public:
Node() = default;
void addero(int num)
{
std::shared_ptr<Node> tryNode = shared_from_this();
for(int i=31;i>=0;i--)
{
int cur = num>>i&1;
tryNode->next[cur] = tryNode->next[cur]?tryNode->next[cur]:std::make_shared<Node>();
tryNode = tryNode->next[cur];
}
}
int getmax(int ero)
{
std::shared_ptr<Node> tryNode = shared_from_this();
int res=0;
for(int i=31;i>=0;i--)
{
int cur = ero>>i&1;
int best = i==31?cur:cur^1;
best = tryNode->next[best]?best:best^1;
res |= (cur^best)<<i;
tryNode = tryNode->next[best];
}
return res;
}
int getAllmax(std::vector<int>& arr)
{
int res = arr[0];
addero(0);
int ero = 0;
for(int i=0;i<arr.size();i++)
{
ero^=arr[i];
res = std::max(res,getmax(ero));
addero(ero);
}
return res;
}
private:
std::shared_ptr<Node> next[2];
};
int main()
{
std::shared_ptr<Node> node = std::make_shared<Node>();
std::vector<int> arr = {-1};
std::cout<<node->getAllmax(arr);
}