我的错因: 本来改用%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. Super Jumping! Jumping! Jumping!杭电1087

    Description Problem Description Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumpi ...

  2. EFI安装Win7

    安装系统之前电脑里最好没有其他系统,安装过程中电脑需重启多次,其他系统会引导电脑开机,无法完成WIN7安装. 一.制作安装分区 1.首先在移动硬盘(U盘)准备一个FAT32分区 一定要FAT32分区, ...

  3. Java线程池使用

    1.构造函数 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit ...

  4. drupal7 修改文件上传大小限制

    参考文章:Drupal 7 设置上传文件的限制大小 自己用 '#type' => 'managed_file'做了一个上传的功能,但是上传时总是说超过了2M的限制,接下来说一下怎么修改限制. 一 ...

  5. ModelDriven

    功能:  submit 之后显示结果  1.项目结构 2.web.xml <?xml version="1.0" encoding="UTF-8"?> ...

  6. window.open打开新页面居中

    var iHeight = 500;//新打开页面的高 var iWidth = 800;//新打开页面的宽 var iTop = (window.screen.height-30-iHeight)/ ...

  7. JSON及与XML比较

      JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - Decem ...

  8. Maze

    Maze 题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5094 BFS+状态压缩 把身上携带钥匙的状态压缩成一个2^10的整数.这道题难在 ...

  9. 朴素贝叶斯算法 & 应用实例

    转载请注明出处:http://www.cnblogs.com/marc01in/p/4775440.html 引 和师弟师妹聊天时经常提及,若有志于从事数据挖掘.机器学习方面的工作,在大学阶段就要把基 ...

  10. hdu_5881_Tea(xjb猜)

    题目链接:hdu_5881_Tea 题意: 有一壶水, 体积在 L 和 R 之间, 有两个杯子, 你要把水倒到两个杯子里面, 使得杯子水体积几乎相同(体积的差值小于等于1), 并且使得壶里剩下水体积不 ...