一个字典树与异或运算的集合。当时写超时了,不知道为什么。。。先记录下来
题目:
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异或值最大。
样例输入
2
3 2
3 4 5
1
5
4 1
4 6 5 6
3
样例输出
Case #1:
4
3
Case #2:
4
#include<iostream>
#include<queue>
#include<cmath>
#include<string.h>
using namespace std;
#define ll long long
const int maxn = 1e5+5;
const int inf = 0x3f3f3f3f;
int pre[maxn][40];
int arr[maxn];
int len;
int tree[maxn*100][2];
int ans[50];
void init(){
ans[0] = 1;
for(int i = 1; i <= 32; i++) ans[i] = ans[i-1]*2;
}
void fun(int x){
queue<int>que;
int f = arr[x];
while(f){
que.push(f%2);
f /= 2;
}
int k = len;
while(!que.empty()){
pre[x][k--] = que.front();
que.pop();
}
}
int rt = 1;
void build(int x){
int u = 0, v;
for(int i = 1; i <= len; i++){
v = pre[x][i];
if (!tree[u][v]) tree[u][v] = rt++;
u = tree[u][v];
}
}
int pp[50];
int an;
void query(){
int u = 0, v;
an = 0;
for(int i = 1; i <= len; i++){
v = pp[i];
if (!tree[u][v]) v ^= 1;
u = tree[u][v];
an += v*ans[len-i];
}
}
int main() {
int t, x;
int kase = 1;
cin >> t;
init();
while(t--){
int n, m;
cin >> n >> m;
int ma = 0;
for(int i = 1; i <= n; i++){
scanf("%d", &arr[i]);
ma = max(ma, arr[i]);
}
len = floor(log(ma)/log(2))+1;
memset(pre, 0, sizeof(pre));
memset(tree, 0, sizeof(tree));
rt = 1;
for(int i = 1; i <= n; i++){
fun(i);
build(i);
}
printf("Case #%d:\n", kase++);
while(m--){
scanf("%d", &x);
memset(pp, 0, sizeof(pp));
int lenth = floor(log(x)/log(2)) + 1;
queue<int>que;
int f = x;
while(f) {
que.push(f%2);
f /= 2;
}
if (lenth >= len){
for(int i = len; i >= 1; i--){pp[i] = que.front(); que.pop();}
}
else {
for(int i = len; i >= len-lenth+1; i--) {pp[i]=que.front(); que.pop();}
}
for(int i = 1; i <= len; i++) pp[i] ^= 1;
query();
printf("%d\n", an);
}
}
return 0;}
超时代码
#include <iostream>
using namespace std;
#define MAX 2
typedef struct trie{ //字典树
int v;//直到最后一个才存储位置,之前都存储为0
trie *next[MAX];
}trie;
trie *root = new trie();
void creatTrie(int *str,int m) //建立字典树的过程,
{
trie *p;
trie *c_root=root;
for(int i=0;i<32;i++)
{
int id=str[i];
if (c_root->next[id]==NULL)
{
p=new trie;
if(i==31){
p->v=m;
}
else{
p->v=0;
}
for (int j=0;j<MAX;j++)
{
p->next[j]=NULL;
}
c_root->next[id]=p;
c_root=c_root->next[id];
}
else
{
c_root=c_root->next[id];
}
}
}
void Binarycout(int m[],int n) //求32位的二进制,并存储在一个数组里
{
for (int i=31,j=0;i>=0,j<=31;i--,j++)
{
m[j]=((n>>i)&1);
}
}
int findTrielocation(int *str) //
{
trie *f_root=root;
for(int i=0;i<32;i++)
{
int id=str[i];
if (id==0){
if (f_root->next[1]!=NULL)
{
f_root=f_root->next[1];
}
else{
f_root=f_root->next[0];
}
}
else{
if (f_root->next[0]!=NULL)
{
f_root=f_root->next[0];
}
else{
f_root=f_root->next[1];
}
}
}
return f_root->v;
}
int main()
{
int t,m,n,str[32],a[100000];
for (int p=0;p<MAX;p++)
{
root->next[p]=NULL;
}
cin>>t;
int flag=1;
while(t--){
cin>>n>>m;
for(int i=0;i<n;i++){
int x;
cin>>x;
Binarycout(str,x);
creatTrie(str,x);
}
for (int j=0;j<m;j++){
cin>>a[j];
}
for (int j=0;j<m;j++)
{
Binarycout(str,a[j]);
a[j]=findTrielocation(str);
}
cout<<"Case #"<<flag++<<":"<<endl;
for(int i=0;i<m;i++)
cout<<a[i]<<endl;
}
return 0;
}
这里有一个比较知识点,将一个十进制数转化为二进制数存进数组里
void Binarycout(int m[],int n) //求32位的二进制,并存储在一个数组里
{
for (int i=31,j=0;i>=0,j<=31;i--,j++)
{
m[j]=((n>>i)&1);
}
}
有时间在研究一下!