【01字典树】HDU 4825 Xor Sum

HDU 4825 Xor Sum

  • 题意:求N个数中和x异或最大的数
  • 思路:01字典树

【注意】

  1. N个数,每个数最大32位,首先肯定是超了int的,所以结果肯定要用longlong。
  2. N个数,每个数最大32位,那么tire最多有N*32个结点标号,也就是32e5,所以我们的tire数组可以开4e6就可以。
  3. 另外开了一个 val 数组,val[ i ] = x来存储 i 标号的结点处是x的最后一位。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <list>
#include <map>
#define INF 0x3f3f3f3f
#define P(x) x>0 ? x : 0
#define MID l + r >> 1
#define lsn rt << 1
#define rsn rt << 1 | 1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr

using namespace std;
typedef long long ll;
typedef vector<int>:: iterator VITer;
const int maxN=4e6+5;
ll T,N,M;
ll tire[maxN][2],val[maxN],rt,tot;

void init()
{
    memset(tire, 0, sizeof(tire));
    memset(val, 0, sizeof(val));
    tot=0;
}

void Insert(ll x)
{
    rt=0;
    for(ll i = 31; i >= 0; i--)
    {
        ll id = (x >> i) & 1;
        if(! tire[rt][id]) tire[rt][id] = ++tot;
        rt = tire[rt][id];
    }
    val[rt] = x;
}

ll Search(ll x)
{
    rt=0;
    for(ll i=31 ; i >= 0; i--)//从高位开始找,保证异或最大
    {
        ll id= (x >> i) & 1;
        if( tire[rt][id ^ 1] )//0 ^ 1 = 1 ; (id为0找1) 1 ^ 1 = 0; (id为1找0)
            rt = tire[rt][id ^ 1];//存在的话就以这个结点为rt继续往下找
        else
            rt = tire[rt][id];//如果没有的话那就只能为id了,此位相同,继续向下找不同的。尽量保证不同的位数多
    }
    return val[rt];
}

int main()
{
    scanf("%lld", &T);
    for(ll Case = 1; Case <= T; Case ++)
    {
        init();
        scanf("%lld%lld", &N, &M);
        for(ll i = 1 ; i <= N; i++)
        {
            ll d; scanf("%lld", &d);
            Insert(d);
        }
        printf("Case #%lld:\n", Case);
        for(ll i=1 ; i <= M ; i++)
        {
            ll d; scanf("%lld", &d);
            printf("%lld\n", Search(d));
        }
    }
    return 0;
}

二刷1A代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxN = 3200000 + 7;
int n, m;
int tire[maxN][2], tot;
ll val[maxN];
void Clear(int rt)
{
    for(int i = 0; i <= 1; i ++ ) tire[rt][i] = 0;
}
void Insert(ll num)
{
    int rt = 0;
    for(int i = 31; i >= 0; i -- )
    {
        int id = num >> i & 1;//二进制位上是多少
        if(!tire[rt][id]) { tire[rt][id] = ++ tot; Clear(tot); }
        rt = tire[rt][id];
    }
    val[rt] = num;
}
ll Search(ll num)
{
    int rt = 0;
    for(int i = 31; i >= 0; i -- )
    {
        int id = num >> i & 1;
        if(tire[rt][id ^ 1]) rt = tire[rt][id ^ 1];
        else rt = tire[rt][id];
    }
    return val[rt];
}
int main()
{
    int cas = 0;
    int TAT; scanf("%d", &TAT);
    while(TAT -- )
    {
        tot = 0; Clear(0);
        scanf("%d%d", &n, &m);
        for(int i = 0; i < n; i ++ )
        {
            ll num; scanf("%lld", &num);
            Insert(num);
        }
        printf("Case #%d:\n", ++cas);
        for(int i = 0; i < m; i ++ )
        {
            ll num; scanf("%lld", &num);
            printf("%lld\n", Search(num));
        }
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值