HDU 1251统计难题 字典树
字典树的应用。
数据结构第一次课的作业竟然就需要用到树了!!!这不科学啊。赶紧来熟悉一下字典树。
空间开销太大T T
#include<cstdio>
#include<cstring>
const int MAXN=26;
struct Trie
{
Trie *next[MAXN];
int v;
}; Trie *root=new Trie(); void creatTrie(char *s)
{
int len=strlen(s);
Trie *p=root,*q;
for(int i=0;i<len;i++)
{
int index=s[i]-'a';
if(p->next[index]==NULL)
{
q=new Trie();
q->v=1;
p->next[index]=q;
p=q;
}
else
{
p->next[index]->v++;
p=p->next[index];
}
}
} int findTrie(char *s)
{
int len=strlen(s);
Trie *p=root;
for(int i=0;i<len;i++)
{
int index=s[i]-'a';
p=p->next[index];
if(p==NULL)
return 0;
}
return p->v;
} int main()
{
char temp[12];
while(gets(temp)&&temp[0]!='\0')
{
creatTrie(temp);
} while(scanf("%s",temp)!=EOF)
{
printf("%d\n",findTrie(temp));
} }
HDU 1251统计难题 字典树的更多相关文章
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- hdu 1251 统计难题 字典树第一题。
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- hdu 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU 1251 统计难题 字典树大水题
今天刚看的字典树, 就RE了一发, 字典树原理还是很简单的, 唯一的问题就是不知道一维够不够用, 就开的贼大, 这真的是容易MLE的东西啊, 赶紧去学优化吧. HDU-1251 统计难题 这道题唯一的 ...
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
- HDU 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- hdu -1251 统计难题(字典树水题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251 建树之后 查询即可. G++提交 ME不知道为什么,c++就对了. #include <iostre ...
- hdoj 1251 统计难题(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 思路分析:该问题要求求出以某个字符串为前缀的单词数目,通过使用字典树,在字典树中添加count记 ...
随机推荐
- 暑假集训-二分图,网络流,2-SAT
匈牙利算法DFS bool dfs(int u){ ; i <= n; i++){ if(a[u][i] && !visit[i]){ visit[i] = true; || d ...
- vue中computed与watch的异同
一.computed 和 watch 都可以观察页面的数据变化.当处理页面的数据变化时,我们有时候很容易滥用watch. 而通常更好的办法是使用computed属性,而不是命令是的watch回调. ...
- PHP生成RSS报
<?php$sql="select * from wx_zimi ";$res=$dbs->query($sql);$arr=array();while($o=$dbs ...
- PHP和JSON
PHP和JSON 一.总结 1.php中json的使用方法:php中json的使用超级简单啦,主要是两个函数json_encode(编码)和json_decode(解码),像md5加密 2.json的 ...
- 1.22 Python基础知识 - 正则表达式
Python正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. re ...
- Winform 获取相对路径 C#
///获取相对路径 ///例如:System.Windows.Forms.Application.StartupPath = "E:\App\CheckingMachine\QueryMac ...
- 仙人掌的同构(hash)
关于仙人掌的同构,主要是我太蒟蒻了QAQ,问了好几位大佬才弄好. 手撕仙人掌,你得先有手套 ,你得先了解以下基本知识 a.点双连通分量,没什么好说得,仙人掌上有环,判环用点双 b.树的hash点这里 ...
- C# Bartender模板打印 条码,二维码, 文字, 及操作RFID标签等。
1.在之前写的一篇文章中, 有讲到如何利用ZPL命令去操作打印里, 后面发现通过模板的方式会更加方便快捷, 既不用去掌握ZPL的实现细节, 就可以轻松的调用实现打印的功能. 解决方案: 1.网络下载 ...
- [Javascript AST] 1. Continue: Write a simple Babel plugin
We want to write a Babel Plugin, which move 'const versionRegex = /(/d+)\.(/d+)\.(/d+)/gi' out of fu ...
- android对话框(Dialog)的使用方法
Activities提供了一种方便管理的创建.保存.回复的对话框机制.比如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...