Chinaunix首页 | 论坛 | 博客
  • 博客访问: 343085
  • 博文数量: 122
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 1191
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 11:12
文章分类

全部博文(122)

文章存档

2010年(122)

我的朋友

分类: C/C++

2010-04-22 02:05:25

一、问题描述

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.

 

二、解题思路

使用trie树进行求解,然后进行深度优先遍历。这题比较郁闷的地方就是输入中除了字母和空格外还有其他字符,提交了好多次。所在trie树的节点在定义得足够大。我给了256个指针。提交成功,922ms。速度还算快吧。

三、代码

 

#include<iostream>
using namespace std;
struct trieNode
{
    int next[256];
    int sum;
    bool isword;
    //char ss[35];
    trieNode(){sum=0;isword=false;memset(next,-1,sizeof(next));}
};
trieNode tt[50000];
int len=1;
int SUM=0;
char s[50];
int L=0;
void insert(char * fw,char * tar)
{
    trieNode * p= &tt[0];
    int id ;
    while(*tar)
    {
        //id=*tar-'a';

        id=*tar;
        if(p->next[id]==-1)
        {
            p->next[id]=len;
            len++;
        }
        p=&tt[p->next[id]];
        tar++;
    }
    p->isword=true;
    //strcpy(p->ss,fw);

    p->sum++;
}
//遍历字母树

void DFS(int n,int l)
{
    if(tt[n].isword==true)
    {
        s[l]='\0';
        printf("%s %.4f\n",s,100.0*tt[n].sum/SUM);
    }
    int i;
    for(i=0;i<256;++i)
    {
        if(tt[n].next[i]!=-1)
        {
            s[l]=i;
            DFS(tt[n].next[i],l+1);
        }
    }
}
int main()
{
    char str[50];
    char s[50];
    len=1;
    SUM=0;
    while(gets(str)!=NULL)
    {
        strcpy(s,str);
        insert(s,str);
        SUM++;
    }
    L=0;
    DFS(0,0);
    return 0;
}


四、Map实现
 

#include<iostream>
#include<map>
#include<string>
using namespace std;
string str;
map<string,int> mp;
map<string,int>::iterator iter,end;
int main()
{
    int n=0;
    while(getline(cin,str))
    {
        mp[str]++;
        ++n;
    }
    iter=mp.begin();
    end=mp.end();
    while(iter!=end)
    {
        cout<<iter->first;
        printf(" %.4lf\n",100.0*(iter->second)/(double)n);
        ++iter;

    }
}

五、二叉检索树实现


#include<iostream>
using namespace std;
struct BSTNode
{
    char name[35];
    int sum;
    int left;
    int right;
    BSTNode()
    {
        strcpy(name,"");
        sum=0;
        left=-1;
        right=-1;
    }
};
int SUM;
BSTNode T[10005];
int len;
void Insert(char * str)
{

    if(len==0)
    {
        strcpy(T[len].name,str);
        T[len].sum++;
        len++;
        return ;
    }
    int p=0;
    while(p!=-1)
    {
        int res=strcmp(str,T[p].name);
        if(res==0)
        {
            T[p].sum++;
            return ;
        }
        else
            if(res>0)
            {
                if(T[p].right==-1)
                {
                    T[p].right=len;
                    strcpy(T[len].name,str);
                    T[len].sum++;
                    len++;
                    return ;
                }
                else
                    p=T[p].right;
            }
            else
            {
                if(T[p].left==-1)
                {
                    T[p].left=len;
                    strcpy(T[len].name,str);
                    T[len].sum++;
                    len++;
                    return ;
                }
                else
                    p=T[p].left;

            }
    }

}
void InOrderTravel(int n)
{
    if(n!=-1)
    {
        InOrderTravel(T[n].left);
        printf("%s %.4f\n",T[n].name,100.0*T[n].sum/(double)SUM);
        InOrderTravel(T[n].right);
    }

}
int main()
{
    char str[50];
    len=0;
    SUM=0;
    while(gets(str)!=NULL)
    {
        //if(strcmp(str,"XXX")==0)

            //break;

        Insert(str);
        SUM++;
    }
    InOrderTravel(0);

    return 0;
}


阅读(1075) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~