C. Perfect Security

time limit per test3.5 seconds

memory limit per test512 megabytes

inputstandard input

outputstandard output

Alice has a very important message M consisting of some non-negative integers that she wants to keep secret from Eve. Alice knows that the only theoretically secure cipher is one-time pad. Alice generates a random key K of the length equal to the message’s length. Alice computes the bitwise xor of each element of the message and the key (, where denotes the bitwise XOR operation) and stores this encrypted message A. Alice is smart. Be like Alice.

For example, Alice may have wanted to store a message M = (0, 15, 9, 18). She generated a key K = (16, 7, 6, 3). The encrypted message is thus A = (16, 8, 15, 17).

Alice realised that she cannot store the key with the encrypted message. Alice sent her key K to Bob and deleted her own copy. Alice is smart. Really, be like Alice.

Bob realised that the encrypted message is only secure as long as the key is secret. Bob thus randomly permuted the key before storing it. Bob thinks that this way, even if Eve gets both the encrypted message and the key, she will not be able to read the message. Bob is not smart. Don’t be like Bob.

In the above example, Bob may have, for instance, selected a permutation (3, 4, 1, 2) and stored the permuted key P = (6, 3, 16, 7).

One year has passed and Alice wants to decrypt her message. Only now Bob has realised that this is impossible. As he has permuted the key randomly, the message is lost forever. Did we mention that Bob isn’t smart?

Bob wants to salvage at least some information from the message. Since he is not so smart, he asks for your help. You know the encrypted message A and the permuted key P. What is the lexicographically smallest message that could have resulted in the given encrypted text?

More precisely, for given A and P, find the lexicographically smallest message O, for which there exists a permutation π such that for every i.

Note that the sequence S is lexicographically smaller than the sequence T, if there is an index i such that Si < Ti and for all j < i the condition Sj = Tj holds.

Input

The first line contains a single integer N (1 ≤ N ≤ 300000), the length of the message.

The second line contains N integers A1, A2, …, AN (0 ≤ Ai < 230) representing the encrypted message.

The third line contains N integers P1, P2, …, PN (0 ≤ Pi < 230) representing the permuted encryption key.

Output

Output a single line with N integers, the lexicographically smallest possible message O. Note that all its elements should be non-negative.

Examples

inputCopy

3

8 4 13

17 2 7

output

10 3 28

inputCopy

5

12 7 87 22 11

18 39 9 12 16

output

0 14 69 6 44

inputCopy

10

331415699 278745619 998190004 423175621 42983144 166555524 843586353 802130100 337889448 685310951

226011312 266003835 342809544 504667531 529814910 684873393 817026985 844010788 993949858 1031395667

output

128965467 243912600 4281110 112029883 223689619 76924724 429589 119397893 613490433 362863284

Note

In the first case, the solution is (10, 3, 28), since , and . Other possible permutations of key yield messages (25, 6, 10), (25, 3, 15), (10, 21, 10), (15, 21, 15) and (15, 6, 28), which are all lexicographically larger than the solution.

上传一份推倒思路,我懒得打字了!

#include<stdio.h>
#include<algorithm>
using namespace std;
int a[300005];
typedef struct Trie
{
int sum;
Trie *next[2];
}Trie;
void Create(Trie *root, int x)
{
int now, i;
Trie *p = root;
for(i=30;i>=0;i--)
{
now = 0;
if(x&(1<<i))
now = 1;
if(p->next[now]==NULL)
{
Trie *temp = new Trie;
temp->next[0] = temp->next[1] = NULL;
temp->sum = 0;
p->next[now] = temp;
}
p = p->next[now];
p->sum++;
}
}
int Query(Trie *root, int x)
{
int now, i, ans = 0;
Trie *p = root;
for(i=30;i>=0;i--)
{
now = 0;
if(x&(1<<i))
now = 1;
if(p->next[now]==NULL || p->next[now]->sum==0)
p = p->next[now^1], ans += (1<<i);
else
p = p->next[now];
p->sum--;
}
return ans;
}
int main(void)
{
int n, i, x;
Trie *root = new Trie;
root->next[0] = root->next[1] = NULL;
root->sum = 0;
scanf("%d", &n);
for(i=1;i<=n;i++)
scanf("%d", &a[i]);
for(i=1;i<=n;i++)
{
scanf("%d", &x);
Create(root, x);
}
for(i=1;i<=n;i++)
printf("%d ", Query(root, a[i]));
puts("");
return 0;
}

