Hardwood Species

Time Limit: 10000MS

Memory Limit: 65536K

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.


解题心得:

  1. 题意就是给你很多的字符串,要求你按照字典序输出字符串并且输出字符串在所有字符串中出现次数的百分比。
  2. 这个题可以使用map映射,也可以使用二叉排序树,这里写的是二叉排序树,没什么难度,主要就是怎么处理字符串的问题。
  3. 关于字符串的处理很有意思,先在输入的时候进行固定的形式的输入,不懂的可以去看看sscanf的用法,里面介绍了怎么固定输入的问题。然后在每个二叉树的节点中记录出现的次数和名字,名字可以定义为string类型,因为string不能使用strcmp和printf,所以可以将string直接转换成c语言的字符数组类型,转换很简单,string.c_str()就可以了,新知识啊,才学到。

#include<stdio.h>
#include<cstring>
#include<string>
using namespace std;
const int maxn = 1e4+100;
char ch[35];
struct node
{
string name;
float num;
node *lchild,*rchild;
}tree[maxn];
int tot; node* newnode()
{
tree[tot].name = ch;
tree[tot].num = 1;
tree[tot].lchild = NULL;
tree[tot].rchild = NULL;
return &tree[tot++];
} void init(node*& root)
{
root = NULL;
} void insertBST(node*& root)
{
if(root == NULL)
{
root = newnode();
return ;
}
int cmp = strcmp(root->name.c_str(),ch);//按照字典序建树
if(cmp == 0)
{
root->num++;
return ;
}
else if(cmp > 0)
insertBST(root->lchild);
else
insertBST(root->rchild);
} void buildtree(node*& root)
{
insertBST(root);
} void prin(node*& root,int sum)
{
if(root == NULL)
return ;
prin(root->lchild,sum);
printf("%s %.4f\n",root->name.c_str(),(float)(((root->num*100)/sum)));
prin(root->rchild,sum);
} int main()
{
tot = 0;
int sum = 0;
node *p;
init(p);//这里一定要初始化
while(scanf("%30[^\n]",ch) != EOF)//字符串固定输入三十位,遇到回车终止
{
getchar();
sum ++;//记录总共输入了多少个字符串
buildtree(p);
}
prin(p,sum);
}
05-26 13:07