Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 数组所有子集中的最大异或_Algorithm - Fatal编程技术网

Algorithm 数组所有子集中的最大异或

Algorithm 数组所有子集中的最大异或,algorithm,Algorithm,我必须在数组子集的元素中找到异或的最大值。我必须检查数组的每个子集,得到最大xor的子集就是答案 例如,设FS表示对数组p={1,2,3,4}的子集S的所有元素取xor的函数 最多7个。因此答案是7。还有其他子集,但它们不值得考虑。若你们要告诉我高斯消去法,我已经在MSE上读到了,但我一点也不清楚。 如果高斯消去法是唯一的答案,请向我详细说明,或者有什么方法/算法我不知道?我想你指的是这个问题 高斯消去法是我在数学网站上期望得到的算法描述。这就是Python中的算法 def max_xor(it

我必须在数组子集的元素中找到异或的最大值。我必须检查数组的每个子集,得到最大xor的子集就是答案

例如,设FS表示对数组p={1,2,3,4}的子集S的所有元素取xor的函数

最多7个。因此答案是7。还有其他子集,但它们不值得考虑。若你们要告诉我高斯消去法,我已经在MSE上读到了,但我一点也不清楚。
如果高斯消去法是唯一的答案,请向我详细说明,或者有什么方法/算法我不知道?

我想你指的是这个问题

高斯消去法是我在数学网站上期望得到的算法描述。这就是Python中的算法

def max_xor(iterable):
    array = list(iterable)  # make it a list so that we can iterate it twice
    if not array:  # special case the empty array to avoid an empty max
        return 0
    x = 0
    while True:
        y = max(array)
        if y == 0:
            return x
        # y has the leading 1 in the array
        x = max(x, x ^ y)
        # eliminate
        array = [min(z, z ^ y) for z in array]

高斯消去法是你需要的

例如:3个数字{9,8,5}

首先按降序排序,并将其转换为二进制:

9 : 1001
8 : 1000
5 : 0101
注意第一个数字。最高位是4。 现在检查第1个数字9的第4位。当它为1时,将数字与其余数字进行异或运算,其中第4位为1

9 : 1001
1 : 0001 > changed
5 : 0101
8 : 1000 > changed
4 : 0100 > changed
1 : 0001
现在检查第2个数字1的第3位。由于它是0,请检查下面第3位为1的其余数字。 数字5的第3位有1。交换:

9 : 1001
5 : 0101 > swapped
1 : 0001 >
现在使用xor 5和其余数字,其中第3位为1。这里一个也不存在。所以不会有变化

现在检查第3个数字1的第2位。因为它是0,并且在第二位为1的位置下面没有其他数字,所以不会有任何更改

现在检查第3个数字1的第一位。因为它是1,所以更改第1位为1的其余数字

9 : 1001
1 : 0001 > changed
5 : 0101
8 : 1000 > changed
4 : 0100 > changed
1 : 0001

不再考虑:

现在对剩余的整个数组{8^4^1}进行异或=13

因此,13是解决方案:

这就是使用高斯消去法解决问题的基本方法:

这是我的C++实现:

#include <bits/stdc++.h>
using namespace std;

typedef long long int           ll;
typedef unsigned long long int  ull;

ull check_bit(ull N,int POS){return (N & (1ULL<<POS));}

vector<ull>v;
ull gaussian_elimination()
{
    int n=v.size();
    int ind=0;  // Array index
    for(int bit=log2(v[0]);bit>=0;bit--)
    {
        int x=ind;
        while(x<n&&check_bit(v[x],bit)==0)
          x++;
        if(x==n)
          continue; // skip if there is no number below ind where current bit is 1
        swap(v[ind],v[x]);
        for(int j=0;j<n;j++)
        {
            if(j!=ind&&check_bit(v[j],bit))
                v[j]^=v[ind];
        }
        ind++;
    }
    ull ans=v[0];
    for(int i=1;i<n;i++)
      ans=max(ans,ans^v[i]);
    return ans;
}
int main()
{
    int i,j,k,l,m,n,t,kase=1;
    scanf("%d",&n);
    ull x;
    for(i=0;i<n;i++)
    {
        cin>>x;
        v.push_back(x);
    }
    sort(v.rbegin(),v.rend());
    cout<<gaussian_elimination()<<"\n";
return 0;
}

子集{1,2,4}=7不需要考虑吗?数组P的元素是否有任何特殊属性?@ JavaMSCs没有特殊属性。@ AialbBER,你也可以考虑。我的意思是答案仍然是7。是的,我指的是那个问题。你能告诉我这个python实现中发生了什么吗?@johnkeets查找数组中最重要的一位。最大异或设置了此位。使用XOR在最大XOR中设置它,并在数组的其余部分清除它,然后解决较小的子问题。此步骤的操作->数组=[minz,z^y代表数组中的z]@Ray After array=listiterable添加行数组=[z@Ray当输入限制为64位时,y最多有64个枢轴元素可供选择。我们可以使用64个低阶位,而不是n个低阶位。除了索引到原始数组之外,它们索引到枢轴列表中。我们在y上设置适当的位,而不是在前面邻接一个标识矩阵st,然后再使用它减少阵列的其余部分。如何修改它以获得最小值?