博客
关于我
AcWing 143. 最大异或对
阅读量:346 次
发布时间:2019-03-04

本文共 2069 字,大约阅读时间需要 6 分钟。

????????????????????????????????????????????????????????????????????????????????

????

????????????Trie????????????Trie?????????????????????????1????????????????????????????????Trie??????????????????

???????

  • ???Trie???
  • ???????????Trie????????????????????
  • ??????????Trie??????????????????????????????
  • ???????????
  • ????

    import java.io.*;import java.lang.Integer;class Main {    static int N = 3000010; // ??N???1000000    static int[][] t = new int[N][2];    static int idx = 0;    static void insert(int num) {        int p = 0;        for (int x = 30; x >= 0; --x) {            int c = (num >> x) & 1;            if (t[p][c] == 0) {                t[p][c] = ++idx;            }            p = t[p][c];        }    }    static int compare(int num) {        int p = 0;        int max = 0;        for (int x = 30; x >= 0; --x) {            int c = (num >> x) & 1;            if (t[p][1 - c] == 0) {                max = (max << 1) | 1;                p = t[p][c];            } else {                max = (max << 1) | 0;                p = t[p][c];            }        }        return max;    }    public static void main(String[] args) throws Exception {        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));        BufferedWriter buw = new BufferedWriter(new OutputStreamWriter(System.out));        int n = Integer.valueOf(buf.readLine());        String[] params = buf.readLine().split(" ");        for (int i = 0; i < n; ++i) {            int num = Integer.valueOf(params[i]);            insert(num);        }        int max = 0;        for (int i = 0; i < n; ++i) {            int num = Integer.valueOf(params[i]);            int m = compare(num);            if (m > max) {                max = m;            }        }        buw.write(max + "");        buw.flush();        buf.close();        buw.close();    }}

    ????

  • Trie???????????t???Trie??????????????????????0?1?
  • ????insert?????????Trie?????????????????????????????
  • ????compare???????Trie?????????????????????
  • ???main????????????Trie??????????????????
  • ????????????O(N)??????????????????

    转载地址:http://rkre.baihongyu.com/

    你可能感兴趣的文章
    Oracle 11g超详细安装步骤
    查看>>
    Oracle 12c中的MGMTDB
    查看>>
    Oracle 12c安装报错Installation failed to access the temporary location(无法访问临时位置)...
    查看>>
    Oracle 9i数据库管理教程
    查看>>
    ORACLE Active dataguard 一个latch: row cache objects BUG
    查看>>
    oracle avg、count、max、min、sum、having、any、all、nvl的用法
    查看>>
    Oracle BEQ方式连接配置
    查看>>
    oracle Blob保存方式,oracle 存储过程操作blob
    查看>>
    Oracle BMW Racing sailing vessel帆船图
    查看>>
    ORACLE Bug 4431215 引发的血案—原因分析篇
    查看>>
    Oracle Business Intelligence Downloads
    查看>>
    Oracle cmd乱码
    查看>>
    Oracle Corp甲骨文公司推出Oracle NoSQL数据库2.0版
    查看>>
    【Docker知识】将环境变量传递到容器
    查看>>
    uniapp超全user-agent判断 包括微信开发工具 hbuilder mac windows 安卓ios端及本地识别
    查看>>
    Oracle DBA课程系列笔记(20)
    查看>>
    oracle dblink 创建使用 垮库转移数据
    查看>>
    oracle dblink结合同义词的用法 PLS-00352:无法访问另一数据库
    查看>>
    Oracle dbms_job.submit参数错误导致问题(ora-12011 无法执行1作业)
    查看>>
    oracle dg switchover,DG Switchover fails
    查看>>