HDU 4825 Xor Sum (01字典树)

70 篇文章 0 订阅

题意:给出一个数,在给定的一组数中找到与之异或值最大的数。

思路:裸01字典树,但是用之前能过5536题的模板T了(并不懂为什么,毕竟没有仔细研究模板如何实现),于是又找了一个新模板。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;

const int maxn = 100000 + 5;        //集合中的数字个数
typedef long long LL;
int ch[32 * maxn][2];               //节点的边信息
LL value[32 * maxn];                //节点存储的值
int node_cnt;                       //树中当前节点个数

inline void init(){                 //树清空
    node_cnt = 1;
    memset(ch[0],0,sizeof(ch));
}           

inline void Insert(LL x){           //在字典树中插入 X    
                                    //和一般字典树的操作相同 将X的二进制插入到字典树中
    int cur = 0;
    for(int i = 32;i >= 0;--i){
        int idx = (x >> i) & 1;
        if(!ch[cur][idx]){
            memset(ch[node_cnt],0,sizeof(ch[node_cnt]));
            ch[cur][idx] = node_cnt;
            value[node_cnt++] = 0;
        }
            cur = ch[cur][idx];
        }
        value[cur] = x;             //最后的节点插入value
}

inline LL Query(LL x){              //在字典树中查找和X异或的最大值的元素Y 返回Y的值
    int cur = 0;
    for(int i = 32;i >= 0;--i){
        int idx = (x >> i) & 1;
        if(ch[cur][idx ^ 1]) cur = ch[cur][idx ^ 1];
        else cur = ch[cur][idx];
    }
    return value[cur];
}
int main(){
	
	int t;
	scanf("%d", &t);
	for(int cas=1;cas<=t;cas++){
		init();
		int n,m;
		scanf("%d%d",&n,&m);
		for(int i=0;i<n;i++){
			LL a;
			scanf("%lld",&a);
			Insert(a);
		}
		printf("Case #%d:\n",cas);
		for(int i=0;i<m;i++){
			int b;
			scanf("%lld",&b);
			printf("%lld\n",Query(b));
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值