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的更多相关文章

  1. HDU1251 统计难题 Trie树

    题目很水,但毕竟是自己第一道的Trie,所以还是发一下吧.Trie的更多的应用慢慢学,AC自动机什么的也慢慢学.... #include<iostream> #include<cst ...

  2. [hdu1251]统计难题(trie模板题)

    题意:返回字典中所有以测试串为前缀的字符串总数. 解题关键:trie模板题,由AC自动机的板子稍加改造而来. #include<cstdio> #include<cstring> ...

  3. HDU1251 统计难题 trie树 简单

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意: 找前缀数量 裸模板 #include<cstdio> #include<cstr ...

  4. HDU1251 统计难题(Trie)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  5. hdu1251统计难题(trie)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  6. HDU1251 统计难题 【trie树】

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  7. hdu1251 统计难题

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1251 题目: 统计难题 Time Limit: 4000/2000 MS (Java/Othe ...

  8. hdu 1251 统计难题 trie入门

    统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...

  9. HDU 1251 统计难题(Trie)

    统计难题 [题目链接]统计难题 [题目类型]Trie &题解: Trie的模板题,只不过这题坑点在没给数据范围,改成5e5就可以过了,用的刘汝佳蓝书模板 &代码: #include & ...

随机推荐

  1. table选项卡

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  2. cmd中编译java脚本 (2013-05-02-bd 写的日志迁移

    此前提是已经搭建好了jdk的编译环境! 先写一个java脚本如:建立一个HelloWord.java //public : 表示此类是公共的 一个java文件中只能有一个public类 //class ...

  3. VIM 如何切换buffer

    命令 :ls 可查看当前已打开的buffer 命令 :b num 可切换buffer (num为buffer list中的编号) 其它命令: :bn -- buffer列表中下一个 buffer :b ...

  4. python基础之多态与多态性、绑定方法和非绑定方法

    多态与多态性 多态 多态并不是一个新的知识 多态是指一类事物有多种形态,在类里就是指一个抽象类有多个子类,因而多态的概念依赖于继承 举个栗子:动物有多种形态,人.狗.猫.猪等,python的序列数据类 ...

  5. mysql in和exists性能比较和使用【转】

    exists对外表用loop逐条查询,每次查询都会查看exists的条件语句,当 exists里的条件语句能够返回记录行时(无论记录行是的多少,只要能返回),条件就为真,返回当前loop到的这条记录, ...

  6. 6,MongoDB 之 Array Object 的特殊操作

    相比关系型数据库, Array [1,2,3,4,5] 和 Object { 'name':'DragonFire' } 是MongoDB 比较特殊的类型了 特殊在哪里呢?在他们的操作上又有什么需要注 ...

  7. nodejs基础1

    nodejs学习网站: https://github.com/alsotang/node-lessons 1.全局对象 (1)node中没有window对象,有global对象替代window对象 g ...

  8. iOS Crash 分析 符号化崩溃日志

    参考: http://blog.csdn.net/diyagoanyhacker/article/details/41247367 http://blog.csdn.net/diyagoanyhack ...

  9. MD5--3D模型

    在学习Away3D的过程中,接触到MD5模型和MD5动画这样两个词.之前对MD5的认识就是一种加密技术,怎么它又和动画扯上关系了呢. 一阵谷歌之后,终于在这个地方发现了关于3D方面的MD5介绍了:ht ...

  10. USACO Section1.4 Arithmetic Progressions 解题报告

    ariprog解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...