POJ 2418 Hardwood Species(trie 樹 MAP qsort)

來源:互聯網
上載者:User
Hardwood Species
Time Limit: 10000MS   Memory Limit: 65536K
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 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.

Source

Waterloo Local 2002.01.26

這個題目用了三種方法,就是想對比一下三種方法啟動並執行時間差別,看看是否利用好的演算法就能真正的提高已耗用時間

果然這樣

方法一:trie樹

這個題目用trie樹來做是比較直接的一種想法,因為涉及到字串的尋找,這樣在trie樹中尋找應該是比較快的了

還有一點就是要按照字典序輸出,那麼想一下trie樹的先順遍曆結果就是順序的

還有就是利用trie樹在插入的同時就把每個字串出現的次數記錄下來的,在最後輸出結果遍曆的時候直接輸出結果就可以了,非常方便!

時間:797ms

#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;}

方法二:排序

其實這個方法不容易想到,因為被這個題目的給的時間10s和100000的資料量嚇住了,其實試了之後發現可以通過

這個思想就沒什麼好說的了,我估計排序能過的原因還是下面代碼有個看似不起眼的小最佳化

因為在排序過程中最大的開銷之一就是元素的移動,而且是字串的移動,那麼就比較耗時

我採取的方案是全部按照指標來排序,原來的字串的位置不動,這樣就實現很快速的字串排序!

時間:2500ms

#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;}

方法三:map

說實話,在看到這個題目的第一眼就想到用map,同樣是怕逾時,在嘗試排序通過之後就嘗試了map

果然能通過,而且時間還比排序快點,map裡面用的是紅/黑樹狀結構,速度也不耐!關鍵是代碼短,幾分種就

完成代碼AC掉!

時間:1532ms

#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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.