*HDU1251 字典树
统计难题
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 35639 Accepted Submission(s): 13322
注意:本题只有一组测试数据,处理到文件结束.
//动态字典树模板
#include<iostream>
#include<cstdio>
#include<cstring>
#include<malloc.h>
using namespace std;
struct Trie
{
int v;
Trie *next[]; //26个小写英文字母,v在这里保存到每个节点有几个单词,根据题意变化
};
Trie *root;
void Tinsert(char s[])
{
int len=strlen(s);
Trie *p=root,*q;
for(int i=;i<len;i++)
{
int id=s[i]-'a'; //换成编号
if(p->next[id]==NULL)
{
q=new Trie;
for(int j=;j<=;j++)
q->next[j]=NULL;
q->v=;
p->next[id]=q;
}
p=p->next[id];
p->v++;
}
}
int Tsearch(char s[])
{
int len=strlen(s);
Trie *p=root;
for(int i=;i<len;i++)
{
int id=s[i]-'a';
if(p->next[id]==NULL)
return ;
p=p->next[id];
}
return p->v;
}
void Tdelete(Trie *t) //动态字典树释放内存
{
if(t==NULL)
return;
for(int i=;i<;i++)
if(t->next[i]!=NULL)
Tdelete(t->next[i]);
free(t);
}
int main()
{
char ch[];
root=new Trie;
for(int i=;i<;i++)
root->next[i]=NULL;
root->v=;
while(gets(ch)&&ch[])
Tinsert(ch);
while(scanf("%s",ch)!=EOF)
printf("%d\n",Tsearch(ch));
Tdelete(root);
return ;
}
//仿照上面的动态字典树写的一个静态字典树
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int const MAX=; //树节点总数
int const CON=; //26个小写英文字母
int nod[MAX][CON],val[MAX]; //节点,节点权值
int sz; //记录节点序号
void init()
{
sz=;
memset(nod[],,sizeof(nod[]));
val[]=;
}
void insert(char s[])
{
int len=strlen(s);
int rt=;
for(int i=;i<len;i++)
{
int id=s[i]-'a';
if(nod[rt][id]==)
{
memset(nod[sz],,sizeof(nod[sz]));
nod[rt][id]=sz;
val[sz++]=;
}
rt=nod[rt][id];
val[rt]++;
}
}
int search(char s[])
{
int len=strlen(s);
int rt=;
for(int i=;i<len;i++)
{
int id=s[i]-'a';
if(nod[rt][id]==)
return ;
rt=nod[rt][id];
}
return val[rt];
}
int main()
{
char ch[];
init();
while(gets(ch)&&ch[])
insert(ch);
while(scanf("%s",ch)!=EOF)
printf("%d\n",search(ch));
return ;
}
*HDU1251 字典树的更多相关文章
- hdu1251+字典树常用模板
这里只简单给出几个常用的字典树的模板,要看具体介绍的请看:传送门 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现) ...
- hdu1251 字典树or map
一道字典树的题,不过看起来用map更为简单 传送门 题意: 给出一堆字符串构成一个字典,求字典里以某字符串为前缀的字符串有几个 思路: 输入字符串时把字符串的前缀全部存进map并标记次数 查询时直接输 ...
- hdu1251字典树递归算法
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU1251 字典树板子题
题意:中文题,统计以某字符串作为前缀的字符串个数 刚学字典树,理解起来十分简单,就是维护一个多叉树,这里用的是链表版本,后面就用的是数组版本了,个人更喜欢数组版本,这里的链表版本就因为 莫名其妙的错误 ...
- hdu1251 字典树trie 模板题
//字典树模板题.题意:给一个库,每次查询,求以之为前缀的单词数量. #include<iostream> #include<string> #include<vecto ...
- 统计难题---hdu1251字典树模板
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 node *head=(node*)malloc(sizeof(node)); for(int ...
- Trie 字典树,hdu1251
参考博客:https://www.cnblogs.com/TheRoadToTheGold/p/6290732.html 字典树就是单词树,顺着一条路径到达终止结点就形成一个单词,该单词的前缀包含在这 ...
- hdu1251(字典树)
统计难题(hdu1251) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Tota ...
- hdu1251 && hud 1247 (字典树)
hdu1251 题目 这道题,主要是在主函数的输入输出上犹豫了. #include<stdio.h> #include<cstring> #include<iostrea ...
随机推荐
- UVA-11991 Easy Problem from Rujia Liu?
Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for ...
- sublime3侧边栏颜色修改,推荐主题
sublime侧边栏的颜色默认是灰白色的,下面方法可以手动定制颜色为深色: 需要修改的文件为: C:\program\Sublime\Packages\Theme - Default.sublime- ...
- ACM-ICPC如何起步
刚刚绝定投身ACM-ICPC的同学先要过两关. 第一关:程序设计语言 如果学校有开设相关课程,则省去了很多麻烦.如果没有则可以选择<程序设计导引及在线实践>作为教材.现在的比赛中允许使用的 ...
- javascript 中的 bind (编辑中。。。。)
这篇文章说的非常好!http://my.oschina.net/blogshi/blog/265415 我的体会就是,函数中的this,指的是运行时,它是被哪个对象调用的.因为javascrpit的函 ...
- spring Quartz多个定时任务的配置
Quartz多个定时任务的配置 1,配置文件与spring整合,需要在spring 的总配置中一入或者在web.xml中spring监听中加上 ztc_cp-spring-quartz.xml 注:定 ...
- spring+IOC+DI+AOP优点分析(一)
Spring是什么: Spring是一个轻量级的DI和AOP容器框架. 说它轻量级有一大部分原因是相对与EJB的(虽然本人从没有接触过EJB的应用),重要的是,Spring是非侵入式的,基于sprin ...
- Spring+SpringMvc+Mybatis框架集成搭建教程二(依赖配置及框架整合)
依赖导入以及框架整合 (1).打开项目的pom.xml文件,声明依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" x ...
- [Java基础] Java中List.remove报错UnsupportedOperationException
Java中List.remove(removeRange,clear类似) 报出 UnsupportedOperationException 的错误.原来该List是一个AbstractList,不支 ...
- ios原声音频播放AVAudioSession 总结
//音频播放/*英译:record:录音 */ 1 导入头文件#import<AVFoundation/AVFoundation.h>//AVAudioSession是一个单例模式.在IO ...
- Servlet 单例多线程
Servlet如何处理多个请求访问? Servlet容器默认是采用单实例多线程的方式处理多个请求的: 1.当web服务器启动的时候(或客户端发送请求到服务器时),Servlet就被加载并实例化(只存在 ...