Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! B. Johnny and His Hobbies

B. Johnny and His Hobbies

入口:B. Johnny and His Hobbies

B. Johnny and His Hobbies
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Among Johnny’s numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad’s office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble.

There is a set S containing very important numbers on his dad’s desk. The minute Johnny heard about it, he decided that it’s a good idea to choose a positive integer k and replace each element s of the set S with s⊕k (⊕ denotes the exclusive or operation).

Help him choose such k that Johnny’s dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn’t matter, i.e. set {1,2,3} equals to set {2,1,3}.

Formally, find the smallest positive integer k such that {s⊕k|s∈S}=S or report that there is no such number.

For example, if S={1,3,4} and k=2, new set will be equal to {3,1,6}. If S={0,1,2,3} and k=1, after playing set will stay the same.

Input
In the first line of input, there is a single integer t (1≤t≤1024), the number of test cases. In the next lines, t test cases follow. Each of them consists of two lines.

In the first line there is a single integer n (1≤n≤1024) denoting the number of elements in set S. Second line consists of n distinct integers si (0≤si<1024), elements of S.

It is guaranteed that the sum of n over all test cases will not exceed 1024.

Output
Print t lines; i-th line should contain the answer to the i-th test case, the minimal positive integer k satisfying the conditions or −1 if no such k exists.

题意:

给定一个数组,此数组的元素互不相同,找到一个数k,k分别和数组中的所有元素进行异或运算,得到一个新数组,要求和原数组一模一样,求最小的k值。

题解思路(暴力解法)

1.穷举,k从1开始
2.将k与原数组每一个元素进行亦或,亦或出来的结果记为一个新数组
3.将新数组与原数组匹配
4.匹配成功一次记一次数
5.最后记的数和原数组的长度相等则是最小k(因为k是从1开始增大的)
6.如果这次异或出来的数和原来数组的元素都匹配不上,直接跳过这个数

代码:
emmm,代码我还在debug,尬了尬了!!晚点晚点,见谅见谅。

你可能感兴趣的:(C++算法)