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. ...
随机推荐
- thinkphp的nginx配置
thinkphp的nginx配置 server { listen 80; server_name www.abc.com; #charset utf-8; access_log /var/www/ww ...
- C# windows窗体程序打包安装及卸载
一.新建安装部署项目
- ASP.NET 查询客户端请求IP地址
public class CheckIP { #region 获取浏览器版本号 /// <summary> /// 获 ...
- oracle_执行计划_谓词信息和数据获取(access and filter区别) (转)
These two terms in the Predicate Information section indicate when the data source is reduced. Simpl ...
- 【HDOJ】4983 Goffi and GCD
题意说的非常清楚,即求满足gcd(n-a, n)*gcd(n-b, n) = n^k的(a, b)的不同对数.显然gcd(n-a, n)<=n, gcd(n-b, n)<=n.因此当n不为 ...
- 【转】android开发 dts、各种接口porting----不错
原文网址:http://www.xuebuyuan.com/1023185.html 1. repo init -u git://review.sonyericsson.net/platform/ma ...
- Merge Sorted Array——LeetCode
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- Java---常用基础面试知识点
综合网上的一点资源,给大家整理了一些Java常用的基础面试知识点,希望能帮助到刚开始学习或正在学习的学员. 1.抽象 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方 ...
- OAuth2的学习小结
Spring Security json Access maven OAUTH2核心参数说明 grant_type参数说明表格: grant_type 说明 authorization_code 标准 ...
- 推送消息 相关公司 手机端分享http://mob.com/
信鸽 http://xg.qq.com/xg/pro/ctr_message 云巴 http://yunba.io/usercases/ 极光https://www.jpush.cn/ 手机端分享ht ...