HDU1251 统计难题(Trie)
统计难题
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 26574 Accepted Submission(s): 10760
注意:本题只有一组测试数据,处理到文件结束.
band
bee
absolute
acm
ba
b
band
abc
3
1
0
【思路】
Trie。
构建一棵字典树,并维护tot[u]表示以u为根的子数中叶子节点的数目。
需要注意的是maxnode要开大一些。
【代码】
#include<cstdio>
#include<cstring>
#include<iostream>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; const int maxnode = ;
//输入的字符串可能很多
const int maxl = ;
const int sigmasize = ; struct Trie{
int ch[maxnode][sigmasize];
int tot[maxnode];
int sz; Trie() {
sz=;
memset(ch[],,sizeof(ch[]));
tot[]=;
}
int idx(char c) { return c-'a'; }
void insert(char* s) {
int n=strlen(s),u=;
for(int i=;i<n;i++) {
int c=idx(s[i]);
if(!ch[u][c]) {
ch[u][c]=sz;
memset(ch[sz],,sizeof(ch[sz]));
tot[sz]=;
sz++;
}
u=ch[u][c];
tot[u]++;
}
}
int find(char* s) {
int n=strlen(s),u=;
for(int i=;i<n;i++) {
int c=idx(s[i]);
if(!ch[u][c]) return ;
else u=ch[u][c];
}
return tot[u];
}
}trie; int n; int main() {
//freopen("cin.in","r",stdin);
//freopen("coutme.out","w",stdout);
char s[maxl];
while(gets(s) && s[]) {
trie.insert(s);
}
while(gets(s)) {
printf("%d\n",trie.find(s));
}
return ;
}
HDU1251 统计难题(Trie)的更多相关文章
- HDU1251 统计难题 Trie树
题目很水,但毕竟是自己第一道的Trie,所以还是发一下吧.Trie的更多的应用慢慢学,AC自动机什么的也慢慢学.... #include<iostream> #include<cst ...
- [hdu1251]统计难题(trie模板题)
题意:返回字典中所有以测试串为前缀的字符串总数. 解题关键:trie模板题,由AC自动机的板子稍加改造而来. #include<cstdio> #include<cstring> ...
- 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 Submi ...
- HDU1251 统计难题 【trie树】
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- hdu1251 统计难题
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1251 题目: 统计难题 Time Limit: 4000/2000 MS (Java/Othe ...
- hdu 1251 统计难题 trie入门
统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...
- HDU 1251 统计难题(Trie)
统计难题 [题目链接]统计难题 [题目类型]Trie &题解: Trie的模板题,只不过这题坑点在没给数据范围,改成5e5就可以过了,用的刘汝佳蓝书模板 &代码: #include & ...
随机推荐
- HTML5 文件域+FileReader 读取文件(二)
一.读取文本文件内容,指定字符编码 <div class="container"> <!--文本文件验证--> <input type="f ...
- (转)PHP下编码转换函数mb_convert_encoding与iconv的使用说明
之--http://www.jb51.net/article/21451.htm mb_convert_encoding这个函数是用来转换编码的.原来一直对程序编码这一概念不理解,不过现在好像有点开窍 ...
- ASP.NET如何发布更新
如果一个应用程序迭代开发后,我们如何来进行发布更新呢? 这里,我用Visual Studio 2008和SQL server来演示. 一.程序修改完毕后,我们要进行以下操作: 1.1 点击解决方案,重 ...
- mysql 安装-编码
mysql的安装过程相对较为简单,在这里就不阐述,我想说的问题是,关于编码的安装, 在安装到达'Please select the default character set'的时候,选择'Manul ...
- Android TV 模拟器启动
模拟器启动错误 使用IntelCPU的模拟器 http://software.intel.com/en-us/android/articles/intel-hardware-accelerated-e ...
- 【转】iOS-Core-Animation-Advanced-Techniques(三)
原文: http://www.cocoachina.com/ios/20150105/10827.html 专用图层 复杂的组织都是专门化的--Catharine R. Stimpson 到目前为止, ...
- 安卓开发之viewpager学习(头条显示)
activity_luancher.xml代码如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res ...
- C++中extern关键字使用(转)
参考文章:http://blog.csdn.net/sruru/article/details/7951019 chapter1.如何混合编译C语言和C++ 实际开发过程中,C++中会调用C与语言编写 ...
- Perl中级第四章课后习题
1.下列表达式各表示什么不同的含义: $ginger->[2][1] ${$ginger[2]}[1] $ginger->[2]->[1] ${$ginger->[2]}[1] ...
- linux常用命令(查看某些软件是否已安装)
查看imap是否已安装 rpm -qa | grep imap 以下为未安装的情形: 检查是否已安装sendmail: rpm -qa | grep sendmail 以下为已安装的返回: