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

    你可能感兴趣的文章
    Pandas 数据框:使用线性插值重新采样
    查看>>
    pandas 数据框将 INT64 列转换为布尔值
    查看>>
    pandas 数据框将列类型转换为字符串或分类
    查看>>
    pandas 数据框条件 .mean() 取决于特定列中的值
    查看>>
    pandas 数据框至海运分组条形图
    查看>>
    Pandas 数据透视表:列顺序和小计
    查看>>
    pandas 时序统计的高级用法!
    查看>>
    pandas 时间序列重新采样结束给定的一天
    查看>>
    pandas 根据不是常量的第三列的值将值从一列复制到另一列
    查看>>
    pandas 根据值从多列中的一列查找
    查看>>
    Pandas 根据布尔条件选择行和列
    查看>>
    pandas 滚动窗口 - datetime64[ns] 未实现
    查看>>
    pandas 版本兼容特定的蟒蛇和NumPy配置吗?
    查看>>
    pandas 生成excel多级表头
    查看>>
    Pandas 的 DataFrame 详解-ChatGPT4o作答
    查看>>
    pandas 读取excel数据,以字典形式输出
    查看>>
    Pandas 读取具有浮点值的 csv 文件会导致奇怪的舍入和小数位数
    查看>>
    pandas 适用,但仅适用于满足条件的行
    查看>>
    pandas 重新采样到每月的特定工作日
    查看>>
    pandas :如何删除以NaN为列名的多个列?
    查看>>