模板:tire 复杂度:O(nlogn)
143. 最大异或对
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100010, M = 31 * maxn;
int ch[maxn];
int n;
int son[M][2], idx = 0;
void insert(int x)
{int p = 0;for (int i = 30; ~i; i--){int u = x >> i & 1;if (!son[p][u])son[p][u] = ++idx;//如果没有就创建p = son[p][u];}
}
int search(int x)
{int p = 0;int ret = 0;for (int i=30;~i;i--){int u = x>>i&1;//二进制上某位是0还是1 if (!son[p][!u])//如果他的儿子没有不一样的{p = son[p][u];ret = ret*2+u;}else//有不一样的{p = son[p][!u];ret = ret*2+!u;}}ret = ret^x;return ret;
}
int main()
{int n;cin >> n;for (int i = 0; i < n; i++){cin >> ch[i];insert(ch[i]);}int res = 0;for (int i = 0; i < n; i++){res = max(res, search(ch[i]));}cout << res;
}