poj 2418 -- Hardwood Species

守給你的承諾、 提交于 2020-02-15 00:57:51
Hardwood Species
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 18174   Accepted: 7206

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.
 
思路:统计出现的百分比。字典树水过。
 
 1 /*======================================================================
 2  *           Author :   kevin
 3  *         Filename :   HardwoodSpecies.cpp
 4  *       Creat time :   2014-07-29 10:53
 5  *      Description :
 6 ========================================================================*/
 7 #include <iostream>
 8 #include <algorithm>
 9 #include <cstdio>
10 #include <cstring>
11 #include <queue>
12 #include <cmath>
13 #define clr(a,b) memset(a,b,sizeof(a))
14 #define M 128
15 using namespace std;
16 struct Trie{
17     Trie *next[M];
18     int cnt;
19     //int v;
20 };
21 Trie *root;
22 int num,z;
23 char ss[35];
24 void CreateTrie(char *str)
25 {
26     int len = strlen(str);
27     Trie *p= root,*q;
28     for(int i = 0; i < len; i++){
29         int id = str[i];
30         if(p->next[id] == NULL){
31             q = (Trie *)malloc(sizeof(Trie));
32             //q->v = 1;
33             for(int j = 0; j < M; j++){
34                 q->next[j] = NULL;
35                 q->cnt = 0;
36             }
37             p->next[id] = q;
38             p = p->next[id];
39         }
40         else{
41             //p->next[id]->v++;
42             p = p->next[id];
43         }
44     }
45     //p->v = -1;
46     p->cnt++;
47 }
48 void Count(Trie * tr)
49 {
50     if(tr->cnt){
51         printf("%s %.4lf\n",ss,100*((double)tr->cnt / (double)num));
52     }
53     for(int i = 0; i < M; i++){
54         if(tr->next[i]){
55             ss[z++] = i;
56             ss[z] = '\0';
57             Count(tr->next[i]);
58             z--;
59         }
60     }
61 }
62 int DealTrie(Trie *T)
63 {
64     if(T == NULL) return 0;
65     for(int i = 0; i < M; i++){
66         if(T->next[i])
67             DealTrie(T->next[i]);
68     }
69     free(T);
70     return 0;
71 }
72 int main(int argc,char *argv[])
73 {
74     root = (Trie*)malloc(sizeof(Trie));
75     for(int i = 0; i < M; i++){
76         root->next[i] = NULL;
77         root->cnt = 0;
78     }
79     char str[35];
80     num = 0;
81     z = 0;
82     while(gets(str) != NULL){
83         num++;
84         CreateTrie(str);
85     }
86     Count(root);
87     DealTrie(root);
88     return 0;
89 }
View Code

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!