POJ 2418 Hardwood Species(trie 树 MAP qsort)

Hardwood Species
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 15439 Accepted: 6190

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

这个题目用了三种方法,就是想对比一下三种方法运行的时间差别,看看是否利用好的算法就能真正的提高运行时间

果然这样

方法一:trie树

这个题目用trie树来做是比较直接的一种想法,因为涉及到字符串的查找,这样在trie树中查找应该是比较快的了

还有一点就是要按照字典序输出,那么想一下trie树的先顺遍历结果就是顺序的

还有就是利用trie树在插入的同时就把每个字符串出现的次数记录下来的,在最后输出结果遍历的时候直接输出结果就可以了,非常方便!


时间:797ms

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
using namespace std;
struct node
{
node *next[128];
int num;
node()
{
memset(next,0,sizeof(next));
num=0;
}
}re_root;
int count;
char global_name[100];
int insert_trie(node *root,char *name)
{
if(name[0]==0)
{
root->num++;
return 0;
}
if(root->next[name[0]]!=NULL)
insert_trie(root->next[name[0]],name+1);
else
{
root->next[name[0]]=new node();
insert_trie(root->next[name[0]],name+1);
}
return 0;
}
int tri_reverse(node *root,int k)
{
if(root->num>0)
{
global_name[k]=0;
printf("%s %.4lf\n",global_name,100*double(root->num)/count);
}
int i;
for(i=0;i<128;i++)
if(root->next[i]!=NULL)
{
global_name[k]=i;
tri_reverse(root->next[i],k+1);
}
return 0;
}
int main()
{
char name[31];
count=0;
while(gets(name)!=NULL)
{
count++;
insert_trie(&re_root,name);
}
if(count==0)
return 0;
tri_reverse(&re_root,0);
return 0;
}
方法二:排序

其实这个方法不容易想到,因为被这个题目的给的时间10s和100000的数据量吓住了,其实试了之后发现可以通过

这个思想就没什么好说的了,我估计排序能过的原因还是下面代码有个看似不起眼的小优化

因为在排序过程中最大的开销之一就是元素的移动,而且是字符串的移动,那么就比较耗时

我采取的方案是全部按照指针来排序,原来的字符串的位置不动,这样就实现很快速的字符串排序!

时间:2500ms


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char name[1000001][31];
char *rec[1000001];
int cmp(const void *a,const void *b)
{
return strcmp(*((char**)a),(*(char **)b));
}
int main()
{
int k;
int i=0;
int j;
while(gets(name[i])!=NULL)
{
rec[i]=name[i];
i++;
}
qsort(rec,i,sizeof(rec[0]),cmp);
k=1;
for(j=1;j<i;j++)
{
if(strcmp(rec[j],rec[j-1])!=0)
{
printf("%s %.4lf\n",rec[j-1],100*double(k)/i);
k=1;
}
else
k++;
}
if(strcmp(rec[i-1],rec[i-2])!=0)
{
printf("%s %.4lf\n",rec[i-1],100*double(1)/i);
}
else
printf("%s %.4lf\n",rec[i-1],100*double(k)/i);
return 0;
}


方法三:map

说实话,在看到这个题目的第一眼就想到用map,同样是怕超时,在尝试排序通过之后就尝试了map

果然能通过,而且时间还比排序快点,map里面用的是红黑树,速度也不耐!关键是代码短,几分种就

完成代码AC掉!


时间:1532ms

#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <map>
using namespace std;
map<string,int> m;
int main()
{
//string s;
char s[100];
int count=0;
while(gets(s)!=NULL)
{
count++;
m[s]++;
}
map<string,int>::iterator it;
for(it=m.begin();it!=m.end();it++)
{
cout<<(*it).first<<' ';
printf("%.4lf\n",100*double((*it).second)/count);
}
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值