我的错因: 本来改用%f输出,我用了%lf,结果编译器直接判定为错误(一部分编译器认为lf是没有错的)。当时我还以为是hash出错了。。

  方法不止一种:

  方法            时间        空间
  Hash           891ms      596k
  map<string,int>     2735ms      1316k
  sort            5000ms+    30000k+

  %lf 与 %f的具体区别:

  printf的%f说明符的确既可以输出float型又可以输出double型。根据“默认参数提升”规则float型会被提升为double型。因此printf()只会看到双精度数。对于scanf,情况就完全不同了,它接受指针,这里没有类似的类型提升。向float存储和向double存储大不一样,因此,scanf区别%f和%lf。

  也就是说输出的时候不管输出的是双精度还是单精度都用%f就没错了,但是输入的时候,输入单精度要用%f而输入双精度要用%lf。

  哈希

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<sstream>
using namespace std;
struct Tree
{
char name[];
int sum;
} tree[];
int SDBMHash(char *str)
{
int Hash = ;
while(*str)
{
Hash = (*str++) + (Hash << ) + (Hash << ) - Hash;
}
return (Hash&0x7FFFFFFF);
}
bool cmp(Tree a,Tree b)
{
return strcmp(a.name,b.name)<;
}
map<int,int> pos;
int main()
{
// freopen("H.in.cpp","r",stdin);
char tmp[];
pos.clear();
int tot = ,all = ;
while(gets(tmp))
{
if(strcmp(tmp,"") == ) break;
all++;
int H = SDBMHash(tmp);
int id = pos[H];
if(id == )
{
strcpy(tree[++tot].name,tmp);
tree[tot].sum = ;
pos[H] = tot;
}
else
{
tree[id].sum++;
}
}
sort(tree+,tree++tot,cmp);
for(int i = ; i <= tot; i++)
{
printf("%s %.4f\n",tree[i].name,tree[i].sum*100.0/all);
}
return ;
}

  map<string,int>

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<sstream>
using namespace std;
struct Tree
{
string name;
int sum;
} tree[];
bool cmp(Tree a,Tree b)
{
return a.name < b.name;
}
map<string,int> vis;
int main()
{
// freopen("H.in.cpp","r",stdin);
char a[];
string tmp;
vis.clear();
int tot = ,all = ;
while(gets(a))
{
all++;
int len = strlen(a);
tmp = "";
for(int i = ; i < len; i++)
{
tmp += a[i];
}
int id = vis[tmp];
if(id == )
{
tree[++tot].name = tmp;
tree[tot].sum = ;
vis[tmp] = tot;
}
else
{
tree[id].sum++;
}
}
sort(tree+,tree+tot+,cmp);
for(int i = ; i <= tot; i++)
{
cout<<tree[i].name<<" ";
printf("%.4f\n",tree[i].sum*100.0/all);
}
return ;
}

  排序

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<sstream>
using namespace std;
struct Tree
{
char name[];
} tree[];
bool cmp(Tree a,Tree b)
{
return strcmp(a.name,b.name)<;
}
int main()
{
// freopen("H.in.cpp","r",stdin);
int all = ;
while(gets(tree[all].name) && strlen(tree[all].name))
{
all++;
}
sort(tree,tree+all,cmp);
int tot = ;
for(int i = ; i < all; i++)
{
if(strcmp(tree[i].name,tree[i+].name) == ) tot++;
else
{
printf("%s %.4f\n",tree[i].name,tot*100.0/all);
tot = ;
}
}
return ;
}

