Trie/Xor
来源:互联网 发布:linux history 参数 编辑:程序博客网 时间:2024/05/13 01:06
题目链接
/*有一个数组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;}
0 0
- Trie/Xor
- hdu4825 Xor Sum【Trie、Xor】
- hdu4825---Xor Sum(Trie + 贪心)
- Trie树 hdu4825 Xor Sum
- 双剑合并 XOR Trie.
- hdu 5715 XOR 游戏(trie)
- Poj 3764 The xor-longest Path(Trie树+xor+贪心)
- HDU4825 - Xor Sum(Trie 字典树)
- pojThe xor-longest Path(trie树)
- HDU 4825 Xor Sum(trie树+贪心)
- [codevs 1187]Xor最大路径 Trie+贪心
- 【bzoj1954】【The xor-longest Path】【trie树】
- HDU4825 Xor Sum(Trie树 + 贪心)
- HDU 4825 Xor Sum [trie]【思维&字符串】
- hdu5845 Best Division (xor-trie+dp)
- trie-bzoj1954-poj3764-The xor-longest Path
- POJ 3764 The xor-longest Path Trie
- HDU4825 Xor Sum(Trie树,二进制)
- 百度编辑器UEditor 图片上传到资源服务器 JSP
- hdu5812Distance(数学)
- 基于opencv3.1的cnn分类系统集成
- 各个字号对应的px大小
- how to import github Oauth token
- Trie/Xor
- hdu 5810 优先队列
- 区间dp_2
- Volley与Activity生命周期联动及简单的二次封装
- POJ 1273 Drainage Ditches
- Java系统中异常封装处理
- c# 读取写入excel单元格(包括对excel的一些基本操作)【总结篇】
- git编译安装与常用命令
- tomcat如何配置项目文件夹外的虚拟子目录?