有n个32位无符号整数,求其中异或之后结果最大的两个数
2010-05-14 16:28
274 查看
链接: http://discuss.joelonsoftware.com/default.asp?interview.11.614716
find the max value after XORing?
Give n 32bit numbers which are all unsigned integers, then find thetwo numbers that can get the maximum value after their XOR.
e.g.
for
these numbers: 1, 2, 3, 4, 0xFFFFFFFE,
we should return 1 and
0xFFFFFFFE whose XOR result is the max.
please give a algorithm
with time complexity <= nlgn, O(n)would be better.
CSDN讨论地址: http://topic.csdn.net/u/20100511/12/3044a051-4fad-40bc-b312-325fa4b7617a.html?19376
解法1:
I think the best approach to this would be a series of bucket sorts
based on which bits of the numbers are set.
So you'd start by
bucket-sorting the inputs according to their highest set bit. That is,
the kth bucket gets all inputs between 2^k and 2^(k+1)-1. Now the
highest-value (nonempty) bucket is guaranteed to contain one of your
outputs.
Then, you'd want to sort the numbers in that bucket by
their highest unset bit (after the first set one). If the highest value
bucket of this second sort has a smallish number of elements, then you
would start comparing them to the comparable bucket from your first
sort, to find a maximal pair. If not, you could further sort them by the
highest set bit after the highest unset bit. And so on.
For
general data I think this would wind up close to O(N). But for perverse
inputs I suspect you'd wind up doing around logN sorts. So it smells
like an NlogN problem in the worst case.
Any of that sound right?
解法2:
I think this can be done in O(n).
I have a two-pass algorithm:
1.
The first pass builds a binary tree.
. The tree has n leaves
corresponding to the n given numbers.
. Each leaf has a depth of
32.
. When you add a leaf, you start from the MSB, if 0 go left,
if 1 go right.
2. In the second pass, for each number x, bit
reverse (m=~x) it first, and then find the best "match" of m in the
tree.
The "match" looks like the following:
unsigned
long BestMatch(unsigned long m, node * pRoot)
{
unsigned long match = 0;
for (int i=31; i>=0; i--)
{
match <<= 1;
BYTE bit =
(m>>i)&0x01;
if (bit)
{
if (p->right)
{
p
= p->right;
match ++;
}
else
{
p =
p->left;
}
}
else
{
if (p->left)
{
p = p->left;
match ++;
}
else
{
p = p->right;
}
}
}
return match;
}
收获:时间复杂度虽然 o(n) 要看着比 o(n*logn)好,但事实上有时候 o(n)实际上是o(k*n),而 k的大小并不一定比 log (n)好
相关文章推荐
- a="abxc",b="ehdi",输出“abxehdic”即把b串插入到a串ascii码最大字符之后,并输出,其中a,b由用户自己输入
- 输入一个32位的整数a,使用按位异或^运算,生成一个新的32位整数b,使得该整数b的每一位等于原整数a中该位左右两边两个bit位的异或结果
- poj 3764 The xor-longest Path 找异或值最大的两个数
- 位运算训练05—输入一个32位的整数a,使用按位异或^运算,生成一个新的32位整数b; 使得该整数b的每一位等于原整数a中该位左右两边两个bit位的异或结果
- Hailstone 在32位无符号整数中最大的跳跃次数
- CodeForces - 892C Pride (n个数,求相邻两个数的最大公约数替换其中一个数,看要操作几次使得n个数全部变成1)
- 求两个数异或最大值
- 使用求两个数的最大值和相减结果的绝对值?
- 2.5输入一个32位的整数a,使用按位异或^运算,生成一个新的32位整数b,使得该整数b的每一位等于原整数a中该位左右两边bit位的异或结果
- 有一个直方图,用一个整数数组表示,其中每列的宽度为1,求所给直方图包含的最大矩形面积。比如,对于直方图[2,7,9,4],它所包含的最大矩形的面积为14(即[7,9]包涵的7x2的矩形)。给定一个直方图A及它的总宽度n,请返回最大矩形面积。保证直方图宽度小于等于500。保证结果在int范围内。
- 字典树的应用:求数组中异或最大的两个数
- Windows平台下GCC编程之从键盘输入3个整数,求其中的最大数和最小数,并输出结果
- 输入个32位的整数a,使用按位异或^运算,生成一个新的32位整数b,使得该整数b的每一位等于原整数a中该位左右两边两个bit位的异或结果
- 输入一个32位的整数a,使用按位异或^运算,生成一个新的32位整数b,使得该整数b的每一位等于原整数a中该位左右两边两个bit位的异或结果
- 5.题目:输入一个32位的整数a,使用按位异或^运算,生成一个新的32位整数b,使得该整数b的每一位等于原整数a中该位左右两边两个bit位的异或结果 提示:0 ^ 0 = 0; 1 ^ 1 = 0;
- 数组中两个数异或求最大值
- 两个数做异或运算的结果,可以反向推断出运算的是哪两个数的小技巧
- 输入一个32位的整数啊,使用按位异或运算,生成一个新的32位整数b,使得该整数b的每一位等于原整数a中该位左右两边两个bit位的异或结果
- 【求两个数异或的最大值】01字典树求解
- Codeforces 706D Vasiliy's Multiset (字典树求异或最大值)