[ACM] POJ 2418 Hardwood species (trie tree or map)

Source: Internet
Author: User

Hardwood species
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 17986 Accepted: 7138

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.

Source

Waterloo Local 2002.01.26


Problem Solving Ideas:

Gives a series of strings. Have the same, ask how many times each string appears as a percentage of the total number of strings, and the output is output by the dictionary order of the strings.

The first method: using the STL inside the map<string, int; int is the number of occurrences. More importantly, the map is the default string by the dictionary order from small to large, so the output is directly from beginning to end.

Another way: Create a trie tree, and insert each string into the tree for the input. The last letter node of each string in the tree holds the number of occurrences of the string, with an OK flag that the node is not the last letter node of a string.

Note: It's been a long time and it's been found that using string to save a string is much slower than using a character array ...

In the future, we will use a character array to save the string.

The first method of code:

#include <iostream> #include <stdio.h> #include <iomanip> #include <string.h> #include <map >using namespace Std;int Main () {    map<string,int>mp;    int cnt=0;    string S;    while (Getline (cin,s))    {        mp[s]++;        cnt++;    }    Map<string,int >::iterator i;    For (I=mp.begin (); I!=mp.end (); i++)    {        cout<<setiosflags (ios::fixed) <<setprecision (4) < <i->first<< "" <<100.0* (I->second)/cnt<<endl;    }    return 0;}

Another method code:

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include < Algorithm> #include <iomanip>using namespace Std;int num;//total number of strings struct trie{int cnt;//The total number of occurrences of a string char NA    me[40];//saved string bool ok;//is not going to the last letter of the string, the node that holds the last letter also holds the entire string name Trie *next[127];//child node, the ASCII maximum is 126 Trie ()        {ok=0;        cnt=0;    for (int i=0;i<127;i++) next[i]=null;    }}root;void Create (char s[]) {int Len=strlen (s);    trie*p=&root;        for (int i=0;i<len;i++) {int id=s[i];        if (p->next[id]==null) p->next[id]=new Trie;    p=p->next[id];    } p->cnt++;//The last letter of the string to the node strcpy (p->name,s); P->ok=1;}    void Dfs (Trie *root)//recursive output {trie*p=root; if (P->ok) cout<<p->name<< "" <<setiosflags (ios::fixed) <<setprecision (4) <<    100.0*p->cnt/num<<endl; for (int i=0;i<127;i++) {if (p->next[i]!=null) {DFS (p->next[i]);    }}}int Main () {char s[40];        while (gets (s)) {create (s);    num++;    } dfs (&root); return 0;}



Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

[ACM] POJ 2418 Hardwood species (trie tree or map)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.