Hardwood species
| Time Limit: 10000MS |
|
Memory Limit: 65536K |
| Total Submissions: 20085 |
|
Accepted: 7911 |
Description
Hardwoods is the botanical group of trees that has 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 char Acteristics. Although oak, maple and cherry All is types of hardwood trees, for example, they is different species. Together, all the hardwood species represent-percent of the trees in the "states".
On the other hand, softwoods, or conifers, from the Latin word meaning "cone-bearing," has needles. Widely available US softwoods include cedar, fir, hemlock, pine, redwood, spruce and cypress. In a home, the softwoods is used primarily as structural lumber such as 2x4s and 2x6s, with some limited decorative appli Cations.
Using Satellite imaging technology, the Department of Natural Resources have compiled an inventory of every tree standing O n a particular day. You is 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 the every tree observed by the satellite; One tree per line. No species name exceeds-characters. There is no more than 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 pop Ulation 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.8966 Sassafras 3.4483Soft Maple 3.4483Sycamore 3.4483White Oak 10.3448Willow 3.4483Yellow Birch 3.4483
Hint
This problem have huge input, use scanf instead of CIN to avoid time limit exceeded.
The main idea : to give a group of species of trees, and then output the proportion of the total of each tree.
It's a dictionary tree.
This problem is a bit of a pit, if you press the size of 26 letters plus a space to deal with, then you re to die, in fact, it gives the data is not only letters and spaces, is all characters. That is, your dictionary tree is a 126-32+1=95 fork tree, in order to write convenient is 0~126 (127 fork Tree)
The number of occurrences is maintained by the variable CNT on the node.
output each character in dictionary order: using the tree's middle order traversal + backtracking.
not difficult and good problem, the following is the Dictionary tree version:
Memory: 3092K time : 782MS
#include <stdio.h> #include <stdlib.h> #include <string.h>const int maxn=127;//127 fork Tree typedef struct Node{int cnt;struct Node *CH[MAXN];} Node;node *t;int tot;void Insert (char s[]) {int I,j,len=strlen (s); node *u;u=t;for (i=0;i<len;i++) {int id=s[i];if (U- >ch[id]==0) {node *t= (node*) malloc (sizeof (node)); for (j=0;j<maxn;j++) T->ch[j]=0;t->cnt=0;u->ch[id] =t;} U=u->ch[id];} u->cnt++;} Char name[40];void ask (node* t,int cur) {if (t==0) return; int i;double res;if (t->cnt) {name[cur]= '} '; res= (double) t-& gt;cnt/tot*100;printf ("%s%.4f\n", name,res);} for (i=0;i<maxn;i++) if (t->ch[i]!=0) {name[cur]=i;ask (t->ch[i],cur+1);} return;} int main () {int I,res,len;char str[40]; t= (node*) malloc (sizeof (node)); for (i=0;i<maxn;i++) t->ch[i]=0; T->cnt=0;tot=0;while (gets (str)) {if (str[0]== ') Break;insert (str); tot++;} Ask (t,0); return 0;}
This map can also be:
Memory: 916K time : 8735MS
#include <iostream> #include <cstdio> #include <iterator> #include <map> #include <string >using namespace Std;int Main () {string Str;int tot=0;map<string,int> mp;while (getline (cin,str)) {mp[str]++; tot++;} Map<string,int>::iterator it;for (It=mp.begin (); It!=mp.end (); it++) {cout<<it->first;printf ("%.4f\n ", (double) it->second/tot*100);} return 0;}
Fast-line can also be too.
POJ 2418 Hardwood species (dictionary tree)