HDU 4825+ dictionary tree

Xor Sum

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


Problem Description
Zeus and Prometheus played a game, Prometheus gave Zeus a set, the set contains N positive integers, and then Prometheus will send M times to Zeus, each query contains a positive integer S, then Zeus needs to find in the set. Get a positive integer K that maximizes the XOR result of K and S. In order to let Zeus see the greatness of mankind, Prometheus immediately agreed that Zeus could turn to mankind for help. Can you prove human wisdom?
 

Input
The input contains sets of test data, each set of test data contains several rows.
The first line of input is an integer T (T < 10), representing a total of T groups of data.
The first line of each set of data inputs two positive integers N, M (<1=N, M<=100000), the next line contains N positive integers, representing Zeus's obtained set, and then M lines, each line A positive integer S, representing the positive integer that Prometheus is asking. All positive integers do not exceed 2^32.
 

Output
For each group of data, first need to output a separate line of "Case #?:", where the question mark should be filled with the current number of data groups, and the number of groups starts from 1.
For each query, output a positive integer K that maximizes the XOR value of K and S.
 

Sample Input
 
  
23 23 4 5154 14 6 5 63
 

Sample Output
 
  
Case #1:43Case #2:4

//Key sentence: To find a positive integer K in the set, so that the XOR result of K and S is the largest
#include <cstdio>
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
#include <set>
#include <string>
#include <functional>
#include <vector>
#include <map>
#include <cmath>
using namespace std;
const int MAX = 2;
struct Trie {
    int num;
    bool flag;
    Trie* next[MAX];
    Trie() {
        num = 1;
        flag = false;
        memset(next, NULL, sizeof(next));
    }
};
Trie* NewTrie() {
    Trie* temp = new Trie;
    return temp;
}
void Insert(Trie* root, int S) {
    Trie* p = root;
    for (int i = 31; i >= 0; i--) {
        int id = ((S & (1 << i)) != 0);
        if (p->next[id] == NULL) {
            p->next[id] = NewTrie();
        }
        p = p->next[id];
    }
    p->flag = true;
}
int trie_query(Trie* root, int S) {
    Trie* p = root;
    int years = 0;
    for (int i = 31; i >= 0; i--) {
        int id = ((S & (1 << i)) != 0);
        if (p->next[id ^ 1] != NULL) {
            ans |= (id ^ 1) << i;
            p = p->next[id ^ 1];
        } else {
            ans |= id << i;
            p = p->next[id];
        }
    }
    return ans;
}

int main() {

    int T, n, Q, t, ansk = 0;
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d", &n, &Q);
        Trie* root = NewTrie();
        for (int i = 1; i <= n; i++){
            scanf("%d", &t);
            Insert(root, t);
        }
        printf("Case #%d:\n", ++ansk);
        while (Q--) {
            scanf("%d", &t);
            printf("%d\n", trie_query(root, t));
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325597562&siteId=291194637