zoukankan      html  css  js  c++  java
  • hdu4825(01trie)

    模板题

    题意就是给一个集合s,一个数,在集合里找到与该数异或值最大的数是多少

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1e5+5;
    struct node
    {
        int son[2],val;
        void reset()
        {
            son[0]=son[1]=val=0;
        }
    }tree[maxn*32];
    int cnt;
    void myinsert(int x)
    {
        int r=0;
        for(int i=31;i>=0;i--)
        {
            int v=(x>>i)&1;
            if(!tree[r].son[v])
            {
                tree[++cnt].reset();
                tree[r].son[v]=cnt;
            }
            r=tree[r].son[v];
        }
        tree[r].val=x;
    
    }
    int query(int x)
    {
        int r=0,v;
        for(int i=31;i>=0;i--)
        {
            v=(x>>i)&1;
            if(tree[r].son[v^1])
                r=tree[r].son[v^1];
            else
                r=tree[r].son[v];
        }
        return tree[r].val;
    }
    int main()
    {
        int t,icase=0,n,m,x;
        scanf("%d",&t);
        while(t--)
        {
            cnt=0;
            tree[0].reset();
            scanf("%d%d",&n,&m);
            for(int i=1;i<=n;i++) scanf("%d",&x),myinsert(x);
            printf("Case #%d:
    ",++icase);
            while(m--)
            {
                scanf("%d",&x);
                printf("%d
    ",query(x));
            }
        }
        return 0;
    }
  • 相关阅读:
    模拟展示动态按钮
    模拟界面请求到web服务器
    bean的生命周期
    structs2的action实现方式
    annotation中的Autowired
    华为笔试题练习
    开发工具
    [转]Linux双向链表的知识
    【转】 嵌入式C语言编程中Inline函数的应用
    打印格式化printf
  • 原文地址:https://www.cnblogs.com/eason9906/p/11754828.html
Copyright © 2011-2022 走看看