zoukankan      html  css  js  c++  java
  • 【Trie】The XOR Largest Pair

    【题目链接】

    https://loj.ac/problem/10050

    【题意】

    给出n个数,其中取出两个数来,让其异或值最大。

    【题解】

    经典的01字典树问题。

    首先需要把01字典树建出来。

    然后对于每一个串都跑一遍。如果存在当前位 不同的 节点,就往那里跑,否则顺着跑。

    一切尽在代码中。

    【代码】

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 const int N = 1e5+10;
     5 const int M = 3e6+10;
     6 int son[M][2];
     7 int a[N],n,idx;
     8 void Insert(int x){
     9     int p = 0 ;
    10     for(int i=31;~i;i--){
    11         int t = x >> i & 1 ;
    12         if( !son[p][t] )
    13             son[p][t] = ++idx;
    14         p = son[p][t] ;
    15     }
    16 }
    17 int Query(int x){
    18     int p = 0 ;
    19     int res = 0 ;
    20     for(int i=31;~i;i--){
    21         int t = x >> i & 1 ;
    22         if( son[p][t^1] ){
    23             res += 1<<i ;
    24             p = son[p][t^1] ;
    25         }else{
    26             p = son[p][t] ;
    27         }
    28     }
    29     return res ;
    30 }
    31 int main()
    32 {
    33     scanf("%d",&n);
    34     for(int i=1;i<=n;i++)
    35         scanf("%d",&a[i]),Insert(a[i]);
    36     int res = 0 ;
    37     for(int i=1;i<=n;i++)
    38         res = max( Query(a[i]) , res );
    39     printf("%d
    ",res);
    40     return 0 ;
    41 }
    01字典树
  • 相关阅读:
    图解CSS3----1-关系选择器
    HTML5----热区(在图片img上第一超链接选区)
    Javascript----练习二(运算符)
    Javascript----练习一(变量)
    PHP表单
    maven
    Docker
    PHP字符串
    面向对象思想的核心概念
    虚方法
  • 原文地址:https://www.cnblogs.com/Osea/p/11361488.html
Copyright © 2011-2022 走看看