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.
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
题意:在一个森林里,有很多物种,现已知所有的树木,问每种树占所有树的比例是多少。
解析:问每种树木所占的比例是多少,首先要知道每种树的数目以及所有树的总数目。所有树的总数目很容易知道,输入了多少次就有多少树,但是要把不同的树木进行分类,然后统计没种树的数目。这里用了两种方法,一种是字典树,一种是MAP,这里先讲字典树。
将每种树木都写进字典树中,树的种类用结构体数组单独存储,每种树的数目通过在字典树中添加的时候确定,如果已经有这种树了,那么返回这种树的存储地址,使地址加一;如果没有这种树,返回新增物种,数目初始化为1。具体看代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define MAX 1<<16
using namespace std;
struct Trie
{
int next[127];
int sign;
} trie[900000];
int top = 0,n = 0;
struct Tree
{
char stree[30];
int sum;
} tree[10009];
int cmp(Tree a,Tree b)
{
if(strcmp(a.stree,b.stree) <= 0)
return 1;
else
return 0;
}
int newTrie()
{
memset(trie[top].next,-1,sizeof(trie[top].next));
trie[top].sign = -1;
return top++;
}
int init(int k,char *sz)
{
int l,j,i;
l = strlen(sz);
for(i = 0; i < l; i++)
{
j = sz[i];
if(trie[k].next[j] == -1)
trie[k].next[j] = newTrie();
k = trie[k].next[j];
}
if(trie[k].sign == -1)
{
trie[k].sign = n;
return -1;
}
return trie[k].sign;
}
int main()
{
char ss[31];
int k,j,i,num = 0;
top = 0;
k = newTrie();
n = 0;
while(gets(ss))
{
num++;
int flag = 0;
flag = init(k,ss);
if(flag == -1)
{
strcpy(tree[n].stree,ss);
tree[n].sum = 1;
n++;
}
else
{
tree[flag].sum++;
}
}
sort(tree,tree+n,cmp);
for(i = 0; i < n; i++)
{
double jie = (double)tree[i].sum/(double)num;
printf("%s %.4lf\n",tree[i].stree,jie*100);
}
return 0;
}
MAP实现
map看起来是很简单的,而且也很好理解,简单说来就是将逗号后面的映射到前面去,前面的就相当于数组的下标,而后面的就相当于数组内存储的内容。默认字典序排列
这里用到了迭代器,是一个指针变量,用来遍历map容器并输出。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include<algorithm>
using namespace std;
int main()
{
map<string,int>q;
map<string,int>::iterator i;
int num = 0;
char sz[40];
while(gets(sz))
{
if(sz[0] == '\0')
break;
q[sz]++;
num++;
}
for(i = q.begin();i != q.end();i++)
{
double x = (i->second * 100.0) / num;
printf("%s %.4lf\n",i->first.c_str(),x);
}
return 0;
}