ACM Trie树 字典树模板
2016-04-06 21:54
399 查看
ACM Trie树 字典树模板
今天做微软实习生笔试题傻逼了,写了好久呀QAQ,手生了/* * Author: NICK WONG * Created Time: 2015/9/3 13:56:58 * File Name: */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; #define out(x) cout<<#x<<": "<<x<<endl const double eps(1e-8); const int maxn=10100; const long long maxint=-1u>>1; const long long maxlong=maxint*maxint; typedef long long lint; #define maxnode 41000 #define sigma_size 26 struct trie { int ch[maxnode][sigma_size]; int val[maxnode]; int sz; trie() { sz=1; memset(ch[0],0,sizeof(ch[0])); } inline int idx(char c) { return c-'a'; } void insert(char *s, int vv) { int u=0, n=strlen(s); for (int i=0; i<n; i++) { int c=idx(s[i]); if (ch[u][c]==0) //empty { memset(ch[sz],0,sizeof(ch[sz])); val[sz]=0; //not a word ch[u][c]=sz++; } u=ch[u][c]; } val[u]=vv; } bool search(char *s, int vv) { int u=0, n=strlen(s); for (int i=0; i<n; i++) { int c=idx(s[i]); if (ch[u][c]==0) return false; u=ch[u][c]; } if (val[u]==0) return false; return true; } }; void init() { } void work() { } int main() { init(); work(); return 0; }
参考
6天通吃树结构—— 第五天 Trie树《算法竞赛入门经典之训练指南》
HDU1004,1247,1254,1671
相关文章推荐
- 测试(一)试题及答案
- SensorSimulator安装
- Windows Server 2008 R2 802.1X和NAP在企业中的整合应用
- SQL 2008升级SQL 2008 R2完全教程或者10.00.1600升级10.50.1600
- windows环境下搭建python+nltk开发环境
- 文章标题
- POJ 3415 不小于k的公共子串的个数
- Facebook或成云领域黑马 冲击亚马逊
- 支持常见数据库差异对照说明
- .NET深入 c#数据类型2
- POJ 3415 不小于k的公共子串的个数
- 最小费用最大流
- Windows Server 2008 R2 Server Core管理
- 利用运行时(Runtime)的方法交叉(Method Swizzling)检测控制器是否释放
- jQuery UI添加效果
- git建立远程仓库
- javascript双等号引起的类型转换
- Jquery和JS的区别
- 【01背包】HDU1864最大报销额
- 计算机史