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;
    }
  • 相关阅读:
    SDWebImage内部实现过程
    物理仿真元素
    物理仿真元素
    运行时案例
    使用运行时交换我们自定义的方法
    运行时交换系统方法
    HTML 钟表 小时钟
    JS小游戏寻找房祖名
    程序启动的完整过程
    ApplicationDelegate里的方法
  • 原文地址:https://www.cnblogs.com/eason9906/p/11754827.html
Copyright © 2011-2022 走看看