zoukankan      html  css  js  c++  java
  • Chip Factory(0/1字典树)

    Chip Factory

     

     AC_Code:

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <cmath>
     5 #include <algorithm>
     6 using namespace std;
     7 typedef long long ll;
     8 const int maxn=1010;
     9 
    10 int trie[maxn*35][2],val[maxn*35],num[maxn*35],tot;
    11 ll s[maxn];
    12 ll ans;
    13 
    14 void init(){
    15     tot=0;
    16     memset(trie,0,sizeof(trie));
    17     memset(val,0,sizeof(val));
    18     memset(num,0,sizeof(num));
    19     ans=-1;
    20 }
    21 
    22 void _insert(ll x){
    23     int root=0;
    24     for(int i=32;i>=0;i--){
    25         int id=(x>>i)&1;
    26         if( !trie[root][id] ) trie[root][id]=++tot;
    27         root=trie[root][id];
    28         val[root]++;
    29     }
    30     num[root]=x;
    31 }
    32 
    33 void del(ll x){
    34     int root=0;
    35     for(int i=32;i>=0;i--){//注意long long才能从32开始,用int的话要从31,不然就WA了
    36         int id=(x>>i)&1;
    37         root=trie[root][id];
    38         val[root]--;
    39     }
    40 }
    41 
    42 ll _find(ll x)
    43 {
    44     int root = 0;
    45     for(int i=32;i>=0;i--){
    46         int id = (x>>i)&1;
    47         if(trie[root][!id]&&val[trie[root][!id]]) root = trie[root][!id];
    48         else root = trie[root][id];
    49     }
    50     return x^num[root];
    51 }
    52 
    53 int main()
    54 {
    55     int t,n;
    56     scanf("%d",&t);
    57     while( t-- ){
    58         init();
    59         scanf("%d",&n);
    60         for(int i=0;i<n;i++){
    61             scanf("%lld",&s[i]);
    62             _insert(s[i]);
    63         }
    64 
    65         for(int i=0;i<n;i++){
    66             del(s[i]);
    67             for(int j=i+1;j<n;j++){
    68                 del(s[j]);
    69                 ans=max(ans,_find(s[j]+s[i]));
    70                 _insert(s[j]);
    71             }
    72             _insert(s[i]);
    73         }
    74         printf("%lld
    ",ans);
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    05-----数据类型转换
    04-----赋值运算符
    03-----数据类型
    02-----第一个JavaScript代码
    Fxx and game
    Bomb
    Stammering Aliens
    DISUBSTR
    Life Forms
    后缀数组二·重复旋律2
  • 原文地址:https://www.cnblogs.com/wsy107316/p/12305735.html
Copyright © 2011-2022 走看看