hdu1251 字典树trie 模板题
//字典树模板题.题意:给一个库,每次查询,求以之为前缀的单词数量。
#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;
int tree[500000][27];
int w[500000]; //记录结点属性。
int numv=0; // 全局变量,记录顶点编号
int n;
void insert(string s) //建树 tree[u][i]:结点u的第i个孩子('a'为0,以此类推).
{ //值是i对应的孩子结点编号.这个要遍历孩子边,要遍历26次,
//否则添加vector<vector<> >,来保存边也可.
int u=0;
int len=s.size();
for(int i=0;i<len;i++)
{
if(tree[u][s[i]-'a']==0) //新建顶点
{
tree[u][s[i]-'a']=++numv;
}
u=tree[u][s[i]-'a']; //往下找
w[u]++;
}
}
int find(string s)
{
int u=0;
int len=s.size();
for(int i=0;i<len;i++) //找不到
{
if(tree[u][s[i]-'a']==0)
return 0;
u=tree[u][s[i]-'a'];
}
return w[u]; }
int main()
{
string s;
while(1) //一直读入字符串,到换行为止
{
cin>>s;
insert(s);
getchar();
if(cin.peek()=='\n')break;
}
while(cin>>s)
{
cout<<find(s)<<endl;
}
return 0;
}
hdu1251 字典树trie 模板题的更多相关文章
- hdu1251+字典树常用模板
这里只简单给出几个常用的字典树的模板,要看具体介绍的请看:传送门 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现) ...
- [hdu1251]统计难题(trie模板题)
题意:返回字典中所有以测试串为前缀的字符串总数. 解题关键:trie模板题,由AC自动机的板子稍加改造而来. #include<cstdio> #include<cstring> ...
- HDU 1251 统计难题(字典树入门模板题 很重要)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- Hihicoder 题目1 : Trie树(字典树,经典题)
题目1 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编 ...
- hdu 1251:统计难题(字典树,经典题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序
一. 题目 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 274040 Accepted: 48891 ...
- 『字典树 trie』
字典树 (trie) 字典树,又名\(trie\)树,是一种用于实现字符串快速检索的树形数据结构.核心思想为利用若干字符串的公共前缀来节约储存空间以及实现快速检索. \(trie\)树可以在\(O(( ...
- poj 2503:Babelfish(字典树,经典题,字典翻译)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30816 Accepted: 13283 Descr ...
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
随机推荐
- No package python-pip available
因为没有此rpm包,此包包含在epel源里面 输入rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarc ...
- ArcMap所有Command GUID
The information in this topic is useful if you're trying to programmatically find a built-in command ...
- DNA fingerprinting|haplotpe|frequency of polymorphism|限制性标记的多态性
5.4利用RFLP和SNP绘制遗传图 因为限制性标记可以确定那个分子水平上的突变(即已知基因座),但是无法和蛋白质功能相联系.所以我们采用限制性标记的多态性,即该限制酶识别的位点若发生突变,则大概率在 ...
- EXTJS中文乱码
在<head>中加入 <meta http-equiv="Content-Type" content="text/html; charset=GB231 ...
- shell脚本,alias别名命令用法。
[root@localhost ~]# alias alias cp='cp -i' alias mv='mv -i' alias rm='rm -i' [root@localhost ~]# [ro ...
- c++ 递归求一个数的阶乘
#include <iostream> using namespace std; long factorial(int value); int main() { int value; co ...
- minGw64编译Qt时遇到too many sections问题
minGw64编译Qt时遇到too many sections问题: 修改\Src\qtbase\mkspecs\win32-g++\qmake.conf中 QMAKE_CFLAGS ...
- mysql 慢查询日志 pt-query-digest 工具安装
介绍:pt-query-digest是用于分析mysql慢查询的一个工具,它可以分析binlog.General log.slowlog,也可以通过SHOWPROCESSLIST或者通过tcpdump ...
- 【转】ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
为了加强安全性,MySQL5.7为root用户随机生成了一个密码,在error log中,关于error log的位置,如果安装的是RPM包,则默认是/var/log/mysqld.log. 一般可通 ...
- python 学习总结4
数字类型及操作 一.整数类型 (1)python中的整数与数学中的概念是一致的,可以正也可以负,没有取值范围. pow(x,y)函数是计算x的y次幂,想计算多大就多大. (2)在整数类型中有四种进 ...