zoukankan      html  css  js  c++  java
  • hdu 4825 Xor Sum (01 Trie)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825

    题面:

    Xor Sum

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


    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
     
    思路:
    01 Trie 模板题
     
    实现代码:
    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    const int M = 1e5+10;
    int tot;
    int ch[32*M][2];
    ll val[32*M];
    
    void init(){
        tot = 1;
        ch[0][0] = ch[0][1] = 0;
    }
    
    void ins(ll x){
        int u = 0;
        for(int i = 32;i >= 0;i --){
            int v = (x>>i)&1;
            if(!ch[u][v]){
                ch[tot][0] = ch[tot][1] = 0;
                val[tot] = 0;
                ch[u][v] = tot++;
            }
            u = ch[u][v];
        }
        val[u] = x;
    }
    
    ll query(ll x){
        int u = 0;
        for(int i = 32;i >= 0;i --){
            int v = (x>>i)&1;
            if(ch[u][v^1]) u = ch[u][v^1];
            else u = ch[u][v];
        }
        return val[u];
    }
    
    int main()
    {
        ios::sync_with_stdio(0);
        cin.tie(0); cout.tie(0);
        int t,n,m,cas = 1;
        ll x;
        cin>>t;
        while(t--){
            cin>>n>>m;
            init();
            for(int i = 1;i <= n;i ++)
                cin>>x,ins(x);
            cout<<"Case #"<<cas++<<":"<<endl;
            for(int i = 1;i <= m;i ++)
                cin>>x,cout<<query(x)<<endl;
        }
        return 0;
    }
  • 相关阅读:
    网络安全协议(1)
    CG-CTF(6)
    CG-CTF(5)
    CG-CTF(4)
    CG-CTF(3)
    MAC地址欺骗(原理及实验)
    CG-CTF(2)
    CG-CTF(1)
    【转载】Spring Boot【快速入门】2019.05.19
    【编程大系】Java资源汇总
  • 原文地址:https://www.cnblogs.com/kls123/p/10723137.html
Copyright © 2011-2022 走看看