博客
关于我
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/

    你可能感兴趣的文章
    OneBlog Shiro 反序列化漏洞复现
    查看>>
    one_day_one--mkdir
    查看>>
    ONI文件生成与读取
    查看>>
    onlyoffice新版5.1.2版解决中文汉字输入重复等问题
    查看>>
    oobbs开发手记
    查看>>
    OPEN CASCADE Curve Continuity
    查看>>
    Open Graph Protocol(开放内容协议)
    查看>>
    Open vSwitch实验常用命令
    查看>>
    Open WebUI 忘了登入密码怎么办?
    查看>>
    open-vm-tools-dkms : 依赖: open-vm-tools (>= 2:9.4.0-1280544-5ubuntu3) 但是它将不会被安装
    查看>>
    open3d-Dll缺失,未找到指定模块解决
    查看>>
    Openbox-桌面图标设置
    查看>>
    opencart出现no such file or dictionary
    查看>>
    opencv Mat push_back
    查看>>
    opencv videocapture读取视频cap.isOpened 输出总是false
    查看>>
    opencv waitKey() 函数理解及应用
    查看>>
    OpenCV 中的图像转换
    查看>>
    OpenCV 在 Linux 上的 python 与 anaconda 无法正常工作.收到未实现 cv2.imshow() 的错误
    查看>>
    Opencv 完美配置攻略 2014 (Win8.1 + Opencv 2.4.8 + VS 2013)上
    查看>>
    opencv 模板匹配, 已解决模板过大程序不工作的bug
    查看>>