hihoCoder#1014 Trie树 (前缀树)
题目大意:给一本有n个单词的词典,有m次询问,每次询问的是该词典中有多少个单词有共同的某个前缀。
题目分析:在添加单词建立trie的时候,每经过一个节点就意味着该节点和它的各级祖先节点是某个单词的前缀,这样就可以在建立trie的同时统计单词个数。
代码如下:
# include<iostream>
# include<cstdio>
# include<cstring>
# include<vector>
# include<queue>
# include<list>
# include<set>
# include<map>
# include<string>
# include<cmath>
# include<cstdlib>
# include<algorithm>
using namespace std;
# define LL long long const int N=1005;
const int INF=1000000000;
const LL oo=0x7fffffffffffffff;
const double eps=1e-10; string p;
int len[N*1000];
int cnt;
int tr[N*1000][26]; void insert()
{
int u=0;
int n=p.size();
for(int i=0;i<n;++i){
int c=p[i]-'a';
if(!tr[u][c]){
memset(tr[cnt],0,sizeof(tr[cnt]));
tr[u][c]=cnt++;
}
u=tr[u][c];
++len[u];
}
} int query()
{
int n=p.size();
int u=0;
for(int i=0;i<n;++i){
if(!tr[u][p[i]-'a'])
return 0;
u=tr[u][p[i]-'a'];
}
return len[u];
} int main()
{
int n,m;
while(~scanf("%d",&n))
{
cnt=1;
memset(len,0,sizeof(len));
memset(tr,0,sizeof(tr));
while(n--)
{
cin>>p;
insert();
}
scanf("%d",&m);
while(m--)
{
cin>>p;
printf("%d\n",query());
}
}
return 0;
}
hihoCoder#1014 Trie树 (前缀树)的更多相关文章
- 【数据结构与算法】Trie(前缀树)模板和例题
Trie 树的模板 Trie 树的简介 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树.他的核心思想是空间换 ...
- 9-11-Trie树/字典树/前缀树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第9章 查找 - Trie树/字典树/前缀树(键树) ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版>(严蔚 ...
- Hihocoder #1014 : Trie树 (字典数树统计前缀的出现次数 *【模板】 基于指针结构体实现 )
#1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助, ...
- hihoCoder #1014 : Trie树 [ Trie ]
传送门 #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互 ...
- hihoCoder 1014 Trie树 (Trie)
#1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi和小Ho是一对好朋友.出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮 ...
- codevs——4189 字典&&HihoCoder #1014 : Trie树
题目描述 Description 最经,skyzhong得到了一本好厉害的字典,这个字典里整整有n个单词(1<=n<=200000) 现在skyzhong需要在字典里查询以某一段字母开头的 ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- Trie(前缀树/字典树)及其应用
Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...
- Trie(前缀树)和ternary trie和binary search tree
1 什么是trie trie是一棵多叉树,假如存放的是由26个字母(不区分大小写)构成的字符串的话,那么就是一棵26叉树. trie树是一棵前缀树,因为每个结点只保存字符串中的一个字符,整个字符串保存 ...
- [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
随机推荐
- 最大公约数——Program G
最大公约数 Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbit ...
- 从github上面拷贝源码
http://www.cnblogs.com/xing901022/p/4287064.html
- Js判断键盘按键
该文转自: namehwh 网址:http://www.cnblogs.com/hanwenhua/articles/3365154.html window.document.onkeydown = ...
- Ogre碰撞检测
转自:http://blog.csdn.net/weiqubo/article/details/7108363 Ogre采用树桩管理场景中的各种"元素"(摄像机.灯光.物体等),所 ...
- (function($){...})(jQuery) 含义
最近在项目js文件末端中发现这样的代码,对于前端技术比较薄弱的我,着实还是有点晕,好在查阅到了相关资料,现解释如下: (function($){ $.plugin = new org.plugin. ...
- FR #3题解
A. 傻逼题?...前缀和什么的随便搞搞就好了. #include<iostream> #include<cstdio> #include<cstring> #in ...
- BZOJ 4011 落忆枫音
几个重点: 1.从每个点任选一条入边,都可以成为一个树形图. 2.于是考虑所有答案减去有环的答案. 3.将要求的东西形式化表示出来,然后发现可以直接dp.. 好厉害啊.. #include<io ...
- BZOJ 1690 奶牛的旅行
分数规划. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...
- Objective-C determine data network type of the iOS device
Im on an application that receive data from server, the problem is when user connect to cellular dat ...
- iptables 用法
又有人攻击服务器了,没有办法又的去防,这里简单介绍一种限制指定IP访问的办法. 单个IP的命令是 iptables -I INPUT -s 59.151.119.180 -j DROP 封IP段的命 ...