统计难题

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. Android网络开发之OkHttp--基本用法POST

    1.OkHttp框架使用了OkIo框架,不要忘记下OkIo.jar 2.通过POST访问网络,和通过GET访问网络基本相同,多了设置请求参数的过程.主要分为五步: (1).声明并实例化一个OkHttp ...

  2. JavaScript运行原理解析

    原文:1.http://blog.csdn.net/liaodehong/article/details/50488098 2.Stack的三种含义 (阮一峰) 3. http://lib.csdn. ...

  3. Android JNI入门第六篇——C调用Java

    本篇将介绍在JNI编程中C调用Java实现. 源码下载地址:http://download.csdn.net/detail/xyz_lmn/4868265 关键代码: java: public cla ...

  4. 微信小程序实例教程(三)

    第七章:微信小程序编辑名片页面开发   编辑名片有两条路径,分为新增名片流程与修改名片流程. 用户手填新增名片流程:   首先跳转到我们的新增名片页面 1 需要传递用户的当前 userId,wx.na ...

  5. iframe载入等待

    <style> #pageloading{position:absolute; left:0px; top:0px;background:white url('../images/load ...

  6. Ubuntu Server 14.04 & Apache2.4 虚拟主机、模块重写、隐藏入口文件配置

    环境: Ubuntu Server 14.04 , Apache2.4 一.Apache2.4 虚拟主机配置 01. 新建一份配置文件 在apache2.4中,虚拟主机的目录是通过/etc/apach ...

  7. PHP 领域逻辑与数据库映射

    http://blog.csdn.net/hguisu/article/details/7569968

  8. js MD5加密后的字符串

    js MD5加密后的字符串 <script language="JavaScript"> /************************************** ...

  9. Cocos2dx 学习笔记整理----场景切换

    据说Cocos2dx场景切换的方法有32种:cocos2dx 常见的32种切换场景的动画 无需一一求证,只需要知道切换场景需要怎么做就行了. 作为导演CCDirector,切换场景的事情当然归它管了. ...

  10. Ubuntu和win10双系统Grup无法引导解决方案

    通常我们经常安装双系统, 但是有时候安装完系统无法正常引导, 以下就说明Ubuntu和win10双系统, win10在grub界面不断循环的解决方案 直接在win10启动项目上按e进入编辑模式 在文档 ...