字典树 HDU 1251 统计难题
http://acm.hdu.edu.cn/showproblem.php?pid=1251 这是重写的,让我感觉到每一次的理解程度都在增加
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
struct node
{
int sum;
node *next[];
node()//初始化数据
{
memset(next, NULL, sizeof(next));
sum=;
}
};
node *head=new node();//用C++的new动态申请内存其实delete和new是一对儿哦
node *now=(node *)malloc(sizeof(node));//用C语言动态申请内存
void buildtiretree(char *s)//建立字典数
{
node *p=new node();
p=head;
for(int i=; s[i]; i++)
{
int k=s[i]-'a';
if(p->next[k]==NULL)//如果p->next[k]为空
p->next[k]=new node();//就动态申请一个内存
now=p->next[k];
now->sum++;
p=now;
/*也可以这样写,其实就是第一个,也就是head不存任何东西
p=p->next[k];
p->sum++;
*/
}
}
int query(char *s)//查询单词
{
node *p=new node();
p=head;
//node *p=head;
for(int i=; s[i]; i++)
{
int k=s[i]-'a';
if(p->next[k]==NULL)
return;
p=p->next[k];
}
return p->sum;
}
void Free(node *head)//释放内存
{
int i;
if(head==NULL)
return;
for(i=; i<; i++)
{
if(head->next[i]!=NULL)
Free(head->next[i]);
}
free(head);
head=NULL;
}
int main()
{
char s[];
while(gets(s), s[])
buildtiretree(s);
while(cin >> s)
printf("%d\n", query(s));
Free(head);
return;
} 之前写的
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
struct node
{
int sum;
node *next[];
};
void buildtrietree(node *head, char s[])
{
node *p=new node();
p=head;
for(int i=; s[i]; i++)
{
int k=s[i]-'a';
if(p->next[k]==)
p->next[k]=new node();
p=p->next[k];
p->sum++;
}
} int query(node *head, char s[])
{
node *p=new node();
p=head;
for(int i=; s[i]; i++)
{
int k=s[i]-'a';
if(p->next[k]==)
return;
p=p->next[k];
}
return p->sum;
}
int main()
{
char s[];
node *head=new node();
while(gets(s), s[])
buildtrietree(head, s);
while(cin >> s)
printf("%d\n", query(head, s));
return;
}
字典树 HDU 1251 统计难题的更多相关文章
- hdu 1251 统计难题(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others) M ...
- HDU 1251 统计难题 (Trie)
pid=1251">统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/ ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- HDU 1251 统计难题(字典树 裸题 链表做法)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- hdu 1251:统计难题(字典树,经典题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- [ACM] hdu 1251 统计难题 (字典树)
统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...
- HDU 1251 统计难题(字典树模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...
- HDU 1251 统计难题(字典树入门模板题 很重要)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
随机推荐
- 浅谈"壳"(一)
壳,即坚硬的外皮,当壳的厚度与其曲面率半径的比值小于0.5时.称为"薄壳".反之称为"厚壳".由壳演化来的胸甲,盾牌. 在计算机这个注重创意又不失从文化科技中汲 ...
- Redis的订阅发布
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ServiceS ...
- 安卓TabHost+ViewPager+RadioGroup多功能模板整理
如今安卓比較流行的布局就是类似新闻client和手机QQ那种的底端可选择,上面的个别页面能够滑动选择. 在測试过程中发现用安卓自带的TabHost去构建.非常难得到自己定义的效果. 因此採用TabHo ...
- hive beeline 的server启动与连接
启动hiveServer2 启动beeline之后 连接 !connect jdbc:hive2://localhost:10000/default 启动的时候连接 /beeline -u jdbc: ...
- Google Code Jam 2014 Round 1 A:Problem C. Proper Shuffle
Problem A permutation of size N is a sequence of N numbers, each between 0 and N-1, where each numbe ...
- xshell 连接腾讯服务器
1.先关机, 创建秘钥,再绑定主机,下载秘钥保存下来 2. 填写好主机好和端口 3 4.导入刚才下载的文件 记住用户名是ubuntu 不是root!!
- 一、任天堂ns (Nintendo Switch) 上手
公司不方便回家详解做个博客非专业评测~
- PerconaXtraBackup-2.2.8手册翻译
1.1.2 Percona Xtrabackup特性 * 不停机创建Innodb数据库热备 * 对Mysql数据库创建增量备份 * 压缩数据流方式备份到异地服务器 * 更加便捷创建新的mysql从库 ...
- C语言基础知识【指针】
2017年7月11日18:33:41C指针 该看地址:http://www.runoob.com/cprogramming/c-pointers.html1.学习 C 语言的指针既简单又有趣.通过指 ...
- Http权威指南学习研究
学习时间: 该学习:第六章 6.6小节 加油 185页 2017年5月15日15:13:00 今天任务: 看完前两章节: ...