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. python学习笔记(一)之为什么学习python

    python的特点: 跨平台 实现同一个功能是Java代码的1/5 python应用范围: 操作系统 web 3D动画 企业应用 云计算 如何学习python? 学习语法 验证例子 学会总结 课外实践

  2. 解决java在对MySQL插入数据时出现乱码问题

    1.在连接数据库的时候请注意, 最使用连接连接数据库的时候,必须在后面追加上编码的设置:useUnicode=true&characterEncoding=UTF-8,如下图所示. 参考连接: ...

  3. vs调试 配置IISExpress允许局域网内部访问

    内网可访问后,本机不能使用localhost   1.找到IISExpress的配置文件,位于 <文档>/IISExpress/config文件夹下,打开applicationhost.c ...

  4. Oracle 获取ddl语句

    --得到所有表空间的ddl语句 SELECT DBMS_METADATA.GET_DDL('TABLESPACE', TS.tablespace_name)FROM DBA_TABLESPACES T ...

  5. Winfrom窗体间传值

    1.通过tag属性传输,tag属性是存储与空间密切相关的数据.比如登陆界面的数据传输给主界面. 子窗体                                                 ...

  6. tornado 模版继承 函数和类的调用

    模版继承.函数和类的调用 目录结构 lesson5.py # -*- coding:utf-8 -*- import tornado.web import tornado.httpserver imp ...

  7. vue 的过滤器

    1.0版本: limitBy filteBy orderBy lowerBy upperBy json currency capitalize pluralize debounce 2.0版本: 要自 ...

  8. NEERC 2016-2017 Probelm G. Game on Graph

    title: NEERC 2016-2017 Probelm G. Game on Graph data: 2018-3-3 22:25:40 tags: 博弈论 with draw 拓扑排序 cat ...

  9. Linux 基础——开山篇

    为什么要开始学习Linux命令? 首先当然是因为工作需要了,现在的工作是负责银行调度的系统的源系统接入的工作,经常要到生产部署版本.所以……买了一本<Linux命令行与shell脚本编程大全&g ...

  10. 半透明AlphaBlend

    AlphaBlend 函数功能:该函数用来显示透明或半透明像素的位图. 函数原型: BOOL AlphaBlend( HDC hdcDest, // handle to destination DC ...