• Xor Sum 01字典树 hdu4825


    Xor Sum

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


    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
     
     
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<vector>
     5 #include<queue>
     6 #define inf 0x3fffffff
     7 #define Memset(x, a) memset(x, a, sizeof(x))
     8 using namespace std;
     9 typedef long long LL;
    10 const int maxn = 100000 + 5;
    11 int tree[32 * maxn][2];
    12 LL val[32 * maxn];
    13 int sz;
    14 void init() {
    15     memset(tree[0], 0, sizeof(tree[0]));
    16  //   Memset(tree[0],0);
    17     sz = 1;
    18 }
    19 void add(LL a) {
    20     int u = 0;
    21     for (int i = 32 ; i >= 0 ; i--) {
    22         int b = ((a >> i) & 1) ;
    23         if (!tree[u][b]) {
    24            memset(tree[sz], 0, sizeof(tree[sz]));
    25            // Memset(tree[sz],0);
    26             val[sz] = 0;
    27             tree[u][b] = sz++;
    28         }
    29         u = tree[u][b];
    30     }
    31     val[u] = a;
    32 }
    33 LL query(LL a ) {
    34     int u = 0;
    35     for (int i = 32; i >= 0 ; i--) {
    36         int b = ((a >> i) & 1) ;
    37         if (tree[u][b ^ 1]) u = tree[u][b ^ 1];
    38         else u = tree[u][b];
    39     }
    40     return val[u];
    41 }
    42 int main() {
    43     int t, n, m, cas = 1;
    44     LL a;
    45     scanf("%d", &t);
    46     while(t--) {
    47         init();
    48         scanf("%d%d", &n, &m);
    49         for (int i = 0 ; i < n ; i++) {
    50             scanf("%lld", &a);
    51             add(a);
    52         }
    53         printf("Case #%d:
    ", cas++);
    54         for (int i = 0 ; i < m ; i++) {
    55             scanf("%d", &a);
    56             printf("%lld
    ", query(a));
    57         }
    58     }
    59     return 0;
    60 }
  • 相关阅读:
    fiddler抓取APP请求
    Docker解决没有vi、vim等命令
    postgresql数据库导入导出
    DockerFile简介以及使用
    Docker-容器数据卷
    Docker-commit镜像提交
    Spring Beans自动装配
    Spring Bean几种注入方式——setter(常用),构造器,注入内部Bean,注入集合,接口...
    Spring Bean的生命周期
    Spring IoC容器的初始化和依赖注入
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9054066.html
Copyright © 2020-2023  润新知