zoukankan      html  css  js  c++  java
  • hdu1825 Xor Sum

    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 leqslant N,M leqslant 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

    Solution

    Trie傻逼题。然而我因为各种小问题调了很久,就写一下注意事项。

    • 不要query的时候不要用"~",直接用异或。
    • 数组空间开够……
    • 不要把trie开成bool(当然这条也许只有我会犯)
    • 不要老想着压行。
    #include<map>
    #include<cstdio>
    #include<vector>
    #include<string>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    #define ll long long
    inline ll read() {
    	ll x = 0, flag = 1; char ch = getchar();
    	while (ch > '9' || ch < '0') { if (ch == '-') flag = -1; ch = getchar(); }
    	while (ch <= '9' && ch >= '0') { x = x * 10 + ch - '0'; ch = getchar(); }
    	return x * flag;
    }
    inline void write(ll x) { if (x >= (ll)10) write(x / (ll)10); putchar(x % (ll)10 + '0'); }
    
    #define base 32
    #define N 100001
    #define rep(i, a, b) for (int i = a; i <= b; i++)
    #define drp(i, a, b) for (int i = a; i >= b; i--)
    #define fech(i, x) for (int i = 0; i < x.size(); i++)
    
    int n;
    ll a[N];
    int trie[32 * N][5];
    int tot;
    int End[32 * N];
    bool str[35];
    
    void getString(ll x) {
    	memset(str, 0, sizeof str);
    	int top = 0;
    	while (x) str[++top] = (x & 1), x >>= 1;
    }
    
    void build(ll x, int num) {
    	getString(x);
    	int u = 1;
    	drp(i, base, 1) {
    		if (!trie[u][str[i]]) trie[u][str[i]] = ++tot;
    		u = trie[u][str[i]];
    	}
    	End[u] = num;
    }
    
    int query(ll x) {
    	getString(x);
    	int u = 1;
    	drp(i, base, 1)
    		if (trie[u][str[i] ^ 1]) u = trie[u][str[i] ^ 1];
    		else u = trie[u][str[i]];
    	return End[u];
    }
    
    int main() {
    	int T = read();
    	rep(_t, 1, T) {
    		tot = 1;
    		memset(trie, 0, sizeof trie);
    		memset(End, 0, sizeof End);
    		n = read(); int m = read();
    		rep(i, 1, n) a[i] = read(), build(a[i], i);
    		printf("Case #%d:
    ", _t);
    		while (m--) {
    			ll k = read();
    			write(a[query(k)]); puts("");
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    关于hibernate中多对多关系
    选择排序
    Offer是否具有法律效力?
    textarea文本域宽度和高度(width、height)自己主动适应变化处理
    序列相关的趣题 之三
    windows的定时任务设置
    Maven pom.xml 配置详解
    PS 图像特效-非线性滤波器
    PS 滤镜算法原理——拼贴
    PS 滤镜算法原理——曝光过度
  • 原文地址:https://www.cnblogs.com/aziint/p/8416194.html
Copyright © 2011-2022 走看看