POJ 2418 Hardwood Species (哈希,%f 和 %lf)的更多相关文章

  1. [字典树] poj 2418 Hardwood Species

    题目链接: id=2418">http://poj.org/problem?id=2418 Hardwood Species Time Limit: 10000MS   Memory ...

  2. POJ 2418 Hardwood Species

                                                     Hardwood Species Time Limit: 10000MS   Memory Limit ...

  3. POJ 2418 Hardwood Species(STL在map应用)

    职务地址:id=2418">POJ 2418 通过这个题查了大量资料..知道了非常多曾经不知道的东西. . .. 在代码中凝视说明吧. 代码例如以下: #include <ios ...

  4. [ACM] POJ 2418 Hardwood Species (Trie树或map)

    Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17986   Accepted: 713 ...

  5. poj 2418 Hardwood Species (map)

    题目:http://poj.org/problem?id=2418 在poj 上交题总是有各种错误,再次感叹各个编译器. c++ AC代码,G++为超时,上代码: #include<cstdio ...

  6. POJ - 2418 Hardwood Species(map,trie,BST)

    1.输入若干行树名,输入结束后,按字典序输出树名及其所占百分比. 2.多种方法:map,trie,BST 3. map: #include<iostream> #include<st ...

  7. 二叉搜索树 POJ 2418 Hardwood Species

    题目传送门 题意:输入一大堆字符串,问字典序输出每个字符串占的百分比 分析:二叉搜索树插入,然后中序遍历就是字典序,这里root 被new出来后要指向NULL,RE好几次.这题暴力sort也是可以过的 ...

  8. POJ 2418 Hardwood Species( AVL-Tree )

    #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> ...

  9. POJ 2418 Hardwood Species 【Trie树】

    <题目链接> 题目大意: 给你一堆字符串,让你按字典序输出他们出现的频率. 解题分析: 首先,这是Trie数词频统计的题目,以Trie树的边储存字母,节点存储以该节点结尾的链所代表的字符串 ...

随机推荐

  1. jquery中的serialize

    jquery中的serialize对serializeArray进行了封装,serializeArray源码中定义将disabled的过滤掉,所以提交后值为null 解决方式是:在提交的时候,讲dis ...

  2. c#简单易用的短信发送服务 悠逸企业短信服务

     悠逸企业短信发送服务,是一种比较简单易操作的短信发送服务,使用POST的方式,请求相应地址就可以实现短信发送功能 1 /// <summary> /// 短信发送服务 /// </ ...

  3. 找轮转后的有序数组中第K小的数

    我们可以通过二分查找法,在log(n)的时间内找到最小数的在数组中的位置,然后通过偏移来快速定位任意第K个数. 此处假设数组中没有相同的数,原排列顺序是递增排列. 在轮转后的有序数组中查找最小数的算法 ...

  4. Linux 网络性能tuning向导

    本文的目的不完全在于提供调优信息,而是在于告诉读者了解Linux kernel如何处理数据包,从而能够在 自己的实践中发挥Linux 内核协议栈最大的性能 The NIC ring buffer 接收 ...

  5. sql 各种格式

    --以2013-12-10 12:56:55为例--convert(nvarchar(10),CreateDate,120)      =>      2013-12-10--DATEPART( ...

  6. Linux下Modules的概念及使用详解[转贴]

    一.什么是 modules? modules 的字面意思就是模块,在此指的是 kernel modules:简单来说,一个模块提供了一个功能,如 isofs.minix.nfs.lp 等等.传统来讲, ...

  7. JVM内存模型,垃圾回收算法

    JVM内存模型总体架构图 程序计数器多线程时,当线程数超过CPU数量或CPU内核数量,线程之间就要根据时间片轮询抢夺CPU时间资源.因此每个线程有要有一个独立的程序计数器,记录下一条要运行的指令.线程 ...

  8. Delphi @ # $ 特殊字符含义

      ^: 指针   @: 取址  #: 十进制符   $: 十六进制符

  9. 设计模式--命令模式(Command)

    基本概念:  Command模式也叫命令模式 ,是行为设计模式的一种.Command模式通过被称为Command的类封装了对目标对象的调用行为以及调用参数,命令模式将方法调用给封装起来了. 命令模式的 ...

  10. 淘淘商城_day06_课堂笔记

    今日大纲 实现单点登录系统 基于单点登录系统实现,用户的注册和登录 商品数据同步问题 问题 后台系统中将商品修改,前台系统没有进行数据的同步,导致前端系统不能够实时显示最新的数据. 解决 后台系统中商 ...