当前位置:编程学习 > C/C++ >>

POJ 2418 Hardwood Species

Hardwood Species
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 14326 Accepted: 5814
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
 
Waterloo Local 2002.01.26
考察点是tire树,但是这题确是非常容易出错因为看他给的的样例就会给人一种错觉,认为字符在A到Z a到z  空格之间,其实题目并没有这么说,树木的名称可能有其他字符 ,认识到这一点就能过了这题目.
[cpp] 
#include <stdio.h>  
#include <string.h>  
#include <stdlib.h>  
char s1[50];  
struct tire  
{  
    int tag;  
    struct tire* next[300];  
};  
struct num  
{  
    char s1[50];  
    int sum;  
}a[1000010];  
int top;  
int cmp(const void *e,const void *f)  
{  
    struct num *p1,*p2;  
    p1=(struct num *)e;  
    p2=(struct num *)f;  
    if(strcmp(p1->s1,p2->s1)>0)  
    {  
        return 1;  
    }else  
    {  
        return -1;  
    }  
}  
int main()  
{  
    struct tire* newnode();  
    int build(char s2[50],struct tire* p);  
    int i,j,n,m,t;  
    double s;  
    struct tire *head;  
    head=newnode(); top=0; s=0;  
    while(gets(s1))  
    {  
        t=build(s1,head); s++;  
        if(t==-1)  
        {  
            a[top].sum=1;  
            strcpy(a[top].s1,s1);  
            top++;  
        }else  
        {  
            a[t].sum++;  
        }  
    }  
    qsort(a,top,sizeof(a[0]),cmp);  
    for(i=0;i<=top-1;i++)  
    {  
        printf("%s ",a[i].s1);  
        printf("%.4lf\n",a[i].sum*100/s);  
    }  
    return 0;  
}  
struct tire* newnode()  
{  
    int i;  
    struct tire *p=(struct tire*)malloc(sizeof(struct tire));  
    p->tag=-1;  
    for(i=0;i<=299;i++)  
    {  
        p->next[i]=NULL;  
    }  
    return p;  
}  
int build(char s2[50],struct tire* p)  
{  
    int i,l=strlen(s2),s;  
    for(i=0;i<=l-1;i++)  
    {  www.zzzyk.com
        s=s2[i];  
        if(p->next[s]==NULL)  
        {  
           p->next[s]=newnode();  
        }  
        p=p->next[s];  
    }  
    if(p->tag==-1)  
    {  
        p->tag=top;  
        return -1;  
    }  
    return p->tag;  
}  
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,