Maximum Xor with Prefix and Suffix
Description
We have N numbers as an array, you need to find a prefix array and a suffix arrar,which we can get the maximum xor value with all elements in them. Notice that for prefix[0,1] and suffix [r,n-1],do not intersect (l 小于 r), and they can be empty.
Limits
-Memory limit per test : 256 megabytes
-Time limit per test : The faster the better
Compile & Environment
C++
g++ Main.cc –o Main –fno-asm –Wall –lm –static –std=c++0x –DONLINE_JUDGE
Java
J2SE8
Maximum stack size is 50m
Input
The first line is one Number N( 1<=N<=100000)
The second line contains N numbers ai (0 <= ai <=1e12) separated by space,
Which represents the array.
Output
Just output the maximum xor result.
Sample Test
Input
3
1 2 3
Out
3
Code
#include<iostream>
#include <algorithm>
long long pre[100005];
long long suf[100005];
long long arr[100005];
long long res[100005];
using namespace std;
long long cal(long long zu[100005], int m, int n){
long long res = 0;
for (int i = m; i <= n; i++){
res = max(res, zu[i]);
}
return res;
}
int main(){
int N;
cin >> N;
int i = 1;
for (int i = 1; i <= N; i++){
cin >> arr[i];
}
pre[0] = pre[N + 1] = suf[0] = suf[N + 1] = 0;
for (int i = 1; i<N; i++){
pre[i] = pre[i - 1] ^ arr[i];
}
for (int i = N; i >= 1; i--){
suf[i] = suf[i + 1] ^ arr[i];
}
long long ans = 0;
for (int i = 1; i <= N; i++){
ans = max(ans, max(cal(pre, 0, i), cal(suf, i + 1, N)));
}
cout << ans << endl;
return 0;
}
ca3d251f-0e7b-4258-8e68-8f7c9caa676+7