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. POJ:1995-Raising Modulo Numbers(快速幂)

    Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9512 Accepted: 578 ...

  2. 34-Cookie-based认证实现

    新建MVC项目,然后用VSCode打开 dotnet new mvc --name MvcCookieAuthSample 在Controllers文件夹下新建AdminController.cs u ...

  3. 3-1 练习 HTML 总结

    1.段落 #段落 <div class="ui segment"> </div> #翻转 <div class="ui inverted s ...

  4. POJ 3580 SuperMemo 伸展树

    题意: 维护一个序列,支持如下几种操作: ADD x y D:将区间\([x,y]\)的数加上\(D\) REVERSE x y:翻转区间\([x,y]\) REVOLVE x y T:将区间\([x ...

  5. Spring Boot :Request请求处理流程

    技术交流群:233513714

  6. spring里面的context:component-scan

    原文:http://jinnianshilongnian.iteye.com/blog/1762632 component-scan的作用的自动扫描,把扫描到加了注解Java文件都注册成bean &l ...

  7. shell参数 传递

    $# 是传给脚本的参数个数 $0 是脚本本身的名字 $1 是传递给该shell脚本的第一个参数 $2 是传递给该shell脚本的第二个参数 $@ 是传给脚本的所有参数的列表 $* 是以一个单字符串显示 ...

  8. 《Cracking the Coding Interview》——第6章:智力题——题目1

    2014-03-19 06:40 题目:有20瓶药,其中19瓶装的都是1.0克的药片,只有1瓶装了1.1克的药.给你一个能称出具体克数的电子秤,只允许你称一次,怎么找出那瓶不一样的? 解法:如果药片管 ...

  9. 服务过美国总统竞选的非传统投票UI【demo已放出】

    =============================== 更新:DEMO和分析已经放出,地址在这里   http://www.cnblogs.com/arfeizhang/p/faceoffde ...

  10. 2.0 python+appium环境搭建

    Python下载地址:链接:https://pan.baidu.com/s/1Z3H8tw8AiBVwpxdcABC7XQ 密码:z66t Pycharm下载地址: 链接:https://pan.ba ...