zoukankan      html  css  js  c++  java
  • hdu-4825(01字典树)

    题意:中文题意

    解题思路:01字典树板子题

    代码:

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #define maxn 100050
    #define ll long long
    using namespace std;
    int trie[maxn*32][2];
    ll val[maxn*32];
    ll x,y;
    int root;
    int tot;
    int n,t,m;
    void init()
    {
        memset(trie,0,sizeof(trie));
        root=0;tot=0;
    }
    void get_trie(ll u)
    {
        int root=0;
        for(int i=32;i>=0;i--)
        {
            int id=(u>>i)&1;
            if(!trie[root][id])
                trie[root][id]=++tot;
            root=trie[root][id];
        }
        val[root]=u;
    }
    ll query(ll u)
    {
        int root=0;
        for(int i=32;i>=0;i--)
        {
            int id=(u>>i)&1;
            if(trie[root][id^1])
                root=trie[root][id^1];
            else
                root=trie[root][id];
        }
        return val[root];
    }
    int main()
    {
        while(scanf("%d",&t)!=EOF)
        {
            int tt=0;
            while(t--)
            {
                init();
                tt++;
                scanf("%d%d",&n,&m);
                for(int i=1;i<=n;i++)
                {
                    scanf("%lld",&x);
                    get_trie(x);
                }
                printf("Case #%d:
    ",tt);
                while(m--)
                {
                    scanf("%lld",&y);
                    ll ans=query(y);
                    printf("%lld
    ",ans);
                }
            }
        }
    }
    

      

  • 相关阅读:
    前端-JavaScript
    前端-HTML
    Python源程序(.py)转换为可执行文件(.exe)
    进程
    算法之动态规划问题
    算法之斐波那契数列
    贪心算法找零问题
    算法之迷宫问题
    数据结构相关知识
    常用排序算法
  • 原文地址:https://www.cnblogs.com/huangdao/p/9525631.html
Copyright © 2011-2022 走看看