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]; } }; ```
查看全文
相关阅读:
游览器保存密码和自动填充密码的困惑 (browser save password and auto fill password )
SQL basic
ui router digest 10 time
HttpCookie
web api 2 学习笔记 (OData Batch request)
$provide.decorator
Entity Framewor 学习笔记 (碎碎的东西)
生活类App原型制作分享-AnyList
2019年网页设计趋势前瞻,先睹为快!
Mockplus3.5.0.1新增标注功能
原文地址:https://www.cnblogs.com/orangee/p/9090287.html
最新文章
孤荷凌寒自学python第五十天第一次接触NoSql数据库_Firebase
孤荷凌寒自学python第四十九天继续研究跨不同类型数据库的通用数据表操作函数
matlab 函数说明--waitforbuttonpress
matlab 函数说明—conv2
matlab 函数说明—ordfilt2
图像处理相关知识(不断更新)
POJ 1005 解题报告
September 14th 2017 Week 37th Thursday
September 13th 2017 Week 37th Wednesday
September 12th 2017 Week 37th Tuesday
热门文章
September 11th 2017 Week 37th Monday
September 10th 2017 Week 37th Sunday
September 09th 2017 Week 36th Saturday
September 08th 2017 Week 36th Friday
September 07th 2017 Week 36th Thursday
September 06th 2017 Week 36th Wednesday
【2017-11-19】Linux基础知识:TP-Link WN823N无线网卡(RTL8192EU芯片)的X86-64及AARCH64驱动安装
MVC route 和 Angular router 单页面的一些方式
Visual Studio 2015 Owin+MVC+WebAPI+ODataV4+EntityFrawork+Identity+Oauth2.0+AngularJS 1.x 学习笔记之"坑"
Visual Studio 2015 Owin+MVC+WebAPI+ODataV4+EntityFrawork+Identity+Oauth2.0+AngularJS 1.x 学习笔记
Copyright © 2011-2022 走看看