统计难题

Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
 
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.

注意:本题只有一组测试数据,处理到文件结束.

 
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
 
Sample Input
banana
band
bee
absolute
acm

ba
b
band
abc

 
Sample Output
2
3
1
0
 
坑点:题目没有给定输入的字符串个数,我将maxnode = 10000*10 + 10;即认为输入字符串个数不超过10000得到的竟然是TLE...之后把maxnode改成4000*1000 + 10才A;
当字符集较大时,不宜使用26叉trie树,这里使用了左孩子右兄弟建树;
 
#include<cstring>
#include<vector>
#include<cstdio>
using namespace std; const int maxnode = * + ;//字符串个数乘以长度
const int sigma_size = ; // 字母表为全体小写字母的Trie
struct Trie {
int head[maxnode]; // head[i]为第i个结点的左儿子编号
int next[maxnode]; // next[i]为第i个结点的右兄弟编号
char ch[maxnode]; // ch[i]为第i个结点上的字符
int tot[maxnode]; // tot[i]为第i个结点为根的子树包含的叶结点总数,即该点作为多少个单词的前缀;
int sz; // 结点总数
long long ans; // 答案
void clear() { sz = ; tot[] = head[] = next[] = ; } // 初始时只有一个根结点 // 插入字符串s(包括最后的'\0'),沿途更新tot
void insert(const char *s) {
int u = , v, n = strlen(s);
tot[]++;
for(int i = ; i <= n; i++) {// =n是为了加上'\0';
// 找字符a[i]
bool found = false;
for(v = head[u]; v != ; v = next[v])
if(ch[v] == s[i]) { // 找到了
found = true;
break;
}
if(!found) {
v = sz++; // 新建结点
tot[v] = ;
ch[v] = s[i];
next[v] = head[u];//将左孩子转为右兄弟
head[u] = v; // 插入到链表的首部
head[v] = ;
}
u = v;
tot[u]++;
}
} int count(const char *s){
int v = head[], n = strlen(s),ans = ;
for(int i = ;i < n; i++){
while(v != && ch[v] != s[i]) v = next[v];
if(v == ) return ;
ans = tot[v];
v = head[v];
}
return ans;
}
}trie;
int main()
{
trie.clear();
char s[];
while(gets(s),s[] != '\0')
trie.insert(s);
while(scanf("%s",s) == && s[] != '\0'){
printf("%d\n",trie.count(s));
}
return ;
}
 

hdu 1251 统计难题 trie入门的更多相关文章

  1. hdu 1251 统计难题(trie树入门)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  2. HDU 1251 统计难题(Trie)

    统计难题 [题目链接]统计难题 [题目类型]Trie &题解: Trie的模板题,只不过这题坑点在没给数据范围,改成5e5就可以过了,用的刘汝佳蓝书模板 &代码: #include & ...

  3. HDU - 1251 统计难题(trie树)

    Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).  Input输入数据的第一部 ...

  4. HDU 1251 统计难题 (Trie)

    pid=1251">统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/ ...

  5. hdu 1251 统计难题(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others)    M ...

  6. HDU 1251 统计难题(Trie模版题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  7. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  8. HDU 1251 统计难题(字典树入门模板题 很重要)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  9. HDU 1251 统计难题 (字符串-Trie树)

    统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...

随机推荐

  1. Distributed locks with Redis--官方

    原文:http://redis.io/topics/distlock Distributed locks with Redis Distributed locks are a very useful ...

  2. linux中shell编程

    shell编程 1 echo -e 识别\转义符 \a \b \t \n \x十六进制 \0八进制 等等 #!/bin/bash echo -e "hello world" 执行脚 ...

  3. About xvfb

    http://blog.csdn.net/span76/article/details/11473315 有时候我们不关注程序是否有界面(比如自动化测试),只要程序在运行就可以了 很感谢 xvfb 这 ...

  4. background不能填充margin的问题

    CSS2 中有5个主要的背景(background)属性,它们是: background-color: 指定填充背景的颜色.background-image: 引用图片作为背景.background- ...

  5. 关于JFace中的输入值(InputDialog)对话框类

    格式: InputDialog(Shell parentShell, String dialogTitle, String dialogMessage, String initialValue, In ...

  6. 关于Git的工作区域和对应的文件状态.

    Git是一个免费的.分布式的版本控制工具,或是一个强调了速度快的源代码管理工具.每一个Git的工作目录都是一个完全独立的代码库,并拥有完整的历史记录和版本追踪能力,不依赖于网络和中心服务器. Git ...

  7. MSP430常见问题之开发工具类

    Q1:我自己做了一块MSP430F149的试验板,以前用下载线进行调试没有出现过问题,但是,最近我每次make后用下载线调试时,总是弹出一个窗口,给我提示:Could not find target ...

  8. JAXB - Hello World

    We'll stick with the tradition and use a sort of "Hello World" XML document to illustrate ...

  9. 通用安全字符串输入,彻底替换server.htmlencode

    Function HTMLEncode(Str) If Isnull(Str) Then HTMLEncode = "" Exit Function End If Str = Re ...

  10. HttpClient的使用

    HttpClient的使用 一.简介 HttpClient是Apache Jakarta Common下的子项目,用来提供高效的.最新的.功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTT ...