Would You like to Share!

POJ2418【二叉搜索树】

by NENU_ACM_Club on Jul.21, 2008, under ACM Reports, Hits 667

POJ2418【二叉搜索树】

题目描述:
Hardwood Species

Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 3576 Accepted: 1421

Description
Hardwoodsare the botanical group of trees that have broad leaves, produce afruit or nut, and generally go dormant in the winter.
America’s temperate climates produce forests with hundreds ofhardwood species — trees that share certain biologicalcharacteristics. Although oak, maple and cherry all are types ofhardwood trees, for example, they are different species. Together, allthe hardwood species represent 40 percent of the trees in the UnitedStates.

On the other hand, softwoods, or conifers, from the Latin wordmeaning “cone-bearing,” have needles. Widely available US softwoodsinclude cedar, fir, hemlock, pine, redwood, spruce and cypress. In ahome, the softwoods are used primarily as structural lumber such as2×4s and 2×6s, with some limited decorative applications.

Using satellite imaging technology, the Department of NaturalResources has compiled an inventory of every tree standing on aparticular day. You are to compute the total fraction of the treepopulation represented by each species.
Input
Inputto your program consists of a list of the species of every treeobserved by the satellite; one tree per line. No species name exceeds30 characters. There are no more than 10,000 species and no more than1,000,000 trees.
Output
Printthe name of each species represented in the population, in alphabeticalorder, followed by the percentage of the population it represents, to 4decimal 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.
题目大意:
有一大堆树,卫星上看到他们的种类,输入数据给出的;让你统计每一个种类的出现次数,按照字典序输出种类名称和频率;
数据范围:
树种类名字小于30字符;种类个数小于1000;树的个数小于1000000;
边界数据;
无;
可能陷进
知道不能直接统计就好;
解题思路:
1000000估计硬统计肯定难过;所以就找一种方法吧,让查找尽量的快;本题用二叉搜索树;建树时通过比较字符串的值;最后再输出;
AC代码:
#include “stdio.h”
#include “stdlib.h”
#include “string.h”
int totalnum = 1;
typedef struct _bst
{
double count;
char name[31];
struct _bst *left;
struct _bst *right;
}BST;
BST *head = NULL;
void insert(char *tmp)
{
int t;
BST *p1, *p2;
BST *_node;
p1 = head;
while(p1 != NULL)
{
p2 = p1;
t = strcmp(tmp, p1->name);
if (t == 0)
{
p1->count++;
return;
}
else if (t > 0)
{
p1 = p1->right;
continue;
}
else
{
p1 = p1->left;
continue;
}
}
_node = (BST *)malloc(sizeof(BST));
_node->count = 1;
_node->left = NULL;
_node->right = NULL;
strcpy(_node->name, tmp);
if (t > 0)
{
p2->right = _node;
}
else
{
p2->left = _node;
}
}
void output(BST *phead)
{
if (phead != NULL)
{
output(phead->left);
printf(”%s %.4f\n”, phead->name, (phead->count*100)/totalnum);
output(phead->right);
}
}
int main()
{

//freopen(”e:\\in.txt”, “r”, stdin);
char temp[31];
totalnum = 1;
head = (BST *)malloc(sizeof(BST));
head->count = 1;
head->left = NULL;
head->right = NULL;
gets(head->name);
while(gets(temp))
{
if (temp[0] == ‘\0′) break;
totalnum++;
insert(temp);
}
output(head);
return 0;
}
【可优化】

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • TwitThis
  • BlogMemes Jp
  • De.lirio.us
  • blinkbits
  • Slashdot
  • Symbaloo
  • TailRank
  • Webnews.de
  • Reddit
  • Yahoo! Buzz
  • YahooMyWeb
:, ,

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!