/*
这题倒是没啥难度 字典树可搞
但是吧 空间是个问题
开始写成这样
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 难题统计的更多相关文章

  1. HDU 1251:统计难题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意不难理解,就是先输入不知道多少个字符串,然后用一个空行结束这个输入,然后接下来不知道多少行再 ...

  2. 讲解——Trie树(字典树)

          Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看以下几个题: 1.给出n个单词和m个询问,每次询问一个单词,回答这个单词是否在单 ...

  3. Trie树(转)

    原文http://www.cnblogs.com/TheRoadToTheGold/p/6290732.html 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看 ...

  4. 浅谈Trie树(字典树)

          Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看以下几个题: 1.给出n个单词和m个询问,每次询问一个单词,回答这个单词是否在单 ...

  5. [转] 浅谈Trie树(字典树)

    原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6290732.html Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找 ...

  6. (转)浅谈trie树

    浅谈Trie树(字典树)         Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看以下几个题: 1.给出n个单词和m个询问,每次询问 ...

  7. 字典树模板( 指针版 && 数组版 )

    模板 :  #include<string.h> #include<stdio.h> #include<malloc.h> #include<iostream ...

  8. hduoj 1251 统计难题

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

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

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

随机推荐

  1. 交叉编译:cannot find /lib/libc.so.6 collect2: ld returned 1 exit status

    1.有时候明明指定了交叉编译的动态库搜索路径,但有些库提示还是搜索不到,而且提示的搜索路径有点奇怪,不是指定的路径,比如: /opt/mips-4.4/bin/../lib/gcc/mips-linu ...

  2. 制作chm格式的帮助文档

    学习java的人都用过jdk帮助文档,借助工具我们也可以自己生成chm格式的帮助文档, 原文:http://www.cnblogs.com/shenliang123/archive/2012/04/2 ...

  3. jquery mobile 主题

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  4. 探究Android中Listview显示错乱问题

    问题 最近在项目中遇到过一个很棘手的问题,就是ListView在滑动后就莫名其妙的显示错乱,网上查阅资料后问题很容易的就解决了,但是对于问题产生的原因仍是一知半解,所以不甘心的我定下心来,狠读源码,终 ...

  5. [Struts2学习笔记] -- 简单的类型转换

    接下来学习一下Struts2简单的类型转换,Struts2基于ognl.jar实现了简单类型的数据转换.比如jsp页面中的form值与字段值的转换,下面写一个例子. 1.创建一个jsp页面,编写一个f ...

  6. [转载]mininet的安装和使用

    http://blog.csdn.net/neterpaole/article/details/8512106 最近在搞controller+mininet的openflow环境模拟,搞得不是很顺利, ...

  7. 如何修正Feedly文章中文標題亂碼或無法正常顯示的問題

    在7月1日Google關閉Reader之前,我想應該有許多人都已經從Google Reader移到其他服務上了,其中受益最大的者莫過於Feedly了,一下子就吸收了幾百萬的用戶,而我也是其中之一,由於 ...

  8. Xamarin Studio –Project not built in active configuration

    当我们加载项目以后如果出现以下项目提示 处理方式如下: 解决方案右键->options 配置->configuration mappings->勾选构建的ios项目 项目右键-> ...

  9. 7.3.1 Establishing a Backup Policy

    7.3 Example Backup and Recovery Strategy 备份和恢复策略实例 7.3.1 Establishing a Backup Policy 7.3.2 Using Ba ...

  10. BZOJ1711: [Usaco2007 Open]Dingin吃饭

    1711: [Usaco2007 Open]Dingin吃饭 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 508  Solved: 259[Submit ...