Hardwood Species
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 |
提示
题意:
给你几棵树(输入树的种类),统计它们在这几棵树中,每个树种所占全部树的百分比(无百分号)。
思路:
这题好像字典树,二叉排序树甚至普通的排序都可以做,二叉排序树不知会不会超时,但排序算法看起来时间复杂度是O(n),下面给出两种字典树的做法。
注意:这题字符范围包括了127个!不然只有RE在等着你,不去百度我也就一直卡在这了。
示例程序
需要排序的做法:
Source Code
Problem: 2418 Code Length: 1162B
Memory: 1796K Time: 750MS
Language: G++ Result: Accepted
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
struct trie
{
int id;
struct trie *n[127];
};
struct data
{
int d; //出现次数
char ch[31]; //树的名称
}a[10000];
int k;
int cmp(struct data a,struct data b)
{
if(strcmp(a.ch,b.ch)<0)
{
return 1;
}
else
{
return 0;
}
}
struct trie *create()
{
int i;
struct trie *p;
p=(struct trie *)malloc(sizeof(struct trie));
for(i=0;127>i;i++)
{
p->n[i]=NULL;
}
p->id=-1;
return p;
};
void Insert(struct trie *t,char s[])
{
int i;
for(i=0;s[i]!='\0';i++)
{
if(t->n[s[i]]==NULL)
{
t->n[s[i]]=create();
}
t=t->n[s[i]];
}
if(t->id==-1)
{
t->id=k; //记录到第几种树
k++;
a[t->id].d=0;
strcpy(a[t->id].ch,s);
}
a[t->id].d++;
}
int main()
{
int num,i;
char s[31];
struct trie *h=create();
num=0;
k=0;
while(gets(s)!=NULL)
{
Insert(h,s);
num++; //树的个数
}
sort(a,a+k,cmp);
for(i=0;k>i;i++)
{
printf("%s %.4f\n",a[i].ch,a[i].d*1.0/num*100); //最好还是用%f做输出吧
}
return 0;
}
不需要排序的做法:
Source Code
Problem: 2418 Code Length: 1034B
Memory: 1696K Time: 641MS
Language: GCC Result: Accepted
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct trie
{
int d;
struct trie *n[127];
};
int num;
struct trie *create()
{
int i;
struct trie *p;
p=(struct trie *)malloc(sizeof(struct trie));
for(i=0;127>i;i++)
{
p->n[i]=NULL;
}
p->d=0;
return p;
};
void insert(struct trie *t,char s[])
{
int i;
for(i=0;s[i]!='\0';i++)
{
if(t->n[s[i]]==NULL)
{
t->n[s[i]]=create();
}
t=t->n[s[i]];
}
t->d++;
}
void output(struct trie *t,char s[],int top)
{
int i;
for(i=0;127>i;i++)
{
if(t->n[i]!=NULL)
{
s[top]=i;
output(t->n[i],s,top+1);
}
if(t->d!=0) //这个做输出的位置放在上面或者下面都没有什么影响
{
s[top]='\0';
printf("%s %.4f\n",s,t->d*1.0/num*100);
t->d=0;
}
}
}
int main()
{
char s[31];
struct trie *h=create();
num=0;
while(gets(s)!=NULL)
{
insert(h,s);
num++;
}
output(h,s,0);
return 0;
}