-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBinary trie using static array.cpp
115 lines (103 loc) · 2.51 KB
/
Binary trie using static array.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// main.cpp
// practice
//
// Created by Mahmud on 03/10/18.
// Copyright © 2018 Mahmud. All rights reserved.
//
// reference problem:
// https://www.spoj.com/problems/XORX/
#include <iostream>
#include <cstdio>
using namespace std;
const int S = 1 << 18;
const int MAX_BIT = 30;
struct node{
int value;
int leftChildID;
int rightChildID;
node() {
}
node(int v, int l, int r):
value(v), leftChildID(l), rightChildID(r) {
}
};
node pool[S];
int offset = 0;
int allocate(int value) {
pool[++offset].value = value;
pool[offset].leftChildID = -1;
pool[offset].rightChildID = -1;
return offset;
}
void add(int value) {
int id = 0;
for (int i = MAX_BIT; i >= 0; i --) {
int bit = (value >> i) & 1;
if (!bit) {
if (pool[id].leftChildID == -1) {
pool[id].leftChildID = allocate(0);
}
id = pool[id].leftChildID;
} else {
if (pool[id].rightChildID == -1) {
pool[id].rightChildID = allocate(0);
}
id = pool[id].rightChildID;
}
pool[id].value = 1;
}
}
int getMatch(int x) {
int id = 0;
int result = 0;
for (int i = MAX_BIT; i >= 0; i --) {
if (pool[id].value == 0) {
break;
}
int bit = (x >> i) & 1;
if (!bit) {
if (pool[id].rightChildID != -1) {
result |= 1 << i;
id = pool[id].rightChildID;
} else {
id = pool[id].leftChildID;
}
} else {
if (pool[id].leftChildID != -1) {
id = pool[id].leftChildID;
} else {
result |= 1 << i;
id = pool[id].rightChildID;
}
}
}
return result;
}
int N, T, X;
int data[S];
int main() {
scanf("%d", &T);
while (T --) {
scanf("%d%d", &N, &X);
for (int i = 1; i <= N; i ++) {
scanf("%d", &data[i]);
} for (int i = 0; i < S; i ++) {
pool[i] = node(0, -1, -1);
}
pool[0].value = 1;
offset = 0;
add(0);
int prefix = 0, bestResult = data[1];
for (int i = 1; i <= N; i ++) {
prefix ^= data[i];
add(prefix);
int current = prefix ^ getMatch(X ^ prefix);
if ((X ^ current) > (X ^ bestResult)) {
bestResult = current;
}
}
printf("%d\n", bestResult);
}
return 0;
}