zoukankan      html  css  js  c++  java
  • Xor Sum HDU

    #include<bits/stdc++.h>
    using namespace std ;
    const int N=10000000;
    int a[N],idx,son[N][2];
    //把每个数插入到trie树中 
    void insert(int x)
    {
        //从头开始插入 
        int p=0;
        for(int i=30;i>=0;i--)
        {
            //取出当前位 
            int u=x>>i&1;
            //如果之前不存在的话 
            if(!son[p][u])
                //创建    
                son[p][u]=++idx;
            //然后继续往下走 
            p=son[p][u];
        }
    }
    int query(int x)
    {
        int p=0,res=0;
        for(int i=30;i>=0;i--)
        {
            int u=x>>i&1;
            //如果存在不同的,就走不同的 
            if(son[p][!u])
            {
                res=res*2+!u;
                p=son[p][!u];
            }
            else
            {
                res=res*2+u;
                p=son[p][u];
            }
        }
        return res;
    }
    int main()
    {
        int t;
    	scanf("%d",&t);
    	int cnt=1;
    	while(t--)
    	{
    		idx=0;
    		memset(a,0,sizeof a);
    		for(int i=0;i<=N;i++)
    			son[i][0]=son[i][1]=0;
    		int n,m;
    		scanf("%d%d",&n,&m);
    		for(int i=1;i<=n;i++)
    		{
    			scanf("%d",&a[i]);
    			insert(a[i]);
    		}
    		printf("Case #%d:
    ",cnt);
    		cnt++;
    		while(m--)
    		{
    			int x;
    			scanf("%d",&x);
    			int t=query(x);
    			int res=x^t;
    			printf("%d
    ",t);
    		}
    	} 
        return 0;
    }
    
  • 相关阅读:
    并发编程
    网络与WEB 编程
    包和模块
    元编程
    类和对象
    【算法题 14 LeetCode 147 链表的插入排序】
    剑指offer面试54题
    剑指offer 面试51题
    剑指offer 面试3题
    剑指offer 面试52题
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12386074.html
Copyright © 2011-2022 走看看