我的错因: 本来改用%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. MySQL安装之zip格式

    背景: 今天本来想学点JDBC的,没想到在MySQL的安装上卡了很久,特此写下此文,希望大家遇到类似问题可以早些跳出坑.   一.寻找资源 今天,为了学习JDBC,准备在公司的电脑上装MySQL,于是 ...

  2. html绑定

    目的 html绑定可以绑定DOM元素内的HTML内容. 示例: <div data-bind="html: details"></div> <scri ...

  3. .net导出不规则Excel

    using Hamp.App.BLL; using Hamp.App.Model; using Hamp.App.Model.QualityManagement; using System; usin ...

  4. 批处理改hosts

    @echo off color 0F @attrib -r "%windir%\system32\drivers\etc\hosts" @echo ######测试配置 beg & ...

  5. PHP 分析1

    D:\wamp64\www\practice test 3: PHP 显示乱码 http://localhost/practice/ex1_5_stu.php <html><meta ...

  6. UNICODE与ANSI的区别

    什么是ANSI,什么又是UNICODE呢?其实这是两种不同的编码方式标准,ANSI中的字符采用8bit,而UNICODE中的字符采用16bit.(对于字符来说ANSI以单字节存放英文字符,以双字节存放 ...

  7. C#多线程:深入了解线程同步lock,Monitor,Mutex,同步事件和等待句柄(中)

    本篇继续介绍WaitHandler类及其子类 Mutex,ManualResetEvent,AutoResetEvent的用法..NET中线程同步的方式多的让人看了眼花缭乱,究竟该怎么去理解呢?其实, ...

  8. SEO定义目的,优化的好处

    SEO:search engine optimization(搜索引擎优化) SEO严谨的定义:SEO是指在了解搜索引擎自然排名机制的基础上,对网站进行内部及外部的调整优化,改进网站在搜索引擎中关键字 ...

  9. each用法的总结

    1.选择器+遍历 $('div').each(function (i){ i就是索引值 this 表示获取遍历每一个dom对象 }); 2.选择器+遍历 $('div').each(function  ...

  10. android ScrollView中嵌套listview listview可点击处理,可展开

    public class MyListView extends ListView { public MyListView(Context context, AttributeSet attrs, in ...