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. laravel EncryptCookies中间件导致无法获取自定义cookie

    解决办法: \app\Http\Middleware\EncryptCookies.php 添加过滤cookie key protected $except = [ 'token' ];

  2. Spark调优之JVM调优

    一.JVM调优 JVM: 老年代: 存放少量生命周期长的对象,如连接池 年轻代: Spark task执行算子函数自己创建的大量对象 JVM机制: 对象进入java虚拟机之后会放在eden区域和一个s ...

  3. [Codeforces976E]Well played!(贪心)

    [不稳定的传送门] Solution 首先可以证明,hp翻倍的操作一定是在同一个生物上最优 Code #include <cstdio> #include <algorithm> ...

  4. python基础之正则表达式爬虫应用,configparser模块和subprocess模块

    正则表达式爬虫应用(校花网) 1 import requests 2 import re 3 import json 4 #定义函数返回网页的字符串信息 5 def getPage_str(url): ...

  5. 关于修改zeppelin的代码显示

    最近我在修改zeppelin(0.7版本)的源码相关的知识,目前做的工作是修改zeppelin的代码,为了让zeppelin可以可以在页面中显示数据集,并且在其数据库中存储式真实的路径1.如果我们要运 ...

  6. PHP.TP框架下商品项目的优化2-图片优化

    图片存储.上传.显示优化 1.图片路径写进配置文件,当路径有变动时[因业务扩大,服务器存储图片空间不足等],只需修改配置文件,而不用修改代码 2.封装显示.上传.删除函数,实现代码重用 [可类比其他类 ...

  7. 微信小程序 | 49,小程序入门集锦系列文章20篇

    以下20篇文章,都是关于微信小程序的文章,以入门常见问题为主.如发现谬误,请与笔者联系. [小程序入门集锦]1,微信小程序在哪里打开 [小程序入门集锦]2,小程序商店 [小程序入门集锦]3,微信小程序 ...

  8. 全方位认识HDMI接口技术

    HDMI接口并不是一个开放的标准.制造商必须向HDMI标准制定协会支付版税,来换取一个生产许可证.不过这个版税可不便宜,每年要交纳15000美元的许可费,并且更黑的是每生产一个HDMI接口就要支付0. ...

  9. 3 Vue.js

    1 2 3 <script> var vm = new Vue({ el:"#app", //context data:{ //context["articl ...

  10. spring boot 入门1-----如何使用@Value注解读取配置文件以及使用@ConfigrationProperties注解

    读取.yml文件属性值的方式 1)如何将配置文件中的属性值与字段匹配起来 @Value("${配置文件中属性的名}") 在application.yml文件中 server: po ...