注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Hao的博客

I'm on my way……

 
 
 

日志

 
 
 
 

POJ 2418二叉查找树的应用  

2009-04-29 16:29:14|  分类: Algorithm discov |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Hardwood Species
Time Limit: 10000MS  Memory Limit: 65536K
Total Submissions: 5335  Accepted: 2192

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

Waterloo Local 2002.01.26

 

题目大意:通过卫星得到了某一个区域的树名,将这些树名按字典顺序输出,并输出在树的总数中所占的比例,保留小数点后四位。

主要思想:对每种树做统计,并计算出所占的比例并不难。难的是如何在规定时间内按字典顺序输出输入中涉及的树名。字典顺序可以启发我们用排序的方法解决,我们可以把树名作为关键字来比较大小,而strcmp函数也给了我们比较大小提供了条件。接下来就是要解决时间问题。如果用插入排序的算法由于大量的数据需要大量的比较,就会超时。所以这里借助了比较经典的数据结构,二叉查找树。那么我们就可以先对输入建树,然后再通过树的中序遍历来输出结果。而比例的计算可以在树的节点中增加一个空间,用于存储关键字出现的次数。

代码如下:

#include<iostream>
#include<string>
#define NAMELEN 40
using namespace std;
typedef struct _treeNode{
    char name[NAMELEN];
    struct _treeNode *left,*right;
    int count;             //计算树名出现的次数
}treeNode;
void insert(char *treeName,treeNode *h);
void inOrderPrint(treeNode *h,int sum);    //按中序输出
int main(){
    char treeName[NAMELEN];
    treeNode *h;
    int sum;
    gets(treeName);
    h=new treeNode;
    h->left=h->right=NULL;
    h->count=1;
    strcpy(h->name,treeName);        //先将根结点赋值
    sum=1;                //记录树的总数
    while(gets(treeName)!=NULL){
        insert(treeName,h);
        sum++;
    }
    inOrderPrint(h,sum);
    return 0;
}
void insert(char *treeName,treeNode *h){
    int judge=strcmp(treeName,h->name);
    if(judge==0)(h->count)++;        //如果输入的树名已经在树中,则将对应节点的count加一
    else if(judge<0){                     //如果小就看左子树
        if(h->left)insert(treeName,h->left);     //如果有左子树,递归;如果没有,则插入一个新的节点
        else{
            h->left=new treeNode;
            h->left->left=h->left->right=NULL;
            h->left->count=1;
            strcpy(h->left->name,treeName);
        }
    }
    else{
        if(h->right)insert(treeName,h->right);
        else{
            h->right=new treeNode;
            h->right->left=h->right->right=NULL;
            h->right->count=1;
            strcpy(h->right->name,treeName);
        }
    }
}
void inOrderPrint(treeNode *h,int sum){
    double percent;
    if(h->left)inOrderPrint(h->left,sum);
    printf("%s ",h->name);
    percent=(double)(h->count)/(double)sum;
    percent*=100;
    printf("%.4lf\n",percent);
    if(h->right)inOrderPrint(h->right,sum);
}

后记:我在之前是把第一个节点用来记录树的总数的,为了让树的左右子树更加平均,我就把根结点的name赋值为"M",然后在输出的时候判断,如果name=="M"就不输出。本来以为可以达到更好的结果,却因此连WA了N次,我是想树名总不会有M吧,可是看来我很有可能错了,以后在处理问题的时候还是不要随便假设的好。

 

  评论这张
 
阅读(1391)| 评论(1)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017