$$\color{Red}{最大异或对JAVA}$$
这里附带打个广告——————我做的所有的题解
包括基础提高以及一些零散刷的各种各样的题
思考
直接看代码
java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class Main {
static int n, idx, N = 3100010;
static int[][] son = new int[N][2];
static void add(int x) {
int p = 0;
for (int i = 30; i >= 0; i--) {
int u = x >> i & 1;
if (son[p][u] == 0)
son[p][u] = ++idx;
p = son[p][u];
}
}
static int query(int x) {
int p = 0, res = 0;
for (int i = 30; i >= 0; i--) {
int u = x >> i & 1;
if (son[p][1 - u] != 0) {
p = son[p][1 - u];
res = (res << 1) + (1 - u);
} else {
p = son[p][u];
res = (res << 1) + u;
}
}
return res;
}
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
n = Integer.parseInt(br.readLine());
String[] s = br.readLine().split(" ");
int res = 0;
for (int i = 0; i < n; i++) {
int x = Integer.parseInt(s[i]);
add(x);
res = Math.max(res, x ^ query(x));
}
System.out.println(res);
}
}