博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nyoj - 947(Max Xor)字典树
阅读量:4286 次
发布时间:2019-05-27

本文共 1239 字,大约阅读时间需要 4 分钟。

题目链接:

/**    你可以把所有的数字变成等长的二进制,当成字符串    然后建字典树,再枚举每个数把这个数的每一位取反去匹配    匹配的时候能匹配就尽量匹配,不能就走不同的分支    这样求出来的就是这个数能搞出来的最大的**/#include
#include
#include
#include
using namespace std;typedef long long LL;LL a[100005],c[55];struct node{ int sum; node *next[2];};struct node *root;struct node *build(){ struct node *p; p=(struct node *)malloc(sizeof(struct node)); for(int i=0; i<2; i++) { p->next[i]=NULL; } return p;}void solve(LL s){ memset(c,0,sizeof(c)); int num = 50; while(s) { c[num--] = s%2; s/=2; }}void insert(LL s){ solve(s); struct node *p; p=root; for(int i=0; i<=50; i++) { if(p->next[c[i]]==NULL) p->next[c[i]]=build(); p=p->next[c[i]]; }}LL find(LL s){ solve(s); struct node *p; p=root; LL sum = 0; for(int i=0; i<=50; i++) { if(p->next[!c[i]]==NULL)//尽量使高位为1,所以取反 { sum = sum * 2; p=p->next[c[i]]; } else { sum = sum * 2 + 1; p=p->next[!c[i]]; } } return sum;}int main(){ int n,i; while(scanf("%d",&n)!=EOF) { root=build(); LL ans = 0; for(i=0; i

转载地址:http://xbsgi.baihongyu.com/

你可能感兴趣的文章
EntiryFramework中事务操作实例
查看>>
删除github上的远程分支
查看>>
Visual Studio Code 1.8 发布
查看>>
SQL Server Management Studio 2016 (SSMS)
查看>>
EF中Sum()异常:到值类型“System.Decimal”的强制转换失败,因为具体化值为 null。
查看>>
Visual Studio Code插件之Atom One Dark Syntax Theme
查看>>
EntiryFramework中事务操作(二)TransactionScope
查看>>
EF获取非跟踪数据之DBSet.AsNoTracking()
查看>>
关于EF6.0整理
查看>>
C# using 关键字使用整理
查看>>
EF日期格式筛选_EF常用日期筛选逻辑整理
查看>>
EF日期筛选异常:SqlServer.DATEDIFF”函数的 DATEPART 参数必须是文字字符串。
查看>>
C# 托管资源 与 非托管资源
查看>>
C#析构函数
查看>>
C#IDisposable 接口&资源释放
查看>>
cssnext简介
查看>>
PostCss简介
查看>>
比较全的前端整理
查看>>
C# lock关键词/lock语句块、线程锁
查看>>
EF TransactionScope异常:分布式事务已完成。请将此会话登记到新事务或 NULL 事务中。
查看>>