zoukankan
html css js c++ java
字典树与01字典树
之前在做一道关于字符串匹配的题时,用到了[字典树](https://www.cnblogs.com/orangee/p/8912971.html),但那时是用指针实现的,这次又遇到需要使用字典树这一结构的题,向学姐要了她的板子,学习了用数组实现的方法,对于解题而言,更为简短快速。 因为题目要求最大异或和,因此用的是01字典树,在字典树的基础上稍作修改。 以下为字典树和01字典树的普遍实现: 字典树 ```C++ #include
#include
using namespace std; struct Trie { static const int N = 101010 , M = 26; int node[N][M],cnt[N],root,L; //cnt记录对应节点的字符串个数 void init() { fill_n(cnt,N,0); fill_n(node[N-1],M,0); L = 0; root = newnode(); } int newnode() { fill_n(node[L],M,0); return L++; } void add(char *s) { int p = root; for(int i=0;s[i];++i) { int c = s[i] - 'a'; if(!node[p][c]) node[p][c] = newnode(); p = node[p][c]; } ++cnt[p]; } }; ``` 01字典树(可用于求异或和最大问题,long long型开64倍,int型开32倍) ```C++ #include
using namespace std; struct Trie_01 { static const int maxn=1e5+10,N = 32*maxn,M = 2; int node[N][M],value[N],rt,L; //value记录对应节点的值,用于返回 void init() { fill_n(node[N-1],M,0); fill_n(value,N,0); L = 0; rt = newnode(); } int newnode() { fill_n(node[L],M,0); return L++; } void add(int x) { int p = rt; for (int i=31;i>=0;--i) { int idx = (x>>i)&1; if (!node[p][idx]) { node[p][idx] = newnode(); } p = node[p][idx]; value[p]=min(value[p],x); } } int query(int x) { int p = rt; for (int i=31;i>=0;--i) { int idx = (x>>i)&1; if (node[p][idx^1]) p = node[p][idx^1]; else p = node[p][idx]; } return value[p]; } }; ```
查看全文
相关阅读:
科学美国人(Scientific American)部分段落小译
Matlab安装使用libsvm
【转】Matlab中特殊符号的写法
计算机视觉资源
AdaBoost
AdaBoost人脸检测原理
NLP常用开源/免费工具(转)
搜索背后的奥秘——浅谈语义主题计算
求数组当中子数组最大和
求二叉树中两个节点的最低父节点
原文地址:https://www.cnblogs.com/orangee/p/9090287.html
最新文章
【转】残缺的六度理论和SNS实践者们
附加SQLServer数据库时出现的错误(错误5173:不能使文件与不同的数据库相关)的解决方案
jquery解析JSON数据的方法
JavaScript 获取页面宽高的方法
通过div样式控制单元格文本超长省略
jquery validate 插件:(2)简单示例
ArrayList 与 string、string[] 的转换
jquery validate 插件:(1)使用说明
asp.net防止页面刷新或后退引起重复提交
jquery avlidate 插件:(3)校验规则
热门文章
frameset, iframe, frame框架页面在IE6中出现横向滚动条bug的隐藏方法
超声测温(ultrasound thermometry, ultrasound temperature estimation / imaging)领域知名学者之一:劉浩澧/HaoLi Liu (http://ee.cgu.edu.tw/files/151007556,c2361.php)
三年硕士五年博,霜染青丝纹上额
经济类图书推荐转自水木
博士3年12篇SCI论文(平均2.7分)的传奇经历
离合器、刹车、油门的操作技巧【转】
国内生物医学工程届著名学者
五遥
开设生物医学工程的高校(按区域划分)
SCI SSCI CSSCI EI ISTP
Copyright © 2011-2022 走看看