CodeForces 923C Perfect Security的更多相关文章

  1. Codeforces 948D Perfect Security(字典树)

    题目链接:Perfect Security 题意:给出N个数代表密码,再给出N个数代表key.现在要将key组排序,使key组和密码组的亦或所形成的组字典序最小. 题解:要使密码组里面每个数都找到能使 ...

  2. Codeforces 948D Perfect Security

    Perfect Security 题意:给你一个A[i]数组, 再给你一个B[i]数组, 现在用选取 B[i] 数组中的一个 去和 A[i] 数组里的一个元素去进行异或操作, B[i]数组的元素只能用 ...

  3. Codeforces 948D Perfect Security 【01字典树】

    <题目链接> 题目大意: 给定两个长度为n的序列,可以改变第二个序列中数的顺序,使得两个序列相同位置的数异或之后得到的新序列的字典序最小. 解题分析: 用01字典树来解决异或最值问题.因为 ...

  4. 2018.12.08 codeforces 948D. Perfect Security(01trie)

    传送门 01trie板子题. 给出两个数列,允许把第二个数列重新排列. 求使得两个数列每个位置对应的数的异或值和成为最小值的每个位置的异或和. 把第二个数列插入到01trie里面然后对于第一个数列中的 ...

  5. 【CodeForces】947 C. Perfect Security 异或Trie

    [题目]C. Perfect Security [题意]给定长度为n的非负整数数组A和数组B,要求将数组B重排列使得A[i]^B[i]的字典序最小.n<=3*10^5,time=3.5s. [算 ...

  6. 01Trie树 CF923C Perfect Security

    CF923C Perfect Security 上下各n个数,求一种排列p,使上面的数i异或pi成为新的数i,求方案另字典序最小,输出该结果 01Trie树. 记录每个节点经过多少次. 每一次查询的时 ...

  7. Codeforces 923 C. Perfect Security

    http://codeforces.com/contest/923/problem/C Trie树 #include<cstdio> #include<iostream> us ...

  8. 923c C. Perfect Security

    Trie树. 要求字典序最小,所以由前到后贪心的选择.建一个trie树维护b数列. #include<cstdio> #include<algorithm> #include& ...

  9. codeforces 14A - Letter & codeforces 859B - Lazy Security Guard - [周赛水题]

    就像title说的,是昨天(2017/9/17)周赛的两道水题…… 题目链接:http://codeforces.com/problemset/problem/14/A time limit per ...

随机推荐

  1. C# Queue与RabbitMQ的爱恨情仇(文末附源码):Q与MQ消息队列简单应用(二)

    上一章我们讲了队列( Queue),这一章我们讲Message Queue消息队列,简称MQ. 定义: MQ是MessageQueue,消息队列的简称(是流行的开源消息队列系统,利用erlang语言开 ...

  2. Deepin-还原Windows平台

    首次启动! 是不是感觉很迷茫呢? 找不到存在感 先设置成Windows那种高校模式(右键下面任意区域) OK了吧,然后我们找到“启动器”或者按Windows键(在Deepin linux我们称为Sup ...

  3. hdoj 5093 Battle ships 【二分图最大匹配】

    题目:pid=5093" target="_blank">hdoj 5093 Battle ships 题意:给你一个n*m的图,图中有冰山 '# ',浮冰 'o' ...

  4. Web安全漏洞及攻击

    背景介绍 先说一个在互联网上常见,但是普通人又不太理解的东西--“验证码”. 验证码(CAPTCHA)是“Completely Automated Public Turing test to tell ...

  5. js中字符串的拼接的另一种方法

    // 按一定长度截断字符串,并使用 + 运算符进行连接. // 分隔字符串尽量按语义进行,如不要在一个完整的名词中间断开. // 特别的,对于HTML片段的拼接,通过缩进,保持和HTML相同的结构. ...

  6. Mac Chrome-点击书签页在新的标签打开之方法

    PS:一直使用的是Firefox,但是现在Firefox有些不能满足我现在的需求,所以下载了chrome.可是当使用chrome时发现有一个很实用的功能它不能设置,这个让我很抓狂. 当点击标签时不能新 ...

  7. 反射学习总结 --为理解SpringMVC底层做准备

    反射是什么? 通俗理解 - 照X光. java:一个类在反射面前就像照X光,清清楚楚明明白白. 应用:我们的ide中,能够"."一下就知道类中的所有方法就是通过反射实现的. XML ...

  8. 嵌入式linux 实现mdev SD卡和U盘自己主动挂载和卸载的方法 mdev.conf

    首先先參考这些博客做一些了解:http://linux.chinaunix.net/techdoc/install/2009/11/18/1144936.shtml http://www.cnblog ...

  9. Javascript 解析字符串生成 XML DOM 对象。

    Javascript 接收字符串生成 XML DOM 对象.实测对 Firefox .IE6 有效.可用于解析 ajax 的服务器响应结果,也可用于解析自定义字符串.​1. [代码]函数   ppt模 ...

  10. [开源下载] 【开源项目】EasySL for Silverlight 4

    [开源下载] [开源项目]EasySL for Silverlight 4  [复制链接]       silverlight 452 主题 7 好友 1万 积分 管理员 贡献 879 原创 0 银元 ...