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);
                }
            }
        }
    }
    

      

  • 相关阅读:
    MySQL 子查询
    mysql3
    mysql2
    mysql
    C语言理论知识
    冒泡排序
    猜数字游戏
    WPF清爽酷炫的界面Mahapps.metro
    如何在.net4.0中使用.net4.5的async/await
    在C#中使用官方驱动操作MongoDB
  • 原文地址:https://www.cnblogs.com/huangdao/p/9525631.html
Copyright © 2011-2022 走看看