1. 程式人生 > >[ACM] POJ 2418 Hardwood Species (Trie樹或者map)

[ACM] POJ 2418 Hardwood Species (Trie樹或者map)

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

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 Alder
Ash
Aspen
Basswood
Ash
Beech
Yellow Birch
Ash
Cherry
Cottonwood
Ash
Cypress
Red Elm
Gum
Hackberry
White Oak
Hickory
Pecan
Hard Maple
White Oak
Soft Maple
Red Oak
Red Oak
White Oak
Poplan
Sassafras
Sycamore
Black Walnut
Willow

Sample Output

Ash 13.7931
Aspen 3.4483
Basswood 3.4483
Beech 3.4483
Black Walnut 3.4483
Cherry 3.4483
Cottonwood 3.4483
Cypress 3.4483
Gum 3.4483
Hackberry 3.4483
Hard Maple 3.4483
Hickory 3.4483
Pecan 3.4483
Poplan 3.4483
Red Alder 3.4483
Red Elm 3.4483
Red Oak 6.8966
Sassafras 3.4483
Soft Maple 3.4483
Sycamore 3.4483
White Oak 10.3448
Willow 3.4483
Yellow Birch 3.4483

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceeded.

Source

解題思路:

給出一系列的字串,有相同的,問每個字串出現的次數站總字串個數的百分比是多少,輸出的時候按字串的字典序輸出。

第一種方法:用STL裡面的map<string ,int >, first為字串,int為出現的個數,更重要的是map是預設string按字典序從小到大排列的,所以輸出的時候直接從頭到尾就可以了。

第二種方法:建立Trie樹,對於輸入的每一個字串都插入到樹中,每個字串在樹中的最後一個字母節點儲存著該字串出現的次數,並用一個ok標記該節點是不是一個字串的最後一個字母節點。

注意:折騰了很長時間,發現用string 儲存字串比用 字元陣列慢的太多太多。。。以後還是用字元陣列來儲存字串把。

第一種方法程式碼:

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

第二種方法程式碼:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <iomanip>
using namespace std;
int num;//字串的總個數

struct Trie
{
    int cnt;//某個字串出現的總個數
    char name[40];//儲存的字串
    bool ok;//是不是走到了字串的最後一個字母,儲存最後一個字母的那個節點也儲存著整個字串的Name
    Trie *next[127];//子節點,ASCII最大值為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++;//走到字串的最後一個字母的節點
    strcpy(p->name,s);
    p->ok=1;
}

void dfs(Trie *root)//遞迴輸出
{
    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;
}