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. ...
随机推荐
- PS纯手式打造不规则颜色的规则方块—学习教程
- 『重构--改善既有代码的设计』读书笔记----Inline Temp
与Inline Method相同,有时候犹豫需要Extract Method,需要对一些临时变量进行内联,而这个往往是Replace Temp with Query的一部分.简单来说,当你看到这种 d ...
- 高效jQuery
1.缓存变量 DOM遍历是昂贵的,所以尽量将会重用的元素缓存. // 糟糕 h = $('#element').height(); $('#element').css('height',h-20); ...
- EclipsePHP Studio 常用设置笔记
工作需要,学习PHP使用EclipsePHP Studio开发工具, 习惯整理下常用的使用设置,分享一下吧: 1.窗口-首选项-常规-工作空间,把文本文件编码改为utf8,以后再新建文件就默认是utf ...
- python运维开发之第十一天(RabbitMQ,redis)
一.RabbitMQ python的Queue与RabbitMQ之间的理解: python的进程或线程Queue只能python自己用.RabbitMQ队列多个应用之间共享队列,互相通信. 1.简单的 ...
- C++进阶阅读
推荐的阅读顺序:level 1从<<essential c++>>开始,短小精悍,可以对c++能进一步了解其特性以<<c++ primer>>作字典和课 ...
- JDBC操作TimesTen
无论是操作本地的还是远程的TimesTen服务,客户端都需要安装Tiems Client,并配置客户端DSN. 基本信息如下: 1:创建客户端DSN 点击“添加”: 双击“TimesTen Clien ...
- 利用readwritelock简单模拟实现多线程下cache的系统
package cn.lyy.hibernate.many2one; import java.util.HashMap; import java.util.Map; import java.util. ...
- android:ListView的局部刷新
1.简介 对于android中的ListView刷新机制,大多数的程序员都是很熟悉的,修改或者添加adapter中的数据源之后,然后调用notifyDataSetChanged()刷新ListView ...
- Binary Tree Inorder Traversa
package cn.edu.xidian.sselab.hashtable; import java.util.ArrayList;import java.util.List;import ja ...