hdu 4825
来源:互联网 发布:知识产权 淘宝 编辑:程序博客网 时间:2024/04/26 01:24
//我自创的字典树哦:)#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <iostream>#include <fstream>using namespace std;const int N=100010;const long long bit=2147483648;struct node{ int l,r; node(): l(0), r(0) {}}tre[40*N];int num=0;void add(long long x){ long long a[32]; memset(a,0,sizeof(a)); int tmp,i=31; while( x ){ a[i--]=x%2; x/=2; } tmp=0; for(i=0;i<32;i++) { if( a[i]==0 && !tre[tmp].l ){ tre[tmp].l=++num; tmp=num; } else if( a[i]==1 && !tre[tmp].r ){ tre[tmp].r=++num; tmp=num; } else{ tmp=(a[i]==0 ? tre[tmp].l:tre[tmp].r); } }}long long getAns(long long x){ long long a[32],ans,BIT; memset(a,0,sizeof(a)); int tmp,i=31; while( x ){ a[i--]=x%2; x/=2; } tmp=0; ans=0; BIT=bit; for(i=0;i<32;i++) { if( a[i]==0 ) { if( tre[tmp].r>0 ){ ans+=BIT; tmp=tre[tmp].r; } else tmp=tre[tmp].l; } else { if( tre[tmp].l>0 ){ tmp=tre[tmp].l; } else { tmp=tre[tmp].r; ans+=BIT; } } BIT/=2; } return ans;}int main(){// freopen("in","r",stdin);// freopen("out","w",stdout); int t,i,j,n,m; long long x,ans; scanf("%d",&t); for(i=1;i<=t;i++) { scanf("%d%d",&n,&m); num=0; for(j=0;j<=32*n;j++) tre[j].l=tre[j].r=0; while( n-- ) { scanf("%I64d",&x); add(x); } printf("Case #%d:\n",i); while( m-- ) { scanf("%I64dd",&x); ans=getAns(x); printf("%I64d\n",ans); } } return 0;}
0 0
- hdu 4825
- HDU 4825 Xor Sum
- hdu 4825字典树
- hdu 4825 Xor Sum
- hdu 4825(trie)
- HDU 4825 Xor Sum
- hdu 4825 字典树
- HDU-4825 Xor Sum
- HDU 4825 Xor Sum
- HDU 4825 字典树
- HDU 4825-Xor Sum
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- POJ2836
- OS X 10.9 Mavericks下如何安装Command Line Tools(命令行工具)
- C++ protected的访问权限问题
- C++中string总结
- Dalvik虚拟机与java虚拟机的异同
- hdu 4825
- Pat(Advanced Level)Practice--1081(Rational Sum)
- leetcode做题总结,题目Best Time to Buy and Sell Stock 2012/10/30
- 设计模式-适配器(Adaper)模式
- 关于dup和dup2函数的重定向与还原
- HDU 2163 Palindromes
- 集合(Map练习)
- 最短路径Ⅰ—Dijkstra算法
- Java多态