Hardwood Species
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 20877 | Accepted: 8186 |
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.
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
字典树
</pre><pre name="code" class="cpp">#include<cstdio> //c++ 750ms<span id="transmark"></span>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
int num;
node *next[130];
}*head,*p;
struct N
{
char name[35];
}str[10010];
node* Creat()
{
node *p;
p=new node;
p->num=0;
memset(p->next,0,sizeof(p->next));
return p;
}
int len,flag;
int Build(char s[])
{
p=head;
len=strlen(s);
for(int i=0;i<len;i++)
{
int x=s[i];
if(!p->next[x])
{
p->next[x]=Creat();
}
p=p->next[x];
}
if(p->num==0)
{
p->num++;
return 1;
}
else
{
p->num++;
return 0;
}
}
int Query(N pp)
{
p=head;
len=strlen(pp.name);
for(int i=0;i<len;i++)
{
int x=pp.name[i];
p=p->next[x];
}
return p->num;
}
bool cmp(N p1,N p2)
{
return strcmp(p1.name,p2.name)<0;
}
int main()
{
char s[35];
head=Creat();
int t=0,num=0;
while(gets(s)!=NULL)
{
flag=Build(s);
if(flag)
{
strcpy(str[num].name,s);
num++;
}
t++;
}
sort(str,str+num,cmp);
for(int i=0;i<num;i++)
{
int x=Query(str[i]);
printf("%s %.4f\n",str[i].name,100.0*x/t);
}
}
Map
#include <iostream> //交C++为 CE
#include <stdio.h>
#include <iomanip>
#include <string.h>
#include <map>
using namespace std;
int main()
{
std::ios::sync_with_stdio(false);//不加会8000ms,加了2100ms
map<string,int>mp;
int cnt=0;
string s;
while(getline(cin,s))
{
mp[s]++;
cnt++;
}
//迭代器就是指针。指向这个关联容器啊,关联容器有键和值,first就是键,second就是值。
map<string,int >::iterator i; //比单纯的输出map[]快很多
cout.setf(ios::fixed); //控制输出格式
for(i=mp.begin();i!=mp.end();i++)
{
cout<<setprecision(4)<<i->first<<" "<<100.0*(i->second)/cnt<<endl;
}
return 0;
}
/*
#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <string.h>
#include <map>
using namespace std;
int main()
{
map<string,int>mp;
int cnt=0;
char s[100];
string ss;
while(gets(s)) //在G++中 单纯将char类型存入map中会超时,但将char存入string类中会跑2400ms
{
ss=s;
mp[ss]++;
cnt++;
}
// while(getline(cin,s))
// {
// mp[s]++;
// cnt++;
// }
map<string,int >::iterator i;
cout.setf(ios::fixed);
for(i=mp.begin();i!=mp.end();i++)
{
cout<<setprecision(4)<<i->first<<" "<<100.0*(i->second)/cnt<<endl;
}
return 0;
}
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
//#define RR freopen("in.txt","r",stdin);
#define INF 0x3f3f3f3f
using namespace std;
char s1[39];
string s[10010],d;
bool cmp(string s1,string s2)
{
return s1<s2;
}
int main()
{
std::ios::sync_with_stdio(false); //用于纯C++的输入输出(cin,cout)//3900ms
map<string,int>Q;
int num=0,t=0;
while(gets(s1)!=NULL)
{
d=s1;
t++;
Q[d]++;
if(Q[d]==1)
s[num++]=d;
}
sort(s,s+num,cmp);
cout.setf(ios::fixed);
for(int i=0;i<num;i++)
{
//double ss=Q[s[i]]*100.00000/t;
cout<<s[i]<<" "<<setprecision(4)<<Q[s[i]]*100.00000/t<<"\n";
//printf(" %.4f\n",ss);
}
// for(int i=0;i<num;i++)
// {
// double ss=Q[s[i]]*100.00000/t;
// cout<<s[i];
// printf(" %.4f\n",ss);
// }
}<span id="transmark"></span>
*/