[hdu1251]统计难题(trie模板题)
题意:返回字典中所有以测试串为前缀的字符串总数。
解题关键:trie模板题,由AC自动机的板子稍加改造而来。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
const int N=;
const int MAXN=;
struct Trie{//数组形式
int Next[MAXN][N],Fail[MAXN],End[MAXN],root,tot;//大小为所以匹配字符串的总和
int newnode(){//结构体内部用
for(int i=;i<N;i++) Next[tot][i]=-;
End[tot++]=;
return tot-;
}
void init(){
tot=;
root=newnode();
}
void insert(char buf[]){
int len=strlen(buf);
int now=root;//now是temp指针
for(int i=;i<len;i++){
int k=buf[i]-'a';
if(Next[now][k]==-) Next[now][k]=newnode();//next数组代表的是下一个字符索引
now=Next[now][k];
End[now]++;
}
}
int fnd(char buf[]){
int len=strlen(buf);
int now=root;//now是temp指针
for(int i=;i<len;i++){
int k=buf[i]-'a';
if(Next[now][k]==-) return ;//next数组代表的是下一个字符索引
now=Next[now][k];
}
return End[now];
}
};
Trie ac;
char buf[];
int main(){
ac.init();
while(gets(buf)){
if(buf[]==NULL)break;//gets读入的回车会自动转化为NULL
ac.insert(buf);
}
while(gets(buf)){
printf("%d\n",ac.fnd(buf));
}
return ;
}
[hdu1251]统计难题(trie模板题)的更多相关文章
- hdu1251 字典树trie 模板题
//字典树模板题.题意:给一个库,每次查询,求以之为前缀的单词数量. #include<iostream> #include<string> #include<vecto ...
- HDU1251 统计难题 Trie树
题目很水,但毕竟是自己第一道的Trie,所以还是发一下吧.Trie的更多的应用慢慢学,AC自动机什么的也慢慢学.... #include<iostream> #include<cst ...
- HDU1251 统计难题 trie树 简单
http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意: 找前缀数量 裸模板 #include<cstdio> #include<cstr ...
- HDU1251统计难题---Trie Tree
map巧过 #include <stdio.h> #include <string.h> #include <map> #include <string> ...
- HDU1251 统计难题 【trie树】
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU1251 统计难题(Trie)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU1251 统计难题 (字典树模板)题解
思路:模板题,贴个模板 代码: #include<cstdio> #include<cstring> #include<cstdlib> #include<q ...
- hdu1251统计难题(trie)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU 1251 统计难题(Trie)
统计难题 [题目链接]统计难题 [题目类型]Trie &题解: Trie的模板题,只不过这题坑点在没给数据范围,改成5e5就可以过了,用的刘汝佳蓝书模板 &代码: #include & ...
随机推荐
- MVC3 学习总结
1.项目文件结构 controllers,views 2.Model特性实现模型的客户端和服务端的验证 1)自带特性 2)扩展特性,或者重写特性 3.实现MVC filter 的类 ...
- asp.net 锚点
可以使用锚点,但这里可使用灵活处理 首先获取需要滚动到的位置的id,如,可以设置一个元素(,注:要在form里),另外在form的任意位置设置 代码如下: 注:a标签里不要有内容,在回传的地方调用 代 ...
- 1001 字符串“水”题(二进制,map,哈希)
1001: 字符串“水”题 时间限制: 1 Sec 内存限制: 128 MB提交: 210 解决: 39[提交][状态][讨论版] 题目描述 给出一个长度为 n 的字符串(1<=n<= ...
- NodeJs学习之API篇
学习nodeJS的API在对于使用nodeJS来进行编程的是十分重要的,所以首先就要去学习看看,相关的node的模块,来看一看相关的内容和可用性. 正文篇: nodeJS的API学习之路.(这里我们将 ...
- commons-dbcp连接池的使用
数据库连接池 池参数(所有池参数都有默认值): 初始大小: 最小空闲连接数: 增量:一次创建的最小单位(5个) 最大空闲连接数:12个 最大连接数:20个 最大的等待时间:1000毫秒 四大连接参数: ...
- 2018.7.28 A murder that scandalised Harvard and the world
A murder that scandalised Harvard and the worldVisiting Boston in 1868, Charles Dickens was asked wh ...
- BEC listen and translation exercise 11
When you are in any contest you should work as if there were — to the very last minute — a chance to ...
- BZOJ - 2618 凸多边形 (半平面交)
题意:求n个凸多边形的交面积. 半平面交模板题. #include<bits/stdc++.h> using namespace std; typedef long long ll; ty ...
- Network Saboteur (深搜递归思想的特殊使用)
个人心得:对于深搜的使用还是不到位,对于递归的含义还是不太清楚!本来想着用深搜构成一个排列,然后从一到n分割成俩个数组,然后后面发现根本实现不了,思路太混乱.后来借鉴了网上的思想,发现用数组来标志,当 ...
- 【LeetCode】028. Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...