简单方法:

#include<bits/stdc++.h>
using namespace std; int main()
{
char s[];
map<string,int>ma;
while(gets(s)&&strlen(s)!=)
{
for(int i=strlen(s)-;i>=;i--)
{ ma[s]++;
s[i]='\0';
// cout<<s<<endl;
} }
while(gets(s))
{
printf("%d\n",ma[s]); } }

但是当数据过多的时候map就不行了

要用新的数据结构——字典树

有两种实现的方法 一种是指针 一种是数组

数组的实现方法更为方便 高明 快速

注意  数组一定要开到一百万   不然wa !!!

#include<bits/stdc++.h>
using namespace std;
int trie[][]={};
int sum[]={};
int pos=; void insert1( char *word )
{
int root=;
for(int i=;i<strlen(word);i++)
{ int ch=word[i]-'a';
if(trie[root][ ch ]==)
trie[root][ ch ]=++pos;
root=trie[root][ch];
sum[root]++;
} } int find1(char *word)
{
int root=;
for(int i=;i<strlen(word);i++)
{
int ch=word[i]-'a';
if(trie[root][ ch ]==)return ;
root=trie[root][ch]; }
return sum[root]; } int main()
{
char word[];
while(gets(word))
{
if(strlen(word)==)break; insert1(word); }
while(gets(word))
{
printf("%d\n",find1(word));
} }

统计难题 HDU1251的更多相关文章

  1. 统计难题---hdu1251字典树模板

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 node *head=(node*)malloc(sizeof(node)); for(int ...

  2. (字典树模板)统计难题--hdu--1251

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 在自己敲了一遍后终于懂了,这不就用了链表的知识来建立了树,对!就是这样的,然后再查找 代码: #i ...

  3. hdu1251 统计难题

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

  4. HDU1251 统计难题(Trie)

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

  5. HDU1251统计难题(水字典树)

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

  6. hdu1251(统计难题)

    这题就是一个字典树的模板题 统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Othe ...

  7. hdu1251统计难题(trie)

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

  8. HDU-1251 统计难题,字典树或者map!

    统计难题 很久就看过这个题了,但不会~~~不会~~ 题意:给出一张单词表,然后下面有若干查询,每次给出一个单词,问单词表中是否存在以这个单词为前缀的单词,输出数量.本身也是自身的前缀.只有一组数据! ...

  9. HDU1251 统计难题 【trie树】

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

随机推荐

  1. 《深入理解java虚拟机》第三章 垃圾收集器与内存分配策略

    第三章 垃圾收集器与内存分配策略 3.1 概述 哪些内存需要回收 何时回收 如何回收 程序计数器.虚拟机栈.本地方法栈3个区域随线程而生灭. java堆和方法区的内存需要回收.   3.2 对象已死吗 ...

  2. DCNN models

    r egion based RNN Fast RCNN Faster RCNN F-RCN Faster RCNN the first five layers is same as the ZF ne ...

  3. POJ1287 Networking【最小生成树】

    题意: 给出n个节点,再有m条边,这m条边代表从a节点到b节点电缆的长度,现在要你将所有节点都连起来,并且使长度最小 思路: 这是个标准的最小生成树的问题,用prim的时候需要注意的是他有重边,取边最 ...

  4. C++学习3--编程基础(vector、string、三种传参)

    知识点学习 Vector容器 vector是C++标准程序库中的一个类,其定义于头文件中,与其他STL组件一样,ventor属于STD名称空间: ventor是C++标准程序库里最基本的容器,设计之初 ...

  5. [转]GDB-----1.GDB概述

    作者: liigo原文链接: http://blog.csdn.net/liigo/archive/2006/01/17/582231.aspx 1.前言 本文写给主要工作在Windows操作系统下而 ...

  6. [转]SPI通信原理简介

    [转自]http://www.cnblogs.com/deng-tao/p/6004280.html 1.前言 SPI是串行外设接口(Serial Peripheral Interface)的缩写.是 ...

  7. 【转】C++ map的基本操作和使用

    1.map简介 map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响较小,除了那个操作节点,对其它的节点都没有什么影响.对于迭代器来说,可以修改实值,而不能修改key. 2.map的功能 自 ...

  8. (记录合并)union和union all的区别

    SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION内部的SELECT语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条SE ...

  9. 常用的4个eclipse插件安装过程及使用方法

    最近整合了4个常用eclipse插件安装过程,分别是PMD.checkstyle.findbugs.sourcemonitor插件.因为我这里没有外网,所以所有的插件不是最新版,建议有网的童鞋自行在外 ...

  10. mysql当查询某字段结果为空并赋值

    1 代码 1.1 当当前字段为空,查询结果返回“none”,并且统计出现频率 select case when 字段 is null then 'none' else 字段 end as 字段, co ...