Hardwood Species
| Time Limit: 10000MS | Memory Limit: 65536K | |
| Total Submissions: 24104 | Accepted: 9355 |
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
//#include<iostream>
//#include<cstring>
//#include<map>
//#include<cstdio>
//#include<iomanip>
//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 it;
// for(it=mp.begin();it!=mp.end();it++)
// {
// cout<<fixed<<setprecision(4)<<it->first<<" "<<100.0*it->second/cnt<<endl;
// }
//}
//#include <iostream>
//#include <algorithm>
//#include <cstdio>
//#include <cstring>
//#include <cstdlib>
//using namespace std;
//struct node
//{
// char s[35];
// int n;
//}que[10101];
//struct node2
//{
// int next[135];
// int num;
//}d[100100];
//int top,num;
//bool cmp(node a,node b)
//{
// return strcmp(a.s,b.s)<0;
//}
//int creat(char *str)
//{
// int j=0,len=strlen(str),i;
// for (i=0;i<len;i++)
// {
// int x=str[i];
// if(d[j].next[x]==-1)
// d[j].next[x]=++top;
// j=d[j].next[x];
// }
// if(d[j].num==-1)
// {
// d[j].num=++num;
// strcpy(que[num].s,str);
// }
// return d[j].num;
//}
//int main()
//{
// memset(d,-1,sizeof(d));
// char str[35];
// int i;
// num=-1;top=0;
// for (i=1;i<10101;i++)
// {
// que[i].n=0;
// }
// int sum=0;
// while(gets(str)!=NULL)
// {
// int x=creat(str);
// que[x].n++;
// sum++;
// }
// sort(que,que+num+1,cmp);
// for (i=0;i<=num;i++)
// {
// printf("%s %.4f\n",que[i].s,que[i].n*1.0/sum*100);
// }
// return 0;
//}
#include<cstring>
#include<iostream>
#include<cstdio>
#include<iomanip>
using namespace std;
int num;//字符串的总个数
struct Trie{
int cnt;
char name[40];
bool ok;
Trie *next[127];
Trie(){
ok=0;cnt=0;//某个字符串出现的总个数//是不是走到了字符串的最后一个字母,保存最后一个字母的那个节点也保
for(int i=0;i<127;i++)//子节点,ASCII最大值为126
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<<" "<<fixed<<setprecision(4)<<p->cnt*100.0/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;
}
本文介绍了一个用于统计特定区域内各种树木种类及其所占比例的算法。该算法使用卫星影像技术获取树木数据,并通过一种高效的数据结构——Trie树来实现树木种类的统计与排序输出。
917

被折叠的 条评论
为什么被折叠?



