题目链接:http://codeforces.com/problemset/problem/282/E
The Bitlandians are quite weird people. They have their own problems and their own solutions. They have their own thoughts and their own beliefs, they have their own values and their own merits. They have their own dishes and their own sausages!
In Bitland a sausage is an array of integers! A sausage's deliciousness is equal to the bitwise excluding OR (the xor operation) of all integers in that sausage.
One day, when Mr. Bitkoch (the local cook) was going to close his BitRestaurant, BitHaval and BitAryo, the most famous citizens of Bitland, entered the restaurant and each ordered a sausage.
But Mr. Bitkoch had only one sausage left. So he decided to cut a prefix (several, may be zero, first array elements) of the sausage and give it to BitHaval and a postfix (several, may be zero, last array elements) of the sausage and give it to BitAryo. Note that one or both pieces of the sausage can be empty. Of course, the cut pieces mustn't intersect (no array element can occur in both pieces).
The pleasure of BitHaval and BitAryo is equal to the bitwise XOR of their sausages' deliciousness. An empty sausage's deliciousness equals zero.
Find a way to cut a piece of sausage for BitHaval and BitAryo that maximizes the pleasure of these worthy citizens.
The first line contains an integer n (1 ≤ n ≤ 105).
The next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 1012) — Mr. Bitkoch's sausage.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.
Print a single integer — the maximum pleasure BitHaval and BitAryo can get from the dinner.
2 1 2
3
3 1 2 3
3
2 1000 1000
1000
题解:
1.预处理前缀异或、后缀异或。
2.枚举每一个前缀异或(i:0~n):
2.1.将此前缀异或加入到Trie树中,
2.2.根据后一位的后缀异或,在Trie树中查找与之异或后的最大值,并一直更新ans。
学习之处:
当需要在一个序列的中间删除若干个连续元素,使得满足xx条件时:
1.预处理出前缀和、后缀和。
2.枚举每一个前缀和:将此前缀和插入某种数据结构中,再用后一位的后缀和在此数据结构中查找。
类似的题目:http://blog.csdn.net/dolfamingo/article/details/71001021
代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const double eps = 1e-6; 5 const int INF = 2e9; 6 const LL LNF = 9e18; 7 const int mod = 1e9+7; 8 const int maxn = 1e5+10; 9 10 typedef struct node 11 { 12 struct node *next[2]; 13 }Node, *Trie; 14 15 Trie T; 16 LL n, a[maxn], pre[maxn], rev[maxn]; 17 18 void init() 19 { 20 scanf("%I64d",&n); 21 for(int i = 1; i<=n; i++) 22 scanf("%I64d",&a[i]); 23 for(int i = 1; i<=n; i++) //前缀 24 pre[i] = pre[i-1]^a[i]; 25 for(int i = n; i>=1; i--) //后缀 26 rev[i] = rev[i+1]^a[i]; 27 28 T = new Node; //初始化Trie树 29 T->next[0] = T->next[1] = NULL; 30 } 31 32 void add(LL x) 33 { 34 Trie p = T; 35 for(int i = 50; i>=0; i--) //从高位到低位 36 { 37 int d = (x>>i)&1; 38 if(p->next[d]==NULL) //此位的d不存在, 则新建 39 p->next[d] = new Node, p->next[d]->next[0] = p->next[d]->next[1] = NULL; 40 p = p->next[d]; 41 } 42 } 43 44 LL query(LL x) 45 { 46 LL tmp = 0; 47 Trie p = T; 48 for(int i = 50; i>=0; i--) 49 { 50 //如果!d路存在,则可加上(d ^ !d = 1), 并顺着这条路走下去; 否则走d路(!d路和d路至少一路存在) 51 int d = (x>>i)&1; 52 if(p->next[!d]) tmp += 1LL*(1LL<<i), p = p->next[!d]; 53 else p = p->next[d]; 54 } 55 return tmp; 56 } 57 58 void solve() 59 { 60 LL ans = 0; 61 for(int i = 0; i<=n; i++) //i为0时, 没有前缀;i为n时,没有后缀。 62 { 63 add(pre[i]); 64 ans = max( ans, query(rev[i+1]) ); 65 } 66 printf("%I64d\n",ans); 67 } 68 69 int main() 70 { 71 init(); 72 solve(); 73 }