Description

Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter. 
America's temperate climates produce forests with hundreds of hardwood species -- trees that share certain biological characteristics. Although oak, maple and cherry all are types of hardwood trees, for example, they are different species. Together, all the hardwood species represent 40 percent of the trees in the United States.

On the other hand, softwoods, or conifers, from the Latin word meaning "cone-bearing," have needles. Widely available US softwoods include cedar, fir, hemlock, pine, redwood, spruce and cypress. In a home, the softwoods are used primarily as structural lumber such as 2x4s and 2x6s, with some limited decorative applications.

Using satellite imaging technology, the Department of Natural Resources has compiled an inventory of every tree standing on a particular day. You are to compute the total fraction of the tree population represented by each species.

Input

Input to your program consists of a list of the species of every tree observed by the satellite; one tree per line. No species name exceeds 30 characters. There are no more than 10,000 species and no more than 1,000,000 trees.

Output

Print the name of each species represented in the population, in alphabetical order, followed by the percentage of the population it represents, to 4 decimal places.

Sample Input

Red Alder
Ash
Aspen
Basswood
Ash
Beech
Yellow Birch
Ash
Cherry
Cottonwood
Ash
Cypress
Red Elm
Gum
Hackberry
White Oak
Hickory
Pecan
Hard Maple
White Oak
Soft Maple
Red Oak
Red Oak
White Oak
Poplan
Sassafras
Sycamore
Black Walnut
Willow

Sample Output

Ash 13.7931
Aspen 3.4483
Basswood 3.4483
Beech 3.4483
Black Walnut 3.4483
Cherry 3.4483
Cottonwood 3.4483
Cypress 3.4483
Gum 3.4483
Hackberry 3.4483
Hard Maple 3.4483
Hickory 3.4483
Pecan 3.4483
Poplan 3.4483
Red Alder 3.4483
Red Elm 3.4483
Red Oak 6.8966
Sassafras 3.4483
Soft Maple 3.4483
Sycamore 3.4483
White Oak 10.3448
Willow 3.4483
Yellow Birch 3.4483

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceeded.

解法一:map应用(自动按照键值字典序排序,强)

#include<bits/stdc++.h>
using namespace std;
map<string,int>mp;
int main()
{ int n;
cin>>n;
int t=n;
getchar();
string s;
while(n--){
getline(cin,s);
mp[s]++;
}
map<string,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++)
cout<<it->first<<" "<<setiosflags(ios::fixed)<<setprecision()<<100.0*it->second/t<<'%'<<endl;
return ;
}

解法二:Trie树

#include<bits/stdc++.h>
using namespace std;
int n,len;
char s[];
struct Trie
{
int cnt;
bool ok;
char name[];
Trie *next[];
Trie(){
cnt=,ok=;
memset(next,,sizeof(next));
}
}*root;
void Insert(char *s,int id)
{
Trie *p=root;
int len=strlen(s);
for(int i=;i<len;i++){
if(!p->next[s[i]-'A'])
p->next[s[i]-'A']=new Trie();
p=p->next[s[i]-'A'];
}
p->cnt++;
if(!p->ok) p->ok=;
strcpy(p->name,s);
}
void dfs(Trie *root)
{
Trie *p=root;
if(p->ok){
printf("%s",p->name);
printf(" %.4lf%\n",100.0*p->cnt/n);
}
for(int i=;i<;i++){//字典序
if(p->next[i])
dfs(p->next[i]);
}
}
int main()
{
cin>>n;
getchar();
root=new Trie;
for(int i=;i<=n;i++){
gets(s);
Insert(s,i);
}
dfs(root);
}

