Trie学习笔记
2024-09-03 15:13:29
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);
}
}
}
最新文章
- 【原】python中文文本挖掘资料集合
- fedora22切换用户windows分区不能自动挂载
- Crontab命令格式
- FileOutputStream和FileInputStream的用法
- 使用navicat连接mysql要报10038的错误
- (Python)导出指定文件夹中as文件的完全限定类名
- RabbitMQ基本概念和使用
- Gerrit日常操作命令收集
- 十大响应式Web设计框架
- 手机端的META你有多了解?
- android.support.v7.widget.Toolbar 中menu图标不显示问题
- ZooKeeper实现分布式锁
- nnet3的并行化训练
- 报错:Exception in thread ";main"; java.lang.NoClassDefFoundError: Lorg/apache/hadoop/fs/FileSystem
- EF C# ToPagedList方法 The method &#39;Skip&#39; is only supported for sorted input in LINQ to Entities. The method &#39;OrderBy&#39; must ……
- spring-cloud-config——Quick Start
- ZVAL——PHP源码分析
- IE6/7 单选按钮 radio 无法选中解决方法
- BZOJ3105:[CQOI2013]新Nim游戏(线性基,贪心)
- 【Go命令教程】5. go clean
热门文章
- 全程实操cdh5.14.4中集成安装kylin2.4.1与使用测试
- 使用node+vue实现简单的WebSocket聊天功能
- WebStorm eslint插件报错解决 - TypeError: this.CliEngine is not a constructor
- JavaScript API 与 DOM
- AR自动开票主程序导入发票的时候,出现错误提示&#39;&#39;不能获取汇款地址&#39;&#39;
- tail: inotify cannot be used, reverting to polling: Too many open files
- 面试常问的join
- js检测页面触底
- CC2530调试过程中遇到的问题们
- Python调用百度接口(情感倾向分析)和讯飞接口(语音识别、关键词提取)处理音频文件