HDU1251统计难题---Trie Tree
map巧过
#include <stdio.h>
#include <string.h>
#include <map>
#include <string>
using namespace std;
map<string,int>m1;
int main()
{
char z[];
while(gets(z))
{
if(strlen(z)==)
break;
for(int i=strlen(z);i>;i--)
{
z[i]='\0';
m1[z]++;
}
}
while(gets(z))
{
printf("%d\n",m1[z]);
}
return ;
}
经典字典树(前缀树)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; struct node
{
int Count;///以此节点为前缀的单词数
node *next[];///26叉树
node() ///初始化数据
{
for (int i=;i<;i++)
next[i]=NULL;
Count=;
}
}; node *p,*root=new node();
void Insert(char *s)///插入新单词,即建立字典树
{
int i,k;
p=root;
for (i=; s[i]!='\0'; i++)
{
k=s[i]-'a';
if (p->next[k]==NULL)
p->next[k]=new node();
///判断是不是新节点,如果是分配创建一个新节点来存贮 ,
///即root的next域对应的k位置是否为空
p=p->next[k];///向前走一步
p->Count++; ///以此位置为前缀的数量+1
}
} int Search(char *s)
{
int i,k;
p=root;
for (i=; s[i]!='\0'; i++)
{
k=s[i]-'a';
if (p->next[k]==NULL)///一旦查找不到,立即跳出
return ;
p=p->next[k];
}
return p->Count;
} int main()
{
char s[];
while (gets(s))
{
int len=strlen(s);
if (len==)
break;
Insert(s);
}
while (gets(s))
{
printf("%d\n",Search(s));
}
return ;
}

第一个字典树(G++内存超限),第二个map(红黑树),对于此类问题,字典树效率优势明显
hihoCoder1014
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; struct node
{
int Count;///以此节点为前缀的单词数
node *next[];///26叉树
node() ///初始化数据
{
for (int i=;i<;i++)
next[i]=NULL;
Count=;
}
}; node *p,*root=new node();
void Insert(char *s)///插入新单词,即建立字典树
{
int i,k;
p=root;
for (i=; s[i]!='\0'; i++)
{
k=s[i]-'a';
if (p->next[k]==NULL)
p->next[k]=new node();
///判断是不是新节点,如果是分配创建一个新节点来存贮 ,
///即root的next域对应的k位置是否为空
p=p->next[k];///向前走一步
p->Count++; ///以此位置为前缀的数量+1
}
} int Search(char *s)
{
int i,k;
p=root;
for (i=; s[i]!='\0'; i++)
{
k=s[i]-'a';
if (p->next[k]==NULL)///一旦查找不到,立即跳出
return ;
p=p->next[k];
}
return p->Count;
} int main()
{
// freopen("input.txt","r",stdin);
char s[];
int n,m;
scanf("%d",&n);getchar();
while (n--)
{
gets(s);
Insert(s);
}
scanf("%d",&m);
getchar();
while (m--)
{
gets(s);
printf("%d\n",Search(s));
}
return ;
}
HDU1251统计难题---Trie Tree的更多相关文章
- HDU1251 统计难题 Trie树
题目很水,但毕竟是自己第一道的Trie,所以还是发一下吧.Trie的更多的应用慢慢学,AC自动机什么的也慢慢学.... #include<iostream> #include<cst ...
- [hdu1251]统计难题(trie模板题)
题意:返回字典中所有以测试串为前缀的字符串总数. 解题关键:trie模板题,由AC自动机的板子稍加改造而来. #include<cstdio> #include<cstring> ...
- HDU1251 统计难题 trie树 简单
http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意: 找前缀数量 裸模板 #include<cstdio> #include<cstr ...
- HDU1251 统计难题(Trie)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- hdu1251统计难题(trie)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU1251 统计难题 【trie树】
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- hdu1251 统计难题
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1251 题目: 统计难题 Time Limit: 4000/2000 MS (Java/Othe ...
- hdu 1251 统计难题 trie入门
统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...
- HDU 1251 统计难题(Trie)
统计难题 [题目链接]统计难题 [题目类型]Trie &题解: Trie的模板题,只不过这题坑点在没给数据范围,改成5e5就可以过了,用的刘汝佳蓝书模板 &代码: #include & ...
随机推荐
- thinkphp centos7 报class POD not found
没有安装PDO yum install php70w-pdo yum install php70w-mysqlnd 两条命令搞定
- 创建数据库配置文件ini(转)
一.有必要了解INI文件的结构: ;注释 [小节名] 关键字=值 ... ---- INI文件允许有多个小节,每个小节又允许有多个关键字, “=”后面是该关键字的值. ---- 值的类型有三种:字符串 ...
- PyCharm 的安装与入门操作
PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试.语法高亮.Project管理.代码跳转.智能提示.自动完成.单元测试.版本控制. ...
- 【Kaggle】泰坦尼克号
引言 Kaggle官方网站 这是泰坦尼克号事件的基本介绍: 我们需要做的就是通过给出的数据集,通过对特征值的分析以及运用机器学习模型,分析什么样的人最可能存活,并给出对测试集合的预测. 对于Kaggl ...
- awk命令例子详解
awk -F: '{print "Number of dields: "NF}' passwd 字段分隔符设为冒号,所以每条记录的字段数变成7: awk '{print &quo ...
- Echarts 解决饼图文字过长重叠的问题
之前在网上查找了很多关于解决饼图文字描述过长导致重叠的问题,找了很多一直没有一个合适的解决方案,最后自己只能花时间研究echarts文档,功夫不负有心人,终于解决了文字重叠展示不全等问题. 废话不多说 ...
- NumPy库入门
ndarray数组的元素类型 ndarray数组的创建 ndarray数组的操作 ndarray数组的运算
- NetCore log4net 集成以及配置日志信息不重复显示或者记录
NetCore log4net 集成,这是一个很常见而且网上大批大批的博文了,我写这个博文主要是为了记录我在使用过程中的一点小收获,以前在使用的过程中一直没有注意但是其实网上说的不清不楚的问题. 官方 ...
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目3
2014-03-18 01:32 题目:对于两个字符串,判断它们是否是Anagrams. 解法:统计俩单词字母构成是否相同即可. 代码: // 1.3 Given two strings, write ...
- USACO Section1.2 Palindromic Squares 解题报告
palsquare解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------ ...