hdu4825---Xor Sum(Trie + 貪心)

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

Source
2014年百度之星程序設計大賽 - 資格賽

Recommend
liuyiding | We have carefully selected several similar problems for you: 5235 5234 5233 5232 5231

把n個數化成二進制,然後建立Trie樹
接下來貪心遍歷這棵樹就行

/*************************************************************************
    > File Name: hdu4825.cpp
    > Author: ALex
    > Mail: [email protected] 
    > Created Time: 2015年05月25日 星期一 14時36分41秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

int bit[40];
static const int MaxNode = 3200010;
class Trie {
    public:
        void init();
        int newnode();
        void insert(int bit[], int w);
        int getNum(int bit[], int w);
    private:
        int next[MaxNode][2];
        int end[MaxNode];
        int L, root;
}trie;

int Trie :: newnode() {
    next[L][0] = next[L][1] = -1;
    ++L;
    return L - 1;
}

void Trie :: init() {
    L = 0;
    root = newnode();
}

void Trie :: insert(int bit[], int w) {
    int now = root;
    for (int i = 31; i >= 0; --i) {
        if (next[now][bit[i]] == -1) {
            next[now][bit[i]] = newnode();
        }
        now = next[now][bit[i]];
    }
    end[now] = w;
}

int Trie :: getNum(int bit[], int w) {
    int now = root;
    for (int i = 31; i >= 0; --i) {
        if (next[now][bit[i] ^ 1] == -1) {
            now = next[now][bit[i]];
        }
        else {
            now = next[now][bit[i] ^ 1];
        }
    }
    return end[now];
};

void calc(int num) {
    int cnt = 0;
    while (num) {
        bit[cnt++] = num % 2;
        num >>= 1;
    }
    for (int i = cnt; i <= 31; ++i) {
        bit[i] = 0;
    }   
}

int main() {
    int t, icase = 1;
    scanf("%d", &t);
    while (t--) {
        int n, m, w;
        scanf("%d%d", &n, &m);
        trie.init();
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &w);
            calc(w);
            trie.insert(bit, w);
        }
        printf("Case #%d:\n", icase++);
        for (int i = 1; i <= m; ++i) {
            scanf("%d", &w);
            calc(w);
            printf("%d\n", trie.getNum(bit, w));
        }
    }
    return 0;
}
發佈了700 篇原創文章 · 獲贊 5 · 訪問量 50萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章