Xor Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 1786    Accepted Submission(s): 758

Problem Description
Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大。Prometheus 为了让 Zeus 看到人类的伟大,随即同意 Zeus 可以向人类求助。你能证明人类的智慧么?
 
Input
输入包含若干组测试数据,每组测试数据包含若干行。
输入的第一行是一个整数T(T < 10),表示共有T组数据。
每组数据的第一行输入两个正整数N,M(<1=N,M<=100000),接下来一行,包含N个正整数,代表 Zeus 的获得的集合,之后M行,每行一个正整数S,代表 Prometheus 询问的正整数。所有正整数均不超过2^32。
 
Output
对于每组数据,首先需要输出单独一行”Case #?:”,其中问号处应填入当前的数据组数,组数从1开始计算。
对于每个询问,输出一个正整数K,使得K与S异或值最大。
 
Sample Input
2
3 2
3 4 5
1
5
4 1
4 6 5 6
3
 
Sample Output
Case #1:
4
3
Case #2:
4
 
Source

同Trie水题,但多叉字典树的模板不可用了,因为时间效率太过低下(每次都无法一次性定位),所以干脆用数组去做。
以下是超时模板:
 #include<cstdio>
#include<iostream>
#include<cstring>
#define clr(x) memset(x,0,sizeof(x))
#define clrmin(x) memset(x,-1,sizeof(x))
#define LL long long
using namespace std;
struct node
{
int lt,rt;
bool num;
LL val;
};
struct Trie
{
int head,len;
node tr[];
Trie () { clr(tr); head=; len=;}
void init()
{
clr(tr);
head=;
len=;
return ;
}
int newnode(LL num,int dep)
{
if(!head)
{
head=len;
}
tr[len].num=((num>>(-dep))%)^;
return len++;
}
void push(int fa,int now,LL num,int dep)
{
int p;
if(!now)
{
now=newnode(num,dep);
tr[fa].lt=now;
if(dep==)
tr[now].val=num;
else
push(now,,num,dep+);
return ;
}
while(now && (((num>>(-dep))%)^)!=tr[now].num)
{
p=now;
now=tr[now].rt;
}
if(!now)
{
now=newnode(num,dep);
tr[p].rt=now;
}
if(dep==)
tr[now].val=num;
else
push(now,tr[now].lt,num,dep+);
return;
}
LL getxor(int now,LL num,int dep)
{
int p;
while(now && (num>>(-dep))%!=tr[now].num)
{
p=now;
now=tr[now].rt;
}
if(!now)
{
if(dep==)
return tr[p].val;
else
return getxor(tr[p].lt,num,dep+);
}
else
{
if(dep==)
return tr[now].val;
else
return getxor(tr[now].lt,num,dep+);
}
}
}tree;
int main()
{
int T,n,m;
LL num;
scanf("%d",&T);
for(int kase=;kase<=T;kase++)
{
tree.init();
printf("Case #%d:\n",kase);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%lld",&num);
tree.push(,tree.head,num,);
}
for(int i=;i<=m;i++)
{
scanf("%lld",&num);
printf("%lld\n",tree.getxor(tree.head,num,));
}
}
return ;
}

hdu 4825(Trie)的更多相关文章

  1. Repository HDU - 2846 (trie)

    题中没给范围 所以控制不好数组范围..不是超内存就是runtime.. 好吧 到了晚上终于调出来数组模拟的了 题意: 求含有某字符段的个数 解析: 把每个字符串遍历一遍 以每个元素为起点建树就好了.. ...

  2. A * B Problem Plus HDU - 1402 (FFT)

    A * B Problem Plus HDU - 1402 (FFT) Calculate A * B.  InputEach line will contain two integers A and ...

  3. D - 淡黄的长裙 HDU - 4221(贪心)

    D - 淡黄的长裙 HDU - 4221(贪心) James is almost mad! Currently, he was assigned a lot of works to do, so ma ...

  4. 【python】Leetcode每日一题-前缀树(Trie)

    [python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...

  5. HDU 1671 Phone List (Trie)

    pid=1671">Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  6. hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)

    统计难题Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submis ...

  7. LA3942-Remember the Word(Trie)

    题意: 有s个不同的单词,给出一个长字符串把这个字符串分解成若干个单词的连接(可重复使用),有多少种分解方法 分析: dp[i]表示i开始的字符串能分解的方法数 dp[i]=sum(dp[i+len( ...

  8. hdu 5055(坑)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5055 Bob and math problem Time Limit: 2000/1000 MS ( ...

  9. hdu 5391 (数论)

    Zball in Tina Town Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

随机推荐

  1. 【51NOD-0】1019 逆序数

    [算法]离散化+树状数组(求逆序对) [题解]经典,原理是统计在i之前插入的且值≤i的个数,然后答案就是i-getsum(i) #include<cstdio> #include<a ...

  2. 头像截取 图片上传 js插件

    先看一下整体效果 页面html <div class="row"> <div class="tabs-container"> <u ...

  3. 【shell】shell编程(六)-shell函数的应用

    linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. shell中函数的定义格式如下: [ function ] funname [()] { action; [return ...

  4. java和C和C++关系

    java和C以及C++ 直接关联,java继承了C的语法,java的对象模型是从C++改编而来的.java和C以及C++关系之所以重要,下面几个就是原因: ①如果一个程序员熟悉C以及C++语法,那么他 ...

  5. Python阶段复习 - part 4 - 用户登录程序

    简易版: #!/usr/bin/env python # _*_ coding:UTF-8 _*_ # __auth__:Dahlhin import sys userinfo = r'userinf ...

  6. Python3安装Celery模块后执行Celery命令报错

    1 Python3安装Celery模块后执行Celery命令报错 pip3 install celery # 安装正常,但是执行celery 命令的时候提示没有_ssl模块什么的 手动在Python解 ...

  7. Linux内核通知链分析【转】

    转自:http://www.cnblogs.com/jason-lu/articles/2807758.html Linux内核通知链分析 1. 引言 Linux是单内核架构(monolithic k ...

  8. 利用keepalive+mysql replication 实现数据库的高可用

    利用keepalive+mysql replication 实现数据库的高可用 http://www.xuchanggang.cn/archives/866.html

  9. 10 个打造 React.js App 的最佳 UI 框架

    10 个打造 React.js App 的最佳 UI 框架 在本文中,我们将分享一些助你打造 React.js App 最佳的 UI 框架.它们具备你所需要的基本 React 组件,以及易用的 API ...

  10. 后台传入的boolean类型到前台alert为String类型了(解决方法)

    后台代码: // 进入仪器list界面之前查看 是否是科研处人员        SessionContainer sc = (SessionContainer) session.getAttribut ...