POJ 2418 Hardwood Species

刺骨的言语ヽ痛彻心扉 2022-09-29 01:55 48阅读 0赞

Hardwood Species














Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 24524   Accepted: 9489

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.

Source

Waterloo Local 2002.01.26

代码1:map

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <string>
  6. #include <map>
  7. #include <stack>
  8. #include <vector>
  9. #include <set>
  10. #include <ctime>
  11. #include <complex>
  12. #include <cassert>
  13. #include <utility>
  14. #include <cmath>
  15. #include <cstdlib>
  16. #include <ctype.h>
  17. #include <functional>
  18. #include <iomanip>
  19. using namespace std;
  20. int main()
  21. {
  22. map<string ,long long> m;
  23. string s;
  24. long long sum =0;
  25. while(getline(cin,s))
  26. {
  27. ++m[s];
  28. ++sum;
  29. }
  30. map<string ,long long>::iterator iter;
  31. for(iter = m.begin();iter != m.end();iter++)
  32. {
  33. cout<<setprecision(4)<<fixed<<iter ->first<<" "<<100*(iter -> second)/(1.0*sum)<<endl;
  34. }
  35. system("pause");
  36. return 0;
  37. }

代码2:字典树

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <string>
  6. #include <map>
  7. #include <stack>
  8. #include <vector>
  9. #include <set>
  10. #include <ctime>
  11. #include <complex>
  12. #include <cassert>
  13. #include <utility>
  14. #include <cmath>
  15. #include <cstdlib>
  16. #include <ctype.h>
  17. #include <functional>
  18. #include <iomanip>
  19. struct Trie
  20. {
  21. int cnt;//字符串出现的总个数
  22. char n[45];//保存的字符串
  23. bool flag;//是不是走到了字符串的最后一个字母
  24. struct Trie *next[130];//子节点,ASCII最大值126
  25. Trie():cnt(0),flag(false){memset(next,NULL,sizeof(next));}
  26. }root;
  27. int n = 0;
  28. void Trie_insert(char *s)
  29. {
  30. int len=strlen(s);
  31. Trie*p=&root;
  32. for(int i=0;i<len;i++)
  33. {
  34. int id=s[i];
  35. if(p->next[id]==NULL)
  36. p->next[id]=new Trie;
  37. p=p->next[id];
  38. }
  39. p->cnt++;//走到字符串的最后一个字母的节点
  40. strcpy(p->n,s);
  41. p->flag=1;
  42. }
  43. void dfs(Trie *root)
  44. {
  45. Trie *p = root;
  46. if(p -> flag)
  47. cout<<p -> n<<" "<<setprecision(4)<<fixed<<p -> cnt*100.0/n<<endl;
  48. for(int i=0;i<127;i++)
  49. {
  50. if(p -> next[i] != NULL)
  51. dfs(p -> next[i]);
  52. }
  53. }
  54. int main()
  55. {
  56. char s[35];
  57. n = 0;
  58. while(gets(s))
  59. {
  60. Trie_insert(s);
  61. n++;
  62. }
  63. dfs(&root);
  64. return 0;
  65. }

代码3:二叉排序树

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <string>
  6. using namespace std;
  7. const int MAX_NUM = 10001;
  8. struct node{
  9. string name;
  10. int count;
  11. node *lchild,*rchild;
  12. node(char* s) : name(s),count(1),lchild(NULL),rchild(NULL){;}
  13. };
  14. void insert(node*& root,char* s){
  15. if(root == NULL){
  16. root = new node(s);
  17. return;
  18. }
  19. node *self=root,*father=root;
  20. int cmpres;
  21. while(self!=NULL){
  22. cmpres = strcmp(s,self->name.c_str());
  23. if(cmpres == 0){
  24. self->count++;
  25. return;
  26. }
  27. father = self;
  28. self = (cmpres > 0)? father->rchild : father->lchild;
  29. }
  30. self = new node(s);
  31. if(cmpres > 0){
  32. father -> rchild = self;
  33. }
  34. else if(cmpres < 0){
  35. father -> lchild = self;
  36. }
  37. }
  38. void print(const node* p,const int count){
  39. if(p!=NULL){
  40. print(p->lchild,count);
  41. printf("%s %.4f\n",p->name.c_str(), 100.0 * p->count / count);
  42. print(p->rchild,count);
  43. }
  44. }
  45. int main(){
  46. char tree_name[32],tmpc;
  47. node* root=NULL;
  48. int count=0;
  49. while(scanf("%30[^\n]",tree_name) != EOF){
  50. getchar();
  51. insert(root,tree_name);
  52. ++count;
  53. }
  54. print(root,count);
  55. return 0;
  56. }

发表评论

表情:
评论列表 (有 0 条评论,48人围观)

还没有评论,来说两句吧...

相关阅读

    相关 poj 1009

    参考自大神思路:[POJ 1009][] 首先,暴力必挂,这是题目的善意提醒。 于是,一直在想不暴力的各种判断计算方法,关于各种跳跃移动,后来都无奈想用STL。原谅我的