2013级新生程序设计基础竞赛-正式赛 F 异或最大值 解题报告

F - 异或最大值

Time Limit: 2000/1000MS (Java/Others)      Memory Limit: 128000/64000KB (Java/Others)

Problem Description

小布除了经常能在野外遇到不明生物外还非常喜欢由非负数组成的数组, 有一天他的获得了一个由n个非负数组成的数组

而小布想在数组中找到一段连续的数字,使得这段数字异或起来的值是最大的

例如这个数组是  1, 2, 5, 7, 8

那么 (2,5,7) 是一段连续的数字, 这段数字异或起来的值为 2 ^ 5 ^ 7 = 0

(1, 2) 也是一段连续的数字,这段数字异或起来的值为 1 ^ 2 = 3

其中异或起来最大的连续数字段为 (7,8) 值为 7 ^ 8 = 15

Input

输入数据第一行是一个整数 T, 接下来有 T个例子

每个例子第一行是一个整数 n, 接下来是n个小于等于1000的非负整数 ( 1 <= n <= 100)

Output

对于每个例子输出 异或起来的值最大的 那个连续的数字段 异或起来的值

Sample Input

3
1
1
2
2 3
5
1 2 5 7 8

Sample Output

1
3
15

为了找回做题的感觉- -......堪称暴力的经典~~~
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 using namespace std;
 5 
 6 const int maxn = 100 + 10;
 7 int a[maxn];
 8 
 9 int main()
10 {
11     int T, n;
12     while (scanf("%d", &T) != EOF)
13     {
14         while (T--)
15         {
16             scanf("%d", &n);
17             for (int i = 0; i < n; i++)
18                 scanf("%d", &a[i]);
19             int ans = a[0];
20             for (int i = 0; i < n; i++)
21             {
22                 int tmp = a[i];
23                 for (int j = i+1; j < n; j++)
24                 {
25                     tmp ^= a[j];
26                     ans = max(tmp, ans);  // 枚举所有长度(>= 2)的异或结果
27                 }
28                 ans = max(tmp, ans);  // 这个是关键啊~~,有可能单独的一个数就是最大的异或结果
29             }
30             printf("%d
", ans);
31         }
32     }
33     return 0;
34 }

原文地址:https://www.cnblogs.com/windysai/p/3823441.html