Trie(字典树)

基本数据结构

实际是:对于每个字符串组的每一个不同前缀建立节点

基本代码

void Insert(char *s,int p){
int now=0;
int l=strlen(s);
rep(i,0,l-1){
int k=s[i]-'a';
if(!nxt[now][k]) nxt[now][k]=++ncnt;
now=nxt[now][k];
}
}

不得不说,结构非常简单

但它有很多妙用

--->Xor Sum

典型数字trie

如何求出最大异或?

从最高位开始,能不同则不同,在建立的trie树上跑就行

const int N=100000,K=50;
int n,m; int nxt[N*32][2];
int cnt; void Insert(int x){
int now=1;
drep(i,31,0) {
int y=(x&(1<<i))>0;
if(!nxt[now][y]) nxt[now][y]=++cnt;
now=nxt[now][y];
}
} int Que(int x){
int now=1,ans=0;
drep(i,31,0) {
int y=(x&(1<<i))>0;
if(nxt[now][!y]) ans|=(!y)<<i,now=nxt[now][!y];
else ans|=y<<i,now=nxt[now][y];
}
return ans;
} int main(){
rep(kase,1,rd()){
cnt=1; memset(nxt,0,sizeof nxt);
n=rd(),m=rd();
rep(i,1,n) Insert(rd());
printf("Case #%d:\n",kase);
rep(i,1,m) {
int ans=Que(rd());
printf("%d\n",ans);
}
}
}

最新文章

  1. 【原】python中文文本挖掘资料集合
  2. fedora22切换用户windows分区不能自动挂载
  3. Crontab命令格式
  4. FileOutputStream和FileInputStream的用法
  5. 使用navicat连接mysql要报10038的错误
  6. (Python)导出指定文件夹中as文件的完全限定类名
  7. RabbitMQ基本概念和使用
  8. Gerrit日常操作命令收集
  9. 十大响应式Web设计框架
  10. 手机端的META你有多了解?
  11. android.support.v7.widget.Toolbar 中menu图标不显示问题
  12. ZooKeeper实现分布式锁
  13. nnet3的并行化训练
  14. 报错:Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: Lorg/apache/hadoop/fs/FileSystem
  15. EF C# ToPagedList方法 The method &#39;Skip&#39; is only supported for sorted input in LINQ to Entities. The method &#39;OrderBy&#39; must ……
  16. spring-cloud-config——Quick Start
  17. ZVAL——PHP源码分析
  18. IE6/7 单选按钮 radio 无法选中解决方法
  19. BZOJ3105:[CQOI2013]新Nim游戏(线性基,贪心)
  20. 【Go命令教程】5. go clean

热门文章

  1. 全程实操cdh5.14.4中集成安装kylin2.4.1与使用测试
  2. 使用node+vue实现简单的WebSocket聊天功能
  3. WebStorm eslint插件报错解决 - TypeError: this.CliEngine is not a constructor
  4. JavaScript API 与 DOM
  5. AR自动开票主程序导入发票的时候,出现错误提示&#39;&#39;不能获取汇款地址&#39;&#39;
  6. tail: inotify cannot be used, reverting to polling: Too many open files
  7. 面试常问的join
  8. js检测页面触底
  9. CC2530调试过程中遇到的问题们
  10. Python调用百度接口(情感倾向分析)和讯飞接口(语音识别、关键词提取)处理音频文件