POJ2418----Hardwood Species

来源:互联网 发布:淘宝自动下架 编辑:程序博客网 时间:2024/05/17 02:42
Hardwood Species
Time Limit: 10000MS Memory Limit: 65536KTotal Submissions: 20874 Accepted: 8186

统计每个单词出现的频率;

可以用map水过!

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.4483map版;
Source CodeProblem: 2418User: 14110103069Memory: 484KTime: 1547MSLanguage: C++Result: Accepted    Source Code    #include <iostream>    #include<cstdio>    #include<cstring>    #include<algorithm>    #include<map>    #include<string>    #include<iomanip>    using namespace std;    struct node    {        char s[32];    }que[100010];    bool cmp(node a,node b)    {        return strcmp(a.s,b.s)<0;    }    int main()    {        ios::sync_with_stdio(0);        map<string,int>Q;        int cnt=0,k=0,g;        node c;        while(gets(c.s))        {            g=Q[c.s]++;            if(g==0)            {                 que[k++]=c;            }            cnt++;        }        sort(que,que+k,cmp);        for(int i=0;i<k;i++)        {            cout<<que[i].s<<" "<<fixed<<setprecision(4)<<Q[que[i].s]*100.0/cnt<<endl;        }        return 0;    }


还有;
Source CodeProblem: 2418User: 14110103069Memory: 3152KTime: 3125MSLanguage: C++Result: Accepted    Source Code    #include <iostream>    #include<cstdio>    #include<cstring>    #include<algorithm>    #include<map>    #include<string>    #include<iomanip>    using namespace std;    struct node    {        string s;    }que[100010];    bool cmp(node a,node b)    {        return a.s<b.s;    }    int main()    {        ios::sync_with_stdio(0);        map<string,int>Q;        int cnt=0,k=0,g;        node c;        while(getline(cin,c.s))        {            g=Q[c.s]++;            if(g==0)            {                 que[k++]=c;            }            cnt++;        }        sort(que,que+k,cmp);        for(int i=0;i<k;i++)        {            cout<<que[i].s<<" "<<fixed<<setprecision(4)<<Q[que[i].s]*100.0/cnt<<endl;        }        return 0;    }

字典书版本;
Source CodeProblem: 2418User: 14110103069Memory: 1568KTime: 719MSLanguage: C++Result: Accepted    Source Code    #include <iostream>    #include<cstdio>    #include<cstring>    #include<queue>    #include<stack>    #include<cstring>    #include<algorithm>    #include<map>    using namespace std;    const int maxn=1e5;    struct node    {        int cont;        node *next[130];    }T[maxn+10];    struct n    {        char s[32];    }zifu[maxn];    int top;char s[maxn+10][32];    node *creat()    {        node *p=&T[top++];        p->cont=0;        memset(p->next,0,sizeof(p->next));        return p;    }    bool build(char *t,node *rt)    {        int j,len=strlen(t);        node *p=rt;        for(int i=0;i<len;i++)        {            j=t[i];            if(p->next[j]==NULL)                p->next[j]=creat();            p=p->next[j];        }        if(p->cont==0)        {            p->cont++;            return 1;        }        else        {            p->cont++;            return 0;        }    }    int Query(char *t,node *rt)    {        node *p=rt;        int j,len=strlen(t);        for(int i=0;i<len;i++)        {            j=t[i];            p=p->next[j];        }        return p->cont;    }    bool cmp(n a,n b)    {        return strcmp(a.s,b.s)<0;    }    int main()    {        top=0;        node *rt=creat();        bool flag;        char t[32];        int i=0;        int sum=0;        while(gets(t)!=NULL)        {            flag=build(t,rt);           if(flag)            {                strcpy(zifu[i].s,t);                 i++;            }            sum++;        }        int pos=i;        sort(zifu,zifu+pos,cmp);        for(int i=0;i<pos;i++)        {            printf("%s",zifu[i].s);            int x=Query(zifu[i].s,rt);            printf(" %.4f\n",x*1.0/sum*100);        }        return 0;    }

 
0 0
原创粉丝点击