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

  1. Red Alder
  2. Ash
  3. Aspen
  4. Basswood
  5. Ash
  6. Beech
  7. Yellow Birch
  8. Ash
  9. Cherry
  10. Cottonwood
  11. Ash
  12. Cypress
  13. Red Elm
  14. Gum
  15. Hackberry
  16. White Oak
  17. Hickory
  18. Pecan
  19. Hard Maple
  20. White Oak
  21. Soft Maple
  22. Red Oak
  23. Red Oak
  24. White Oak
  25. Poplan
  26. Sassafras
  27. Sycamore
  28. Black Walnut
  29. Willow

Sample Output

  1. Ash 13.7931
  2. Aspen 3.4483
  3. Basswood 3.4483
  4. Beech 3.4483
  5. Black Walnut 3.4483
  6. Cherry 3.4483
  7. Cottonwood 3.4483
  8. Cypress 3.4483
  9. Gum 3.4483
  10. Hackberry 3.4483
  11. Hard Maple 3.4483
  12. Hickory 3.4483
  13. Pecan 3.4483
  14. Poplan 3.4483
  15. Red Alder 3.4483
  16. Red Elm 3.4483
  17. Red Oak 6.8966
  18. Sassafras 3.4483
  19. Soft Maple 3.4483
  20. Sycamore 3.4483
  21. White Oak 10.3448
  22. Willow 3.4483
  23. Yellow Birch 3.4483

Hint

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

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

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. map<string,int>mp;
  4. int main()
  5. {
  6.  
  7. int n;
  8. cin>>n;
  9. int t=n;
  10. getchar();
  11. string s;
  12. while(n--){
  13. getline(cin,s);
  14. mp[s]++;
  15. }
  16. map<string,int>::iterator it;
  17. for(it=mp.begin();it!=mp.end();it++)
  18. cout<<it->first<<" "<<setiosflags(ios::fixed)<<setprecision()<<100.0*it->second/t<<'%'<<endl;
  19. return ;
  20. }

解法二:Trie树

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n,len;
  4. char s[];
  5. struct Trie
  6. {
  7. int cnt;
  8. bool ok;
  9. char name[];
  10. Trie *next[];
  11. Trie(){
  12. cnt=,ok=;
  13. memset(next,,sizeof(next));
  14. }
  15. }*root;
  16. void Insert(char *s,int id)
  17. {
  18. Trie *p=root;
  19. int len=strlen(s);
  20. for(int i=;i<len;i++){
  21. if(!p->next[s[i]-'A'])
  22. p->next[s[i]-'A']=new Trie();
  23. p=p->next[s[i]-'A'];
  24. }
  25. p->cnt++;
  26. if(!p->ok) p->ok=;
  27. strcpy(p->name,s);
  28. }
  29. void dfs(Trie *root)
  30. {
  31. Trie *p=root;
  32. if(p->ok){
  33. printf("%s",p->name);
  34. printf(" %.4lf%\n",100.0*p->cnt/n);
  35. }
  36. for(int i=;i<;i++){//字典序
  37. if(p->next[i])
  38. dfs(p->next[i]);
  39. }
  40. }
  41. int main()
  42. {
  43. cin>>n;
  44. getchar();
  45. root=new Trie;
  46. for(int i=;i<=n;i++){
  47. gets(s);
  48. Insert(s,i);
  49. }
  50. dfs(root);
  51. }

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. 认识OpenStack中的flatnetwork

    目录 [隐藏] 1 Understanding FlatNetworking 1.1 FlatNetworking 1.1.1 Single Adapter, All in one setup 1.1 ...

  2. XmlSerializer的GenerateTempAssembly性能问题例外

    XmlSerializer的两个构造函数不会出现每次构造都创建TempAssembly的性能问题,其内部做了缓存. public XmlSerializer(Type type) public Xml ...

  3. VS2013打开2008的项目

    找到 .csproj 后缀的文件.然后右键选择文本打开. 找到下面一段话: <ProjectTypeGuids>-00065b846f21};{fae04ec0-301f-11d3-bf4 ...

  4. Java内存泄露监控工具:JVM监控工具介绍【转】

    jstack?-- 如果java程序崩溃生成core文件,jstack工具可以用来获得core文件的java stack和native stack的信息,从而可以轻松地知道java程序是如何崩溃和在程 ...

  5. iOS - ShareSDK第三方分享(图文和视频)和登录

    由于近期工作需要自己抽时间搞了一下第三方分享,这里使用的是shareSDK的第三方,在使用的过程中有一些心得和体会,特在此和大家分享一下~ 1.在经过将近一周时间的开发,终于搞定ios分享了. 2.由 ...

  6. python获取windows所有com口

    import serial import serial.tools.list_ports port_list = list(serial.tools.list_ports.comports()) po ...

  7. vue之介绍

    vue的作者叫尤雨溪,中国人.自认为很牛逼的人物,也是我的崇拜之神. 关于他本人的认知,希望大家读一下这篇关于他的文章,或许你会对语言,技术,产生浓厚的兴趣.https://mp.weixin.qq. ...

  8. Equinox P2 介绍(一)Getting Start

    一直觉得 Equinox 的 P2 是个神秘的东西,常常使得 Eclipse 或 Equinox 表现出一些奇怪的行为,于是找来官方文档读一读,试图更好地理解与应用 Equinox . 官方文档很多, ...

  9. easyui---easyloader.js

    1.easyloader.js 是根据用户指定,动态加载组件,可以替换下面jqueryeasyui <!-- <script type="text/javascript" ...

  10. [No00004F]史上最全Vim快捷键键位图(入门到进阶)vim常用命令总结

    在命令状态下对当前行用== (连按=两次), 或对多行用n==(n是自然数)表示自动缩进从当前行起的下面n行.你可以试试把代码缩进任意打乱再用n==排版,相当于一般IDE里的code format.使 ...