首页 > 代码库 > poj 2418 -- Hardwood Species
poj 2418 -- Hardwood Species
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.
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 AlderAshAspenBasswoodAshBeechYellow BirchAshCherryCottonwoodAshCypressRed ElmGumHackberryWhite OakHickoryPecanHard MapleWhite OakSoft MapleRed OakRed OakWhite OakPoplanSassafrasSycamoreBlack WalnutWillow
Sample Output
Ash 13.7931Aspen 3.4483Basswood 3.4483Beech 3.4483Black Walnut 3.4483Cherry 3.4483Cottonwood 3.4483Cypress 3.4483Gum 3.4483Hackberry 3.4483Hard Maple 3.4483Hickory 3.4483Pecan 3.4483Poplan 3.4483Red Alder 3.4483Red Elm 3.4483Red Oak 6.8966Sassafras 3.4483Soft Maple 3.4483Sycamore 3.4483White Oak 10.3448Willow 3.4483Yellow 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 12815 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 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。