统计难题

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 36637    Accepted Submission(s):
13622

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

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

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

ba
b
band
abc

 
Sample Output
2
3
1
0
 
Author
Ignatius.L
 
Recommend
Ignatius.L   |   We have carefully selected several
similar problems for you:  1075 1247 1671 1298 1800 
 
思路:
  裸tire树;
 
来,上代码:

#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; struct TreeNode {
short num;
bool if_[];
struct TreeNode *word[];
}; class TrieTreeType {
private:
struct TreeNode *null; public:
struct TreeNode *root; TrieTreeType()
{
null=new TreeNode;
null->num=;
for(int i=;i<=;i++) null->if_[i]=false;
for(int i=;i<=;i++) null->word[i]=null;
root=new TreeNode;
root->num=;
for(int i=;i<=;i++) root->if_[i]=false;
for(int i=;i<=;i++) root->word[i]=null;
} inline void Insert(char str[])
{
TreeNode *now=root;
now->num++;
int len=strlen(str);
for(int v=;v<len;v++)
{
if(!now->if_[str[v]-'a'])
{
now->word[str[v]-'a']=new TreeNode;
now->word[str[v]-'a']->num=;
for(int i=;i<=;i++) now->word[str[v]-'a']->if_[i]=false;
for(int i=;i<=;i++) now->word[str[v]-'a']->word[i]=null;
now->if_[str[v]-'a']=true;
}
now=now->word[str[v]-'a'];
now->num++;
}
} inline int Size(char str[])
{
TreeNode *now=root;
int len=strlen(str);
for(int v=;v<len;v++)
{
if(now->word[str[v]-'a']==null) return ;
now=now->word[str[v]-'a'];
}
return now->num;
}
};
class TrieTreeType tree; int len_sen,n; char sen[]; bool if_c=false; int main()
{
while(gets(sen))
{
if(strlen(sen)==)
{
if_c=true;
continue;
}
if(if_c) printf("%d\n",tree.Size(sen));
else tree.Insert(sen);
}
return ;
}

AC日记——统计难题 hdu 1251的更多相关文章

  1. 字典树模板题(统计难题 HDU - 1251)

    https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...

  2. 统计难题 HDOJ --1251

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

  3. AC日记——统计单词数 openjudge 1.12 5

    05:统计单词数 总时间限制:  1000ms 内存限制:  65536kB 描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次 ...

  4. AC日记——统计数字字符个数 openjudge 1.7 01

    01:统计数字字符个数 总时间限制:  1000ms 内存限制:  65536kB 描述 输入一行字符,统计出其中数字字符的个数. 输入 一行字符串,总长度不超过255. 输出 输出为1行,输出字符串 ...

  5. AC日记——Keywords Search hdu 2222

    2222 思路: ac自动机模板题: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ...

  6. AC日记——Number Sequence hdu 1711

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. AC日记——病毒侵袭 hdu 2896

    2896 思路: 好题: 代码: #include <queue> #include <cstdio> #include <cstring> using names ...

  8. AC日记——Paint Pearls hdu 5009

    Paint Pearls 思路: 离散化+dp+剪枝: dp是个n方的做法: 重要就在剪枝: 如果一个长度为n的区间,有大于根号n种颜色,还不如一个一个涂: 来,上代码: #include <c ...

  9. AC日记——Super Mario hdu 4417

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. FusionChart实现柱状图、饼状图的动态数据显示 附Demo

    最近做的项目中需要用饼状图显示——'问卷调查'的统计结果(之前用过FusionChart做过柱状图的数据展示,那还是两年前的事了),在网上查了下FusionChart实现饼状图显示方面的资料,却发现资 ...

  2. 2016青岛网络赛 Sort

    Sort Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Des ...

  3. article标签

    <html><head lang="en"> <meta charset="UTF-8"> <title>富士康 ...

  4. 怎样让外界无法改变自定义view的尺寸大小

    重写setFrame和setBounds方法即可. + (instancetype)testView { return [[self alloc] init]; } - (void)setFrame: ...

  5. [转]httpclient 上传文件、下载文件

    用httpclient4.3 post方式推送文件到服务端  准备:httpclient-4.3.3.jar:httpcore-4.3.2.jar:httpmime-4.3.3.jar/** * 上传 ...

  6. CentOS新增开机启动项

    编辑rc.local文件 vi /etc/rc.d/rc.local 例如 #!/bin/sh # # This script will be executed *after* all the oth ...

  7. [转] WebService开发笔记 1 -- 利用cxf开发WebService竟然如此简单

    以下文章来自   http://www.blogjava.net/jacally/articles/186655.html 现在的项目中需要用到SOA概念的地方越来越多,最近我接手的一个项目中就提出了 ...

  8. Javascript Jquery 中的数组定义与操作_子木玲_新浪博客

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  9. Linux系统的/proc目录

    1. /proc目录 Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机 制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它 ...

  10. dedecms标签使用

    关键描述调用标签: <meta name="keywords" content="{dede:field name='keywords'/}">&l ...