PKU2418_树种统计(map应用||Trie树)的更多相关文章

  1. BZOJ 2754 [SCOI2012]喵星球上的点名 (AC自动机+map维护Trie树)

    题目大意:略 由于字符集大,要用map维护Trie树 并不能用AC自动机的Trie图优化,不然内存会炸 所以我用AC自动机暴跳fail水过的 显然根据喵星人建AC自动机是不行的,所以要根据问题建 然而 ...

  2. hdu 1251:统计难题[【trie树】||【map】

    <题目链接> 统计难题                        Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131 ...

  3. 统计难题(trie树)

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

  4. hdu 1004 Let the Balloon Rise strcmp、map、trie树

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  5. HDU1251 统计难题 【trie树】

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

  6. HDU 1251 统计难题 (Trie树模板题)

    题目链接:点击打开链接 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单 ...

  7. HDU - 1251 统计难题(Trie树)

    有很多单词(只有小写字母组成,不会有重复的单词出现) 要统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). 每个单词长度不会超过10. Trie树的模板题.这个题内存把控不好容易MLE. ...

  8. hdu 1251 统计难题(trie 树的简单应用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给你多个字符串,求以某个字符串为前缀的字符串数量. 思路:简单的trie数应用,在trie ...

  9. Trie树及其应用

    Trie树及其应用 Trie树 Trie树,又称单词查找树.字典树,是一种树形结构,是一种哈希树的变种,是一种用于快速检索的多叉树结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经 ...

随机推荐

  1. Jenkins权限管理之Matrix Authorization Strategy

    一.权限管理概述 jenkins的权限管理,我目前使用的是Role-based Authorization Strateg.这个很简单,权限是jenkins已经定死了的,就那些.该插件可以让我们新建角 ...

  2. Node.j中path模块对路径的操作

    一.path模块 https://nodejs.org/docs/latest/api/path.html#path_path_join_paths 1.join方法 ==> 该方法将多个参数值 ...

  3. Mysql语句优化

    总结总结自己犯过的错,网上说的与自己的Mysql语句优化的想法. 1.查询数据库的语句的字段,尽量做到用多少写多少. 2.建索引,确保查询速度. 3.orm框架自带的方法会损耗一部分性能,这个性能应该 ...

  4. 23种设计模式之装饰模式(Decorator)

    装饰模式是一种对象结构型模式,可动态地给一个对象增加一些额外的职责,就增加对象功能来说,装饰模式比生成子类实现更为灵活.通过装饰模式,可以在不影响其他对象的情况下,以动态.透明的方式给单个对象添加职责 ...

  5. 【CF917D】Stranger Trees 树形DP+Prufer序列

    [CF917D]Stranger Trees 题意:给你一棵n个点的树,对于k=1...n,问你有多少有标号的n个点的树,与给出的树有恰好k条边相同? $n\le 100$ 题解:我们先考虑容斥,求出 ...

  6. 【CF802C】Heidi and Library (hard) 费用流

    [CF802C]Heidi and Library (hard) 题意:有n个人依次来借书,第i人来的时候要求书店里必须有种类为ai的书,种类为i的书要花费ci块钱购入.而书店的容量只有k,多余的书只 ...

  7. [APP] Android 开发笔记 003-使用Ant Release 打包与keystore加密说明

    接上节 [APP] Android 开发笔记 002 5. 使用ant release 打包 1)制作 密钥文件 release.keystore (*.keystore) keytool -genk ...

  8. [Html5] HTML5 开发手机应用

    上次周例会我给大家做了题目为:<漫游移动平台前端开发>的汇报,现在推荐一些额外的学习资料.          依照目前iPhone/Android 迅速提升市占率的情势来看,未来如果想要在 ...

  9. python WEB UI自动化在日期框中动态输入当前日期

    要在日期框中输入当前日期,如下图 代码为 本想用最简单的方法,直接用sendkeys发送当前日期,如下: current_time=time.strftime('%Y-%m-%d',time.loca ...

  10. linux下jmeter持续集成Jenkins部署时问题解决

    之前成linux下安装了Jenkins,并做了一些简单的工作,这次正好将jmeter也集成进去,在实际操作时发现好多坑,写下做记录 怎么安装这里就不介绍了,网上很多资料,这里只记录问题,以供大家参数 ...