hdu-4825(01字典树)
Xor Sum
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 4198????Accepted Submission(s): 1845
?
Problem Description
Zeus 和 Prometheus 做了一個(gè)游戲,Prometheus 給 Zeus 一個(gè)集合,集合中包含了N個(gè)正整數(shù),隨后 Prometheus 將向 Zeus 發(fā)起M次詢(xún)問(wèn),每次詢(xún)問(wèn)中包含一個(gè)正整數(shù) S ,之后 Zeus 需要在集合當(dāng)中找出一個(gè)正整數(shù) K ,使得 K 與 S 的異或結(jié)果最大。Prometheus 為了讓 Zeus 看到人類(lèi)的偉大,隨即同意 Zeus 可以向人類(lèi)求助。你能證明人類(lèi)的智慧么?
?
?
Input
輸入包含若干組測(cè)試數(shù)據(jù),每組測(cè)試數(shù)據(jù)包含若干行。
輸入的第一行是一個(gè)整數(shù)T(T < 10),表示共有T組數(shù)據(jù)。
每組數(shù)據(jù)的第一行輸入兩個(gè)正整數(shù)N,M(<1=N,M<=100000),接下來(lái)一行,包含N個(gè)正整數(shù),代表 Zeus 的獲得的集合,之后M行,每行一個(gè)正整數(shù)S,代表 Prometheus 詢(xún)問(wèn)的正整數(shù)。所有正整數(shù)均不超過(guò)2^32。
?
?
Output
對(duì)于每組數(shù)據(jù),首先需要輸出單獨(dú)一行”Case #?:”,其中問(wèn)號(hào)處應(yīng)填入當(dāng)前的數(shù)據(jù)組數(shù),組數(shù)從1開(kāi)始計(jì)算。
對(duì)于每個(gè)詢(xún)問(wèn),輸出一個(gè)正整數(shù)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字典樹(shù)?
#include<bits/stdc++.h> using namespace std; #define MAXN 100005 #define ll long long int n,m; int trie[32*MAXN][2]; ll val[32*MAXN]; int tot; void insert(ll d) {int root=0;for(int i=32;i>=0;i--){int id=(d>>i)&1;//獲得這一個(gè)bit位的值if(!trie[root][id]) trie[root][id]=++tot;root=trie[root][id];}val[root]=d; } ll query(ll d) {int root=0;for(int i=32;i>=0;i--){int id=(d>>i)&1;if(trie[root][id^1]) root=trie[root][id^1];else root=trie[root][id];}return val[root]; } int main() {int cas;scanf("%d",&cas);for(int tt=1;tt<=cas;tt++){scanf("%d%d",&n,&m);memset(trie,0,sizeof trie);for(int i=0;i<n;i++){int d;scanf("%d",&d);insert(d);}printf("Case #%d:\n",tt);for(int i=0;i<m;i++){ll d;scanf("%lld",&d);printf("%lld\n",query(d));}}return 0; }?
總結(jié)
以上是生活随笔為你收集整理的hdu-4825(01字典树)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux免密登录(ssh命令)
- 下一篇: MySql通过Limit限制查询的行数