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;
    }
  • 相关阅读:
    spring加载bean实例化顺序
    Java生成CSV文件实例详解
    JSch
    socket(一)
    Core Data
    运行时c函数
    ReactiveCocoa(RAC)
    先来个xmpp学习连接
    FMDB
    NSKeyedArchive(存储自定义对象)
  • 原文地址:https://www.cnblogs.com/eason9906/p/11754828.html
Copyright © 2011-2022 走看看