Trie/Xor
/*有一个数组a1,a2,a3……an。找到一个连续子段[l,r],使得al ^ al+1 ^……^ ar达到最大。
一般思路:维护前缀异或+暴力;
for(int i=1;i<=n;i++)
a[i]^=a[i-1];
for(int i=1;i<=n;i++)
for(int j=1;j<i;i++)
ans=max(ans,a[i]^a[j]);
数据量很大,暴力不行。
维护前缀异或+Trie优化;
*/
//指针版本
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1000000+5;
const int si=23;
int a[maxn];
int n;
struct node
{
node *chi[2];
void init()
{
chi[0]=NULL;
chi[1]=NULL;
}
};
void Insert(int k,node * root)
{
for(int i=si;i>=0;i--)
{
if((k&(1<<i)))
{
if(root->chi[1]==NULL)
{
node *t=(node *)malloc(sizeof(node));
t->init();
root->chi[1]=t;
}
root=root->chi[1];
}
else
{
if(root->chi[0]==NULL)
{
node *t=(node *)malloc(sizeof(node));
t->init();
root->chi[0]=t;
}
root=root->chi[0];
}
}
}
int query(int k,node *root)
{
int ret=0;
for(int i=si;i>=0;i--)
{
if(k&(1<<i))//找0
{
if(root->chi[0]!=NULL)
{
root=root->chi[0];
}
else
{
root=root->chi[1];
ret+=(1<<i);
}
}
else//找1
{
if(root->chi[1]!=NULL)
{
root=root->chi[1];
ret+=(1<<i);
}
else
{
root=root->chi[0];
}
}
}
return k^ret;
}
void rease(node * root)
{
if(root->chi[0]!=NULL)
rease(root->chi[0]);
if(root->chi[1]!=NULL)
rease(root->chi[1]);
delete root;
}
int main ()
{
while(~scanf("%d",&n))
{
node *root=(node *)malloc(sizeof(node));
root->init();
Insert(0,root);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]^=a[i-1];
Insert(a[i], root);
}
int ans=0;
for(int i=1;i<=n;i++)
{
ans=max(ans,query(a[i], root));
}
printf("%d\n",ans);
rease(root);
}
return 0;
}
//数组版本
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1000000+5;
const int si=23;
int a[maxn];
int tree[maxn*3][2];
int cnt=0;
int n;
void Insert(int k)
{
int root=0;
for(int i=si;i>=0;i--)
{
if((k&(1<<i)))
{
if(tree[root][1]==0)
{
cnt++;
tree[root][1]=cnt;
}
root=tree[root][1];
}
else
{
if(tree[root][0]==0)
{
cnt++;
tree[root][0]=cnt;
}
root=tree[root][0];
}
}
}
int query(int k)
{
int root=0;
int ret=0;
for(int i=si;i>=0;i--)
{
if(k&(1<<i))//找0
{
if(tree[root][0]!=0)
{
root=tree[root][0];
}
else
{
root=tree[root][1];
ret+=(1<<i);
}
}
else//找1
{
if(tree[root][1]!=0)
{
root=tree[root][1];
ret+=(1<<i);
}
else
{
root=tree[root][0];
}
}
}
return k^ret;
}
int main ()
{
while(~scanf("%d",&n))
{
memset(tree,0,sizeof(tree));
cnt=0;
Insert(0);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]^=a[i-1];
Insert(a[i]);
}
int ans=0;
for(int i=1;i<=n;i++)
{
ans=max(ans,query(a[i]));
}
printf("%d\n",ans);
}
return 0;
}
Trie/Xor的更多相关文章
- HDU4776 Ants(Trie && xor)
之前mark下来的一道题,今天填一下坑. 题意是这样子的.给你一棵边上有权的树.然后有树上两点(u,v)的路径有n*(n-1)条,路径(u,v)的权值是边权的xor. 然后下面有m个询问,询问你n*( ...
- Codeforces.842D.Vitya and Strange Lesson(Trie xor)
题目链接 /* 异或只有两种情况,可以将序列放到01Tire树上做 在不异或的情况下在Tire上查找序列的mex很容易,从高位到低位 如果0位置上数没有满,则向0递归:否则向1 (0位置上的数都满了 ...
- 二分+DP+Trie HDOJ 5715 XOR 游戏
题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- [USACO]6.1.3 cow xor(二进制+Trie)
题意:给你一个序列(n<=100000),求出一个连续的子序列[i,j]使得ai xor ai+1 xor…… xor aj最大,求出这个最大值(其中每个数<=2^21) 分析:题目和求一 ...
- usaco6.1-Cow XOR:trie树
Cow XOR Adrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All o ...
- 51nod1295 XOR key(可持久化trie)
1295 XOR key题目来源: HackerRank基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题 给出一个长度为N的正整数数组A,再给出Q个查询,每个查 ...
- hdu 4825 Xor Sum (01 Trie)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题面: Xor Sum Time Limit: 2000/1000 MS (Java/Others) ...
- 51nod 1295 XOR key (可持久化Trie树)
1295 XOR key 题目来源: HackerRank 基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题 给出一个长度为N的正整数数组A,再给出Q个查 ...
- 【xsy1147】 异或(xor) 可持久化trie
我的脑回路可能比较奇怪. 我们对这些询问离线,将所得序列${a}$的后缀和建$n$棵可持久化$trie$. 对于一组询问$(l,r,x)$,我们在主席树上询问第$l$棵树$-$第r$+1$棵树中与$s ...
随机推荐
- hdu_5969_最大的位或(贪心)
题目链接:hdu_5969_最大的位或 题意: 中文,还是自己看 题解: xjb贪心一下就行了 #include<bits/stdc++.h> #define F(i,a,b) for(i ...
- poj_1743_Musical Theme(后缀数组)
题目链接:poj_1743_Musical Theme 题意: 给你一串数字,让你找最长的变化相同不重叠的子串,至少长度为5 题解: 处理数据后用后缀数组加二分答案,然后用height数组check答 ...
- 6、50道JAVA基础编程练习题跟答案
50道JAVA基础编程练习题 [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 程序分析 ...
- JavaScript基础知识复习
1,javascript是基于对象和事件驱动的,并有安全性能的脚本语言: 2,javascript的特点: 1)向HTML中添加交互事件: 2)脚本语言,与java语法类似: 3)解释性语言,边执行边 ...
- webapi中的扩展点
Extension Points Web API provides extension points for some parts of the routing process. Interface ...
- Leetcode018 4Sum
public class S018 { //借鉴S015,速度有些慢 public List<List<Integer>> fourSum(int[] nums, int ta ...
- $and $not null 正则表达式
查询MasterID大于1且MasterType等于TestType的文档: db.SysCore.find({$and:[{"MasterID":{$gt:1}},{" ...
- Java Tcp文件传输---转载
/** 客户端 1.服务端点 2.读取客户端已有的文件数据 3.通过socket输出流发给服务端 4.读取服务端反馈信息 5.关闭 **/ import java.io.*; import java. ...
- Openjudge-计算概论(A)-鸡尾酒疗法
描述: 鸡尾酒疗法,原指“高效抗逆转录病毒治疗”(HAART),由美籍华裔科学家何大一于1996年提出,是通过三种或三种以上的抗病毒药物联合使用来治疗艾 滋病.该疗法的应用可以减少单一用药产生的抗药性 ...
- aspose 解决插入html后字体问题
/// <summary> /// 添加html /// </summary> /// <param name="strText"></p ...