字典树 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 ...
随机推荐
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- Cygwin 版本的 Curl 安装,提取,使用笔记
Cygwin 版本的 Curl 安装,提取,使用笔记 Cygwin 版本的 Curl 使其恢复 HTTPS 请求功能Cygwin 版本的 Curl 依赖的 DLL 清单提取 Cygwin 版本的 Cu ...
- iOS iPhoneX/iPhoneXS/iPhoneXR/iPhoneXS Max系列适配
以前异性屏只有一款iPhoneX,所以在适配的时候直接判断高度是否等于812即可判断是否是iPhoneX #define IS_IPHONE_X (IS_IPHONE && SCREE ...
- inline用于替代宏函数
在C&C++中 一.inline关键字用来定义一个类的内联函数,引入它的主要原因是用它替代C中表达式形式的宏定义. 表达式形式的宏定义一例: #define ExpressionName(Va ...
- Java学习篇之---json-lib(Maven)
json-lib(Maven) java中用于解释json的主流工具有org.json.json-lib与gson.本篇文章介绍json-lib. 项目中要用到json-lib.在pom.xml文件里 ...
- 【hiho一下】第一周 最长回文子串
题目1:最长回文子串 题目原文:http://hihocoder.com/contest/hiho1/problem/1 [题目解读] 题目与 POJ 3974 palindrome 基本同样.求解最 ...
- MUI 清除缓存
mui 清除但是在ios和安卓稍微有点区别, ios可以清除的很彻底,下载文件也能删除: 安卓能清理缓存,但是不能删除下载的文件: plus.cache.calculate(function(size ...
- Coursera machine learning 第二周 quiz 答案 Octave/Matlab Tutorial
https://www.coursera.org/learn/machine-learning/exam/dbM1J/octave-matlab-tutorial Octave Tutorial 5 ...
- MySQL复制经常使用拓扑结构具体解释
复制的体系结构有下面一些基本原则: (1) 每一个slave仅仅能有一个master: (2) 每一个slave仅仅能有一个唯一的serverID: (3) 每一个master能够有 ...
- linux c编程:系统数据文件和信息
linux系统相关的文件信息包含在/etc/passwd文件和/etc/group中.每次登录linux系统以及每次执行ls -l命令时都要使用口令文件.这些字段都包含在<pwd.h>中定 ...