UVALIVE 4682: XOR Sum

解决一个算法问题,即寻找一组连续整数的异或和的最大值。通过使用前缀异或和与 Trie 树的数据结构来高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11008

题意:给定n个数,求一段连续的数是的他们的异或和最大。1<=n<=10^5

题解:在trie树上搞。首先预处理出sum[i]表示前i个数的异或和,那么第i个到第j个异或和就是sum[j]^sum[i-1]。然后可以建立一个高度为32的二叉字典树,每个节点的左儿子是0,右儿子是1。把sum[0]插入trie树中。枚举i,对于sum[i],从trie树的根节点出发,贪心的往前位异或值为1的方向头,更新答案,然后将sum[i]插入trie树中。很容易证明这个算法是对的。

代码君:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int maxn = 100010;

struct Node {
	Node *next[2];
};

int tot = 0, n, a[maxn];
Node pool[maxn*40], *root;

Node *newNode() {
	Node *p = &pool[tot++];
	memset(p->next, 0, sizeof(p->next));
	return p;
}

void init() {
	tot = 0;
	root = newNode();
}

void insert(int x) {
	Node *cur = root;
	for (int i = 30; i >= 0; i--) {
		if (x & (1 << i)) {
			if (cur->next[1] == NULL)
				cur->next[1] = newNode();
			cur = cur->next[1];
		} else {
			if (cur->next[0] == NULL)
				cur->next[0] = newNode();
			cur = cur->next[0];
		}
	}
}

int query(int x) {
	Node *cur = root;
	int res = 0;
	for (int i = 30; i >= 0; i--) {
		if (x & (1 << i)) {
			if (cur->next[0]) {
				res |= (1 << i);
				cur = cur->next[0];
			} else {
				cur = cur->next[1];
			}
		} else {
			if (cur->next[1]) {
				res |= (1 << i);
				cur = cur->next[1];
			} else {
				cur = cur->next[0];
			}
		}
	}
	return res;
}

int main() {
	int test;
	scanf("%d", &test);
	for (int cas = 1; cas <= test; cas++) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) {
			scanf("%d", &a[i]);
			a[i] ^= a[i-1];
		}
		int ans = 0;
		init();
		for (int i = 0; i < n; i++) {
			insert(a[i]);
			ans = max(ans, query(a[i+1]));
		}
		printf("%d\n", ans);
	}
	return 0;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值