Poj 2418 hardwood species (trie tree map qsort)

Source: Internet
Author: User
Hardwood Species
Time limit:10000 ms   Memory limit:65536 K
Total submissions:15439   Accepted:6190

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
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.

Source

Waterloo local 2002.01.26

This topic uses three methods, that is, to compare the time difference between the three methods, to see if the good algorithm can actually improve the running time.

Sure enough.

Method 1: trie tree

This topic uses the trie tree as a direct idea. Because string search is involved, searching in the trie tree should be faster.

Another point is to output data in Lexicographic Order. The result of the first traversal of the trie tree is sequential.

In addition, the trie tree is used to record the number of occurrences of each string at the same time of insertion. The result can be output directly when the final output result is traversed, which is very convenient!

Date: 797 Ms

#include <iostream>#include <stdlib.h>#include <string.h>#include <stdio.h>using namespace std;struct node{node *next[128];int num;node(){memset(next,0,sizeof(next));num=0;}}re_root;int count;char global_name[100];int insert_trie(node *root,char *name){if(name[0]==0){root->num++;return 0;}if(root->next[name[0]]!=NULL)insert_trie(root->next[name[0]],name+1);else{root->next[name[0]]=new node();insert_trie(root->next[name[0]],name+1);}return 0;}int tri_reverse(node *root,int k){if(root->num>0){global_name[k]=0;printf("%s %.4lf\n",global_name,100*double(root->num)/count);}int i;for(i=0;i<128;i++)if(root->next[i]!=NULL){global_name[k]=i;tri_reverse(root->next[i],k+1);}return 0;}int main(){char name[31];count=0;while(gets(name)!=NULL){count++;insert_trie(&re_root,name);}if(count==0)return 0;tri_reverse(&re_root,0);return 0;}

Method 2: Sort

In fact, this method is not easy to think of, because the question gave 10 s and 100000 of the data volume, in fact, after trying to find that you can pass

There is nothing to say about this idea. I guess the reason why the sorting can be done is that the following code has a seemingly inconspicuous small optimization.

One of the biggest overhead in the sorting process is the movement of elements and the movement of strings, which is time-consuming.

The solution I used is to sort all the strings by pointers. The original strings do not move, so that the string can be sorted quickly!

Date: 2500 Ms

#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;char name[1000001][31];char *rec[1000001];int cmp(const void *a,const void *b){return strcmp(*((char**)a),(*(char **)b));}int main(){int k;int i=0;int j;while(gets(name[i])!=NULL){rec[i]=name[i];i++;}qsort(rec,i,sizeof(rec[0]),cmp);k=1;for(j=1;j<i;j++){if(strcmp(rec[j],rec[j-1])!=0){printf("%s %.4lf\n",rec[j-1],100*double(k)/i);k=1;}elsek++;}if(strcmp(rec[i-1],rec[i-2])!=0){printf("%s %.4lf\n",rec[i-1],100*double(1)/i);}elseprintf("%s %.4lf\n",rec[i-1],100*double(k)/i);return 0;}

Method 3: Map

To be honest, when I first saw this question, I thought of using map. I was also afraid of timeout. I tried map after trying to sort it.

Indeed, it can pass, and the time is faster than sorting. The map uses the red and black trees, and the speed is also impatient! The key is that the Code is short, just a few minutes

Complete the code AC!

Date: 1532 Ms

#include <string.h>#include <stdio.h>#include <algorithm>#include <iostream>#include <string>#include <map>using namespace std;map<string,int> m;int main(){//string s;char s[100];int count=0;while(gets(s)!=NULL){count++;m[s]++;}map<string,int>::iterator it;for(it=m.begin();it!=m.end();it++){cout<<(*it).first<<' ';printf("%.4lf\n",100*double((*it).second)/count);}return 0;}

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.