hdu 4825(Trie)

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

2014年百度之星程序设计大赛 - 资格赛


同Trie水题,但多叉字典树的模板不可用了,因为时间效率太过低下(每次都无法一次性定位),所以干脆用数组去做。

以下是超时模板:

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cstring>
  4 #define clr(x) memset(x,0,sizeof(x))
  5 #define clrmin(x) memset(x,-1,sizeof(x))
  6 #define LL long long
  7 using namespace std;
  8 struct node
  9 {
 10     int lt,rt;
 11     bool num;
 12     LL val;
 13 };
 14 struct Trie
 15 {
 16     int head,len;
 17     node tr[500010];
 18     Trie () { clr(tr); head=0; len=1;}
 19     void init()
 20     {
 21         clr(tr);
 22         head=0;
 23         len=1;
 24         return ;
 25     }
 26     int newnode(LL num,int dep)
 27     {
 28         if(!head)
 29         {
 30             head=len;
 31         }
 32         tr[len].num=((num>>(32-dep))%2)^1;
 33         return len++;
 34     }
 35     void push(int fa,int now,LL num,int dep)
 36     {
 37         int p;
 38         if(!now)
 39         {
 40             now=newnode(num,dep);
 41             tr[fa].lt=now;
 42             if(dep==32)
 43                 tr[now].val=num;
 44             else
 45                 push(now,0,num,dep+1);
 46             return ;
 47         }
 48         while(now && (((num>>(32-dep))%2)^1)!=tr[now].num)
 49         {
 50             p=now;
 51             now=tr[now].rt;
 52         }
 53         if(!now)
 54         {
 55             now=newnode(num,dep);
 56             tr[p].rt=now;
 57         }
 58         if(dep==32)
 59             tr[now].val=num;
 60         else
 61             push(now,tr[now].lt,num,dep+1);
 62         return;
 63     }
 64     LL getxor(int now,LL num,int dep)
 65     {
 66         int p;
 67         while(now && (num>>(32-dep))%2!=tr[now].num)
 68         {
 69             p=now;
 70             now=tr[now].rt;
 71         }
 72         if(!now)
 73         {
 74             if(dep==32)
 75                 return tr[p].val;
 76             else
 77                 return getxor(tr[p].lt,num,dep+1);
 78         }
 79         else
 80         {
 81             if(dep==32)
 82                 return tr[now].val;
 83             else
 84                 return getxor(tr[now].lt,num,dep+1);
 85         }
 86     }
 87 }tree;
 88 int main()
 89 {
 90     int T,n,m;
 91     LL num;
 92     scanf("%d",&T);
 93     for(int kase=1;kase<=T;kase++)
 94     {
 95         tree.init();
 96         printf("Case #%d:\n",kase);
 97         scanf("%d%d",&n,&m);
 98         for(int i=1;i<=n;i++)
 99         {
100             scanf("%lld",&num);
101             tree.push(0,tree.head,num,0);
102         }
103         for(int i=1;i<=m;i++)
104         {
105             scanf("%lld",&num);
106             printf("%lld\n",tree.getxor(tree.head,num,0));
107         }
108     }
109     return 0;
110 }
时间: 2024-08-10 19:11:23

hdu 4825(Trie)的相关文章

HDU 1247 Hat&#39;s words(Trie)

HDU 1247 Hat's words(Trie) ACM 题目地址: HDU 1247 Hat's words 题意: 给些单词,问每个单词是否能用另外两个单词拼出. 分析: 直接保存到trie里面,然后暴力切割查询即可. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: 1247.cpp * Create Date: 2014-09-24 11:04:11 * Descripton: */ #include <cstdio

hdu 1874(Dijkstra )

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27692    Accepted Submission(s): 10019 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路

hdu 4194(模拟)

符合三者之一的则不满足规定,求不满足规定的个数.直接模拟. 1.被同一个人审查多次 2.被和自己同一组织的审查 3.被审查次数不等于k 代码如下: 1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-06-28 17:36 5 * Filename :

hdu 4196(数论)

题意:问小于n的数的乘积能拼成的最大平方数是多少? 思路:给n!做质数分解在除去指数为奇数的那些质数,由于题目中需要模运算所以不能直接除,必须乘上摸逆. 代码如下: 1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-06-28 15:26 5 * Fi

Hdu 1402 (FFT)

题目链接 A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12490    Accepted Submission(s): 2206 Problem Description Calculate A * B. Input Each line will contain two integers A and

HDU 1695(GCD)

GCD Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u [Submit]   [Go Back]   [Status] Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common

HDU 1506 &amp;&amp; HDU1505 &amp;&amp; HDU 2870 (DP).

~~~~ 这三道DP题是逐层递进的,大家可以从前往后做. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1506 http://acm.hdu.edu.cn/showproblem.php?pid=1505 http://acm.hdu.edu.cn/showproblem.php?pid=2870 ~~~~ 1506: 分别找每一块板子的可以的最左边界和最右边界,注意这时不能用两个for暴力(显然会TLE),所以要用DP的思想边搜边保存. 另外注

hdu 1800 (map)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10830    Accepted Submission(s): 3472 Problem Description In the year 8888, t

HDU 4821 (hash)

这道题最重要的不仅是hash这种算法,更要学会利用好STL中的<map>才行. 将连续的L个字符经过hash赋值,最后线性判断.其中的判断步骤用到了map的插入特性. #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <map> using namespace std; #define maxn 500010 #