Perfect Security
Perfect Security
https://ac.nowcoder.com/acm/problem/112567
Perfect Security
思路
只要把P数组加入树就好了,然后通过这颗树对A中的每一个数,去查找一个与之异或最小的值即可,
同时还要支持删除操作,直接加入一个数组,记录当前是否还有数字经过这条边即可。
注意在查询的过程中要不断的减小数组的标记。
代码
/*
Author : lifehappy
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 10;
int trie[N][2], num[N], a[N], tot;
void insert(int x) {
int rt = 0;
for(int i = 30; i >= 0; i--) {
int now = x >> i & 1;
if(!trie[rt][now]) trie[rt][now] = ++tot;
rt = trie[rt][now];
num[rt]++;
}
}
int query(int x) {
int ans = 0, rt = 0;
for(int i = 30; i >= 0; i--) {
int now = x >> i & 1;
if(num[trie[rt][now]]) {
num[trie[rt][now]]--;
rt = trie[rt][now];
}
else {
num[trie[rt][now ^ 1]]--;
rt = trie[rt][now ^ 1];
ans |= 1 << i;
}
}
return ans;
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for(int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
insert(x);
}
for(int i = 1; i <= n; i++) {
printf("%d%c", query(a[i]), i == n ? '\n' : ' ');
}
return 0;
}
海康威视公司福利 1325人发布