-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_xor.go
79 lines (67 loc) · 1.02 KB
/
max_xor.go
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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
const N = 100010
const M = 3000030
var son [M][2]int
var idx int
var a [N]int
func insert(x int) {
p := 0
for i := 30; i >= 0; i-- {
u := (x >> i) & 1
if son[p][u] == 0 {
idx++
son[p][u] = idx
}
p = son[p][u]
}
}
func query(x int) int {
p := 0
val := 0
for i := 30; i >= 0; i-- {
u := (x >> i) & 1
if son[p][u^1] > 0 {
val = val + 1<<i
p = son[p][u^1]
} else {
p = son[p][u]
}
}
return val
}
func main() {
n := 0
fmt.Scanf("%d", &n)
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanWords)
a := make([]int, 0)
for scanner.Scan() {
if scanner.Text() != "" {
val, _ := strconv.Atoi(scanner.Text())
a = append(a, val)
}
}
if scanner.Err() != nil {
fmt.Printf("error: %s\n", scanner.Err())
}
for i := 0; i < len(a); i++ {
insert(a[i])
}
res := 0
for i := 0; i < len(a); i++ {
res = max(res, query(a[i]))
}
fmt.Println(res)
}
func max(res, a int) int {
if res <= a {
return a
}
return res
}