HDU1251 统计难题 trie树
题意:给定一些单词和一些前缀,问每个前缀出现多少次
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251
第一次写trie树,数组开小交上去TLE调了好久
1 #include<bits/stdc++.h> 2 3 using namespace std; 4 5 const int MAXN = 2333666; 6 int s[MAXN], g[MAXN][26], n; 7 char str[20]; 8 9 int find (char str[]) { 10 int x = 1; 11 for (int i = 0; str[i]; ++i) { 12 char ch = str[i]; 13 if (g[x][ch - 'a'] == 0) return 0; 14 x = g[x][ch - 'a']; 15 } 16 return s[x]; 17 } 18 19 void add (char str[]) { 20 int x = 1; 21 ++s[1]; 22 for (int i = 0; str[i]; ++i) { 23 char ch = str[i]; 24 if (g[x][ch - 'a'] == 0) { 25 g[x][ch - 'a'] = ++n; 26 } 27 x = g[x][ch - 'a']; 28 ++s[x]; 29 } 30 } 31 32 int main() { 33 n = 1; 34 while (gets(str) && str[0] != NULL) { 35 add(str); 36 } 37 while (gets(str) && str[0] != NULL) { 38 printf("%d\n", find(str)); 39 } 40 return 0; 41 }
View Code
转载于:https://www.cnblogs.com/m-m-m/p/8621486.html
HDU1251 统计难题 trie树相关推荐
- HDU1251 统计难题 trie树 简单
http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意: 找前缀数量 裸模板 1 #include<cstdio> 2 #include< ...
- hdu 1251 统计难题(trie树入门)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- hdu 1251 统计难题 (Trie树)
本题是trie树模板题,如果不用trie而用map写可以看出trie处理这类问题有明显的时间优势. 在trie树中查找一个关键字的时间和树中包含的结点数无关,而取决于组成关键字的字符数.(对比:二叉查 ...
- HDU1251 统计难题【字典树】
统计难题 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 ...
- HDU 1251 统计难题 字典树/STL
统计难题 Time Limit:2000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u Description Ig ...
- hdu1251(统计难题)
这题就是一个字典树的模板题 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Othe ...
- hdu 1251 统计难题 (字典树入门题)
1 /******************************************************* 2 题目: 统计难题 (hdu 1251) 3 链接: http://acm.hd ...
- HDU - 1251 统计难题(字典树)
题目链接:点击查看 题目大意:给出一些单词,后续再给出一些前缀,询问包含此前缀的单词一共有多少个 题目分析:这个题目的数据可能有点水,而且时间给的也很足,给了两秒,而且加上是hdu的,可以用无序map ...
- HDU-1251 统计难题 map写法
#include<cstdio> #include<cstring> #include<cstdlib> #include<string> #inclu ...
最新文章
- 【ios开发/Xcode】实现关键字检索
- 活久见!音乐在AR手里“活”了起来
- bminfowindow是什么_三步实现地图自定义InfoWindow
- max 安装 mysql5.x_MySQL5.7安装
- docker 无法正常启动或版本信息会报错 Cannot connect to the Docker daemon at
- linux各种复制命令
- 内存不能“read”
- 趣学java,编程趣学习app
- c#输出最大值、最小值和平均值(B)【C#】
- python3+urllib打造新浪微博爬虫,真的很easy
- Java 如何优雅的导出 Excel
- 电力仿真, 电力可视化源码库,MATLAB仿真,Multisim仿真,PSASP仿真, 电力地理接线图,VC++ 控件源程序
- LINUX HTB队列规定用户指南
- java criterion_hibernate Criterion和Criteria
- 176. 第二高的薪水(SQL中的ifnull函数和limt1,1的使用)
- vue kepp-alive实践总结
- python求平均工资_python实现求和,求平均值——函数
- 手把手教你在VSCode中使用Git
- iscsi 网络存储
- QT+PCL+VS制作点云显示界面(彩色显示xyz点云)