字典树 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 ...
随机推荐
- nightwatch-js ----并发运行
从v0.5开始nightwatch支持并发测试.通过在命令行中指定多个环境来工作,用逗号分隔.例如: $ nightwatch -e default,chrome 这样可以在多个相同或是不同的浏览器上 ...
- Android网络框架Volley
Volley是Google I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...
- CentOS LVS安装配置
一般2.6.10以上内核版本都已经自带了ipvsadm,故不需要安装. Ipvs 1.25编译 ipvsadm-1.25编译不过 去掉netlink库的依赖:去掉libipvs/Makefile的CF ...
- Java学习第一步——JDK安装及Java环境变量配置
Java作为当下很主流的编程语言,学习Java的朋友也越来越多了,作为一门面向对象的编程语言,Java也有着安全.高 效等诸多有点.从TIOBE(TIOBE排行榜是根据互联网上有经验的程序员.课程和第 ...
- HTML5 2D平台游戏开发#7Camera
在庞大的游戏世界中,玩家不能一览地图全貌,而是只能看到其中一部分,并一步步探索,这时就要用到一种技术来显示局部的地图,游戏术语称为摄像机(Camera).下面两张图中的白色矩形框表示了Camera的作 ...
- Exception in thread "main" java.util.ConcurrentModificationException
package test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public c ...
- PMD:Java源代码扫描器
PMD是一个开源代码分析器.可以查找常见编程缺陷,比如未使用的变量.空catch代码块.不必要的对象创建等.支持Java.JavaScript.PLSQL.Apache Velocity.XML.XS ...
- C# 中安全代码与不安全代码
C# 中安全代码与不安全代码 P/Invoke 非托管代码需要在unsafe块中书写. using System; using System.Collections.Generic; using Sy ...
- 网页上10秒倒计时,完了后就自动跳转到另一个网页上html代码
用html代码的话就这样: <meta http-equiv="Refresh" content="10;URL=http://www.baidu.com" ...
- phpstudy配置php7.1.11 + phpstudy nginx伪静态
切记要把新的php版本配到环境变量,cmd才会生效 php7.1.11下载地址 http://windows.php.net/download/ 下载之后,解压. 重名的为php-7.1.11-nts ...