HUD 1251 难题统计
/*
这题倒是没啥难度 字典树可搞
但是吧 空间是个问题
开始写成这样
struct node
{
int next[27],sum[27];
bool over;
}t[maxn];
死活过不了 开小了er 开大了MLE
问了问wmy 很机智的说用map 管用的 然后卡空间过了
看他们用指针动态分配内存 然而我并不太会.....
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#define maxn 480010
using namespace std;
int topt;
char s[];
struct node
{
map<int,int>next,sum;
bool over;
}t[maxn];
void Add_tree()
{
int now=,l=strlen(s);
for(int i=;i<l;i++)
{
int x=s[i]-'a'+;
if(t[now].next[x])
{
t[now].sum[x]++;
now=t[now].next[x];
}
else
{
topt++;t[now].next[x]=topt;
t[now].sum[x]++;now=topt;
}
}
}
int find_tree()
{
int ans=,p=,now=,l=strlen(s),x,pre;
while(p<=l-)
{
x=s[p]-'a'+;
if(t[now].next[x])pre=now,now=t[now].next[x],p++;
else return ;
}
return t[pre].sum[x];
}
int main()
{
while(gets(s)&&strlen(s))Add_tree();
while(gets(s))cout<<find_tree()<<endl;
return ;
}
/*
恩 之前一直纠结空间
因为状态写错了 可以不用那么多....
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#define maxn 500010
using namespace std;
int topt;
char s[];
struct node
{
int next[],sum;
}t[maxn];
void Add_tree()
{
int now=,l=strlen(s);
for(int i=;i<l;i++)
{
int x=s[i]-'a'+;
if(t[now].next[x])now=t[now].next[x];
else
{
topt++;t[now].next[x]=topt;now=topt;
}
t[now].sum++;
}
}
int find_tree()
{
int ans=,p=,now=,l=strlen(s);
while(p<=l-)
{
int x=s[p]-'a'+;
if(t[now].next[x])now=t[now].next[x],p++;
else return ;
}
return t[now].sum;
}
int main()
{
while(gets(s)&&strlen(s))Add_tree();
while(gets(s))cout<<find_tree()<<endl;
return ;
}
HUD 1251 难题统计的更多相关文章
- HDU 1251:统计难题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意不难理解,就是先输入不知道多少个字符串,然后用一个空行结束这个输入,然后接下来不知道多少行再 ...
- 讲解——Trie树(字典树)
Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看以下几个题: 1.给出n个单词和m个询问,每次询问一个单词,回答这个单词是否在单 ...
- Trie树(转)
原文http://www.cnblogs.com/TheRoadToTheGold/p/6290732.html 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看 ...
- 浅谈Trie树(字典树)
Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看以下几个题: 1.给出n个单词和m个询问,每次询问一个单词,回答这个单词是否在单 ...
- [转] 浅谈Trie树(字典树)
原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6290732.html Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找 ...
- (转)浅谈trie树
浅谈Trie树(字典树) Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看以下几个题: 1.给出n个单词和m个询问,每次询问 ...
- 字典树模板( 指针版 && 数组版 )
模板 : #include<string.h> #include<stdio.h> #include<malloc.h> #include<iostream ...
- hduoj 1251 统计难题
http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
随机推荐
- nginx配置无效的问题
今天修改nginx的一个配置文件,却怎么都没效果,发现是启动nginx指定的配置文件不一样. #ps aux|grep nginx root 17672 0.0 0.0 45856 2 ...
- JS判断鼠标从哪个方向进入DIV容器
写的不够高大上 , 不要介意哦... Js: //进去 $(".flash").bind("mouseenter",function(e){ /** the w ...
- 关于Chrome(谷歌浏览器)对docume,准确获取网页客户区的宽高、滚动条宽高、滚动条Left和Top
对于document.compatMode,很多朋友可能都根我一样很少接触,知道他的存在却不清楚他的用途.今天在ext中看到 document.compatMode的使用,感觉这个对于我们开发兼容性的 ...
- SCJP_104——题目分析(3)
11. what is reserved words in java?A. run B. default C. implement D. import Java 中,给标识符取名的时候,不能使用关键字 ...
- 查询一周最近一周的数据,date String 随意转换,更有疯狂的排序
--查看一周随访的详情70007PROCEDURE GET_PATIENT_WEEKFU ( userId in NUMERIC, time in date, V_LIST OUT MYCURSOR ...
- Scut:脚本引擎
Scut 可以执行 C#.Python.Lua 三种类型的脚步,Scut 是如何加载并传递参数的呢? 首先值得注意的是:Scut 在编译时就会将逻辑层脚本源码复制到bin/Script的目录下. 1. ...
- 利用Keil u4调试,精确实现软件延时
用定时器延时,有时候显得有点麻烦,我们不如考虑软件精确延时,软件延时无非就是利用for或while多重循环.以前用到延时函数时,都是从网上下载别人写好的延时子程序.延时5ms,400ms,1s,……, ...
- latch:library cache
一:硬解析造成的shared pool latch 争用: 每一个sql被执行之前,先要到library cache中根据hash_value查找parent cursor,这就需要先获得librar ...
- 源代码管理工具TFS2013安装与使用
最近公司新开发一个项目要用微软的TFS2013进行项目的源代码管理,以前只是用过SVN,从来没有用过TFS,所以在网上百度.谷歌了好一阵子来查看怎么安装和配置,还好花了一天时间总算是初步的搞定了,下面 ...
- Minimum Depth of Binary Tree ——LeetCode
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...