• hdu-4825(01字典树)


    Xor Sum

    hdu-4825(01字典树)

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


    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
     
    #pragma GCC optimize(2)
    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <cstring>
    #include <algorithm>
    typedef long long ll;
    ll read(){
        ll x=0; ll f=1;
        char ch=getchar();
        while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();}
        while(ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
        return x*f;
    }
    using namespace std;
    const int maxn=3e6+100;
    int son[maxn][2];
    ll val[maxn];
    int cnt=0;
    int t,n,m;
    void jianshu(ll x){
        int root=0;
        for(int i=32;i>=0;i--){
            int op=(x>>i)&1ll;
            if(!son[root][op]) son[root][op]=++cnt;
            root=son[root][op]; 
        }
        val[root]=x;
    } 
    ll chaxun(ll x){
        int root=0;
        for(int i=32;i>=0;i--){
            int op=(x>>i)&1ll;
            if(son[root][!op]){
                root=son[root][!op];
            }
            else{
                root=son[root][op];
            }
        }
        return val[root];
    }
    int main(){
        t=read();
        int tt=1; 
        while(t--){
            memset(son,0,sizeof(son));
            cin>>n>>m;
            ll p;
            for(int i=1;i<=n;i++){
                p=read();
                jianshu(p);
            }
            printf("Case #%d:
    ",tt++);
            for(int i=1;i<=m;i++){
                ll d;
                d=read();
                printf("%lld
    ",chaxun(d)); 
            } 
        }
    }
     
  • 相关阅读:
    C# Devexpress GridView获得当前活动行(选中行)的索引
    C# Devexpress GridView获得当前活动行(选中行)的索引
    计算机网络学习总结(超赞!!!)
    计算机网络学习总结(超赞!!!)
    30张图带你彻底理解红黑树
    30张图带你彻底理解红黑树
    DevExpress GridView 添加和设置右键菜单
    DevExpress GridView 添加和设置右键菜单
    数据库分离和附加 (SQL Server)
    数据库分离和附加 (SQL Server)
  • 原文地址:https://www.cnblogs.com/lipu123/p/13397330.html
Copyright © 2020-2023  润新知