POJ 2418 Hardwood Species (Dictionary tree)
Hardwood Species
Time Limit:10000 MS |
|
Memory Limit:65536 K |
Total Submissions:20085 |
|
Accepted:7911 |
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 2x4 s and 2x6 s, with some limited decorative applications.
Using satellite imaging technology, the Department of Natural Resources has compiled an inventory of every tree standing on a particle 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.
Give a group of trees of various species, and then output the proportion of the total number of trees.
Dictionary tree.
This question is a bit pitfall. If you add spaces to 26 upper and lower case letters, you should try again. In fact, the data it gives is not only letters and spaces, is all characters. That is, your dictionary tree is a 126-32 + 1 = 95 cross-tree. For convenience, it is 0 ~ 126 (127 Cross Tree)
The number of occurrences is maintained by the variable cnt on the node.
Output by Lexicographic Order: uses the tree's central order traversal + backtracking.
It is not difficult but a good question. The following is the dictionary tree version:
Memory: 3092 K Time: 782 MS
# Include
# Include
# Include
Const int maxn = 127; // 127 Cross 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
Ch [id] = 0) {node * t = (node *) malloc (sizeof (node); for (j = 0; j
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] = '\ 0'; res = (double) T-> cnt/tot * 100; printf ("% s %. 4f \ n ", name, res) ;}for (I = 0; I
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
Ch [I] = 0; T-> cnt = 0; tot = 0; while (gets (str) {if (str [0] = '\ 0 ') break; insert (str); tot ++;} ask (T, 0); return 0 ;}
This map can also be used:
Memory: 916 K Time: 8735 MS
#include
#include
#include
#include
#include
using namespace std;int main(){string str;int tot=0;map
mp;while(getline(cin,str)){mp[str]++;tot++;}map
::iterator it;for(it=mp.begin();it!=mp.end();it++){cout<
first;printf(" %.4f\n",(double)it->second/tot*100);}return 0;}
Fast sorting is also possible ..