*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 ...
随机推荐
- 如何解决mathpage.dll或MathType.dll文件找不到问题
解决方法(具体图文教程): 步骤一 要确保路径被office信任.依次打开word->文件->选项->信任中心->信任中心设置->添加新位置,添加C:\Program F ...
- Excel函数——DATE、SUBSTITUTE、REPLACE、ISERROR、IFERROR
1.DATE DATE 函数返回表示特定日期的连续序列号.例如,公式 =DATE(2008,7,8) 返回 2008-7-8或39637,取决于单元格格式,但空单元格计算和默认为日期格式. DATE也 ...
- JAVA关键字与保留字说明及使用
1.abstract 2.boolean 3.break 4.byte 5.case 6.catch 7.char 8.class 9.continue 10.default 11.do 12.dou ...
- sql中case when语句的使用-来自网摘文章
Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...
- Effective C++ 33 避免遮掩继承而来的名称
首先介绍一个原则LSP(Liskov Substitution Principle),如果Class D以Public方式继承Class B,则所有B对象可以派上用场的任何地方,D对象一样可以派上用场 ...
- 关于CDN的认识
传统的未加缓存服务的访问过程: 用户提交域名→浏览器对域名进行解释→得到目的主机的IP地址→根据IP地址访问发出请求→得到请求数据并回复 由上可见,用户访问未使用CDN缓存网站的过程为: 1).用户向 ...
- Factory 模式
在面向对象编程中, 最通常的方法是一个new操作符产生一个对象实例,new操作符就是用来构造对象实例的.但是在一些情况下, new操作符直接生成对象会带来一些问题.举例来说, 许多类型对象的创造需要一 ...
- linux下ssh的几种验证方式
ssh的认证方式有很多种,大概可以概括为以下几类: 1.pam认证方式 在配置文件/etc/ssh/sshd_config中对应参数:UsePAM 2.密钥认证方式 配置文件/etc/ssh/sshd ...
- Python之路,Day2 - Python基础2
def decode(self, encoding=None, errors=None): """ 解码 """ ""& ...
- JS Date
JS获取当前日期时间 var myDate = new Date();myDate.getFullYear(); //获取完整的年份(4位,1970-????)myDate.getMonth() ...