HDU 1251 统计难题(字典树模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251
题意:
给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量。
思路:
字典树入门题。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = ; int num = ; struct Trie
{
int son[];
int cnt; //前缀数量
int ends; //单词数量,在本题中其实并没有用到
}t[maxn]; void init(int x)
{
t[x].ends = ;
t[x].cnt = ;
memset(t[x].son,,sizeof(t[x].son));
} void insert(char* s)
{
int u = , n = strlen(s);
for(int i=;i<n;i++)
{
int c = s[i]-'a';
if(!t[u].son[c])
{
num++;
init(num);
t[u].son[c] = num;
}
u = t[u].son[c];
t[u].cnt++;
}
t[u].ends++;
} int query(char* s)
{
int u = , n = strlen(s);
for(int i=;i<n;i++)
{
int c = s[i]-'a';
if(t[u].son[c] == ) return ;
u = t[u].son[c];
}
return t[u].cnt;
} char s[]; int main()
{
while(gets(s) && strcmp(s,"")!=) insert(s);
while(scanf("%s",s)!=EOF) printf("%d\n",query(s));
return ;
}
HDU 1251 统计难题(字典树模板题)的更多相关文章
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- hdu 1251 统计难题 字典树第一题。
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU 1251 统计难题 字典树大水题
今天刚看的字典树, 就RE了一发, 字典树原理还是很简单的, 唯一的问题就是不知道一维够不够用, 就开的贼大, 这真的是容易MLE的东西啊, 赶紧去学优化吧. HDU-1251 统计难题 这道题唯一的 ...
- hdu -1251 统计难题(字典树水题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251 建树之后 查询即可. G++提交 ME不知道为什么,c++就对了. #include <iostre ...
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
- HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- hdu 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU 1251统计难题 字典树
字典树的应用. 数据结构第一次课的作业竟然就需要用到树了!!!这不科学啊.赶紧来熟悉一下字典树. 空间开销太大T T #include<cstdio> #include<cstrin ...
- HDU 1251 统计难题(Trie模版题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
随机推荐
- 转:MD5辅助类
public class MD5Helper { private static MD5 md5 = new MD5CryptoServiceProvider(); private static str ...
- LoggerFactory.getLogger用法
使用指定类初始化日志对象,在日志输出的时候,可以打印出日志信息所在类 如:Logger logger = LoggerFactory.getLogger(com.lz.Test.class); ...
- The Little Prince-12/02
The Little Prince-12/02 What moves me so deeply, about this little prince who is sleeping here, is h ...
- BATJ等大厂最全经典面试题分享
金九银十,又到了面试求职高峰期,最近有很多网友都在求大厂面试题.正好我之前电脑里面有这方面的整理,于是就发上来分享给大家. 这些题目是网友去百度.蚂蚁金服.小米.乐视.美团.58.猎豹.360.新浪. ...
- DOS下读取smbios的汇编程序(通过搜索memory)
汇编程序编写的读取smbios的代码: ;------------------------------------------------- ;功能: 读取SMBIOS 的Entry Point ,并 ...
- python的os模块中的os.walk()函数
os.walk('path')函数对于每个目录返回一个三元组,(dirpath, dirnames, filenames), 第一个是路径,第二个是路径下面的目录,第三个是路径下面的文件 如果加参数t ...
- 模拟实现ATM+购物商城程序
流程图: 需求: ATM:模拟实现一个ATM + 购物商城程序额度 15000或自定义实现购物商城,买东西加入 购物车,调用信用卡接口结账可以提现,手续费5%支持多账户登录支持账户间转账记录每月日常消 ...
- C++max的使用方法
#include <iostream> //#include <algorithm>//std::min std::max #include <stdint.h> ...
- topcoder srm 415 div1
problem1 link 每次贪心地从crans由大到小地找到一个能搬得动地尽量大地box即可. problem2 link 首先,$hava$可以全部换成钱,然后就是找到一个最小的钱减去自己已有的 ...
- topcoder srm 715 div1 -23
1.一个计算器,它执行的是一个只包含‘+’,‘-’的字符串$s$.初始化值为0,每遇到一个‘+’增加1,否则减少1.并保存运算过程的最大最小值$Max,Min$,最后的答案是$Max-Min$.比如$ ...