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 ...
随机推荐
- AspNetCore容器化(Docker)部署(三) —— Docker Compose容器编排
一.前言 上一篇部署了一个最基础的helloworld应用,创建了两个容器和一个network,还算应付得过来. 如果该应用继续引入mysql.redis.job等若干服务,到时候发布一次得工作量之大 ...
- PAT (Basic Level) Practise (中文)-1028. 人口普查(20)
PAT (Basic Level) Practise (中文)-1028. 人口普查(20) http://www.patest.cn/contests/pat-b-practise/1028 某 ...
- 电商技术中企业数据总线ESB和注册服务管理的区别
一.概述 1.什么是ESB 就是企业数据总线的意思,他的核心功能就是兼容各种协议接口,可以将数据在各种协议之间进行流转,并且可以针对数据格式进行编排转换. 异构系统,功能繁多,复杂 代表性的项目有:J ...
- iOS 常用尺寸
APP ICON: @1x:57*57 @2x:114*114 @3x:171*171 机型 屏幕尺寸 像素(px)pixel 点(pt)point PPI iphone4s 3.5吋 ...
- shell基础笔记1
---恢复内容开始--- 1 test命令中不能使用浮点小数值,如: A=1.444444:[ $A -gt 1 ] 2 test命令中的>或<必须转义,否则shell会把它 ...
- 初涉trie
trie:字符串算法中的重要“数据结构” 什么是trie trie就是利用字符串的公共前缀所建成的树. 众所周知树是有很多很好的性质的,于是trie可以结合其他知识点做一些有趣的事情. trie的例题 ...
- Elasticsearchs的安装/laravel-scout和laravel-scout-elastic的安装
安装: https://github.com/medcl/elasticsearch-rtf 先下载包 下载解压后 cd elasticsearch-rtf-master ll bin/elastic ...
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode(40) Combination Sum II
题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...
- struts向网页输出图片验证码
前言:今天做个功能需要展示图片到页面,并不是下载,在网上搜了老半天,大部分都是下载,有的话也是只能在IE下进行输出,其它浏览器就都是下载了. Action代码: public String proce ...