https://www.acwing.com/problem/content/145

#include <iostream>
#include <algorithm>
using namespace std; const int N = ;
int n;
int a[N]; class TrieNode {
public:
TrieNode() {};
TrieNode* children[];
}; class Trie {
public:
Trie() {
root = new TrieNode;
} void insert(int x) {
TrieNode* cur = root;
for (int i = ; i >= ; i--) {
int c = x >> i & ;
if (!cur->children[c]) {
cur->children[c] = new TrieNode;
}
cur = cur->children[c];
}
} int search(int x) {
TrieNode* cur = root; int res = ;
for (int i = ; i >= ; i--) {
int c = x >> i & ;
if (cur->children[!c]) {
res += << i;
cur = cur->children[!c];
} else if (cur->children[c]) {
cur = cur->children[c];
} else {
break;
}
}
return res;
}
private:
TrieNode* root;
}; int run() {
Trie trie;
int res = ;
for (int i = ; i < n; i++) {
trie.insert(a[i]);
res = max(res, trie.search(a[i]));
} return res;
} int main()
{
cin >> n;
for (int i = ; i < n; i++) {
cin >> a[i];
} cout << run() << endl;
return ;
}

AcWing 143. 最大异或对的更多相关文章

  1. AcWing 143. 最大异或对 01字典树打卡

    在给定的N个整数A1,A2……ANA1,A2……AN中选出两个进行xor(异或)运算,得到的结果最大是多少? 输入格式 第一行输入一个整数N. 第二行输入N个整数A1A1-ANAN. 输出格式 输出一 ...

  2. AcWing:143. 最大异或对(01字典树 + 位运算 + 异或性质)

    在给定的N个整数A1,A2……ANA1,A2……AN中选出两个进行xor(异或)运算,得到的结果最大是多少? 输入格式 第一行输入一个整数N. 第二行输入N个整数A1A1-ANAN. 输出格式 输出一 ...

  3. 143. 最大异或对(Trie树存整数+二进制)

    在给定的N个整数A1,A2……ANA1,A2……AN中选出两个进行xor(异或)运算,得到的结果最大是多少? 输入格式 第一行输入一个整数N. 第二行输入N个整数A1A1-ANAN. 输出格式 输出一 ...

  4. 《算法竞赛进阶指南》1.6Trie

    142. 前缀统计 给定N个字符串S1,S2-SN,接下来进行M次询问,每次询问给定一个字符串T,求S1-SN中有多少个字符串是T的前缀. 输入字符串的总长度不超过106,仅包含小写字母. 输入格式 ...

  5. AcWing 228. 异或 (dfs+线性基)打卡

    题目:https://www.acwing.com/problem/content/230/ 题意:有一个图,每条边有一个权值,现在求1-n的一条路径的最大异或和,一条边能经过多次,相应的也要计算那么 ...

  6. AcWing:144. 最长异或值路径(dfs + 01字典树)

    给定一个树,树上的边都具有权值. 树中一条路径的异或长度被定义为路径上所有边的权值的异或和: ⊕ 为异或符号. 给定上述的具有n个节点的树,你能找到异或长度最大的路径吗? 输入格式 第一行包含整数n, ...

  7. AcWing 144. 最长异或值路径 01字典树打卡

    给定一个树,树上的边都具有权值. 树中一条路径的异或长度被定义为路径上所有边的权值的异或和: ⊕ 为异或符号. 给定上述的具有n个节点的树,你能找到异或长度最大的路径吗? 输入格式 第一行包含整数n, ...

  8. AcWing:239. 奇偶游戏(前缀和 + 离散化 + 带权并查集 + 异或性质 or 扩展域并查集 + 离散化)

    小A和小B在玩一个游戏. 首先,小A写了一个由0和1组成的序列S,长度为N. 然后,小B向小A提出了M个问题. 在每个问题中,小B指定两个数 l 和 r,小A回答 S[l~r] 中有奇数个1还是偶数个 ...

  9. RMAN异机恢复遭遇ORA-01547、ORA-01152、ORA-01110错误案例

    测试环境:     操作系统  :  Red Hat Enterprise Linux ES release 4 (Nahant Update 4)   VMWARE     数据库     :  O ...

随机推荐

  1. Firemonkey Button 颜色

    delphi FMX Firemonkey Button 按钮 颜色 TintColor 颜色 Button1.TintColor:=TAlphaColorRec.Green;

  2. tensorflow 基本函数(1.tf.split, 2.tf.concat,3.tf.squeeze, 4.tf.less_equal, 5.tf.where, 6.tf.gather, 7.tf.cast, 8.tf.expand_dims, 9.tf.argmax, 10.tf.reshape, 11.tf.stack, 12tf.less, 13.tf.boolean_mask

    1.  tf.split(3, group, input)  # 拆分函数    3 表示的是在第三个维度上, group表示拆分的次数, input 表示输入的值 import tensorflow ...

  3. 解决pip安装时的Readtime out问题

    方法一 pip --default-timeout=100 install -U Pillow就可以了方法二 pip install pyinstaller -i https://pypi.douba ...

  4. Xcode 中设置部分文件ARC支持

    ARC是什么 ARC是iOS 5推出的新功能,全称叫 ARC(Automatic Reference Counting).简单地说,就是代码中自动加入了retain/release,原先需要手动添加的 ...

  5. web api的新玩法

    前言: 目前大多数的.net core 项目的web api 都是用的json作为数据传输格式,或者说几乎是所有的都是,可是有没有想过换一种数据传输格式怎么处理,比如XML,或者谷歌首推的Protob ...

  6. Visual Studio C++ include与library

    首先介绍几种目录: 1. 系统路径 系统路径在vc中是"Properties->Configuration Properties -> VC++ Directories" ...

  7. jQuery的event事件

    1.冒泡和默认行为 <div class="aa"> <div class="bb"> <div class="cc&q ...

  8. 7-n!的位数(斯特灵公式)

    http://acm.hdu.edu.cn/showproblem.php?pid=1018 Big NumberTime Limit: 2000/1000 MS (Java/Others) Memo ...

  9. SVG与HTML、JavaScript的三种调用方式

    一.在HTMl中访问SVG的DOM Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHig ...

  10. W-D-S-Nandflash

    1.6410的硬件一上电就会把nandflash前8K的内容拷贝到6410片内内存来,从片内内存0地址开始运行,如果烧写到nandflash里面的程序大于8k,则拷贝到6410片内的8k程序要把nan ...