1. Hardwood Species原题描述

 
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 14326   Accepted: 5814

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

2. 理解题意并解答

题目大致意思是输入一堆字符串,可能重复,按字典序输出,并输出占的比例。

当然这道题非常简单,可能大家第一想到的是用hash方法来做,但是hash的方式会造成一定的浪费,且有冲撞的风险,有没有更好的办法呢?当然有,就是使用二叉查找树。

#include <stdio.h>
#include <stdlib.h>
#include <string.h> typedef struct node
{
char name[31];
struct node *lchild, *rchild;
int count;
}BSTree; int totalNum; void mid_visit(BSTree *root, FILE *fp);
void insertBST(BSTree **root, char *str); int main()
{
BSTree *root = NULL;
char str[31];
FILE *fp; if((fp = fopen("sample.in", "r")) == NULL)
{
printf("Cannot open file!\n");
exit(1);
} while (!feof(fp))
{
fgets(str, 30, fp); // 读入一行
if(strlen(str) > 0) // 防止空行
{
str[strlen(str) - 1] = 0; // 去掉读入的换行符
insertBST(&root, str);
totalNum++;
}
} fclose(fp); if((fp = fopen("sample.out", "w")) == NULL)
{
printf("Cannot open file!\n");
exit(1);
} mid_visit(root, fp); fclose(fp); return 0;
} void mid_visit(BSTree *root, FILE *fp)
{
if(root)
{
mid_visit(root->lchild, fp);
fprintf(fp, "%s %.4lf\n", root->name, (root->count * 100.0) / totalNum);
mid_visit(root->rchild, fp);
}
} void insertBST(BSTree **root, char *str)
{
if(!*root)
{
BSTree *p = (BSTree*)malloc(sizeof(BSTree));
strcpy(p->name, str); // 注意这里不能直接赋值p->name = str,因为p->name是数组名,不是左值
p->lchild = NULL;
p->rchild = NULL;
p->count = 1;
*root = p;
}
else
{
if(strcmp((*root)->name, str) == 0)
{
(*root)->count++;
return;
}
else if(strcmp((*root)->name, str) > 0) // 注意*root必须加(),因为->优先级大于*
{
insertBST(&(*root)->lchild, str);
}
else
{
insertBST(&(*root)->rchild, str);
}
}
}

可能有人会想,干嘛怎么麻烦?C++中不是有map吗?map就是红黑树实现的,红黑树是一种高效的二叉查找树,它通过红黑结点的约束,保证了树的高度总是在2logn以下,性能比二叉查找树更高。是的,用C++的话就方便了许多。

#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <iomanip> using namespace std; int main()
{
string str;
int totalNum = 0;
map<string, int> tree; ifstream fin("sample.in");
ofstream fout("sample.out"); while(getline(fin, str))
{
tree[str]++;
totalNum++;
} map<string, int>::iterator itr;
for(itr = tree.begin(); itr != tree.end(); itr++)
{
fout << itr->first << ' '<< fixed << setprecision(4)
<< itr->second * 100.0 / totalNum << endl;
} fin.close();
fout.close(); return 0;
}

发现代码量减少了一半,说明C++的STL库还是非常值得熟悉和掌握的,特别是在参加一些比赛时,能节省不少时间。如果是在平时练习,那么不用STL也是不错的选择,因为可以锻炼我们深入理解二叉查找树,其它也类似。

POJ2418 Hardwood Species—二叉查找树应用的更多相关文章

  1. POJ2418——Hardwood Species(map映射)

    Hardwood Species DescriptionHardwoods are the botanical group of trees that have broad leaves, produ ...

  2. POJ-2418 Hardwood Species(二叉搜索树)

    思路就是先将每个单词存进二叉树中,没出现一次,修改该单词所在结点的cnt++: 最后通过递归中序遍历输出结果. 思路很清晰,主要注意一下指针的使用,想一想为什么要这么用? 简单的解释就是,insert ...

  3. POJ 2418 Hardwood Species

                                                     Hardwood Species Time Limit: 10000MS   Memory Limit ...

  4. Hardwood Species 分类: POJ 树 2015-08-05 16:24 2人阅读 评论(0) 收藏

    Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 20619 Accepted: 8083 De ...

  5. Hardwood Species(水)

    Time Limit:10000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u SubmitStatus Descrip ...

  6. POJ 2418 ,ZOJ 1899 Hardwood Species - from lanshui_Yang

    Description Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nu ...

  7. [ACM] POJ 2418 Hardwood Species (Trie树或map)

    Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17986   Accepted: 713 ...

  8. POJ 之 Hardwood Species

                                                         Hardwood Species Time Limit:10000MS     Memory ...

  9. [字典树] poj 2418 Hardwood Species

    题目链接: id=2418">http://poj.org/problem?id=2418 Hardwood Species Time Limit: 10000MS   Memory ...

随机推荐

  1. c需要注意的细节

    1.在纯的.c文件中,例如struct Stu,之后不可以只使用Stu作为关键字来表示这个定义的结构体类型,一定要使用struct Stu一起作为类似int这种关键字来定义或者获取size. 2.函数 ...

  2. java注解学习(1)注解的作用和三个常用java内置注解

    今天,记录一下自己学习的关于注解方面的知识. Annotation是从JDK5.0开始引入的新技术 Annotation的作用: -不是程序本身,可以对程序做出解释(这一点和注释没什么区别) -可以被 ...

  3. 比较两个Excle表格的修改内容

    //输入参数为文件输入流public static Map<String, List<String>> excelColumn2maplist(InputStream is){ ...

  4. 4.update更新和delete删除用法

    一.update更新 UserMapper.java package tk.mybatis.simple.mapper; import org.apache.ibatis.annotations.Pa ...

  5. Opencv3.4:显示一张图片

    Github https://github.com/gongluck/Opencv3.4-study.git #include "opencv2/opencv.hpp" using ...

  6. oracle查询语句查询增加一列内容

    select a,sys_guid() as b from mytable sys_guid() 是生成带分隔符(-)的GUID的自定义函数 查询B表的内容插入A表,MY_ID是A表的主键不可为空,因 ...

  7. .net图表之ECharts随笔06-这才是最简单的

    今天搞柱形图的时候,发现了一个更简单的用法.那就是直接使用带all的那个js文件 基本步骤: 1.为ECharts准备一个具备大小(宽高)的Dom 2.ECharts的js文件引入(echarts-a ...

  8. 背水一战 Windows 10 (51) - 控件(集合类): ItemsControl - 项模板选择器, 数据分组

    [源码下载] 背水一战 Windows 10 (51) - 控件(集合类): ItemsControl - 项模板选择器, 数据分组 作者:webabcd 介绍背水一战 Windows 10 之 控件 ...

  9. 背水一战 Windows 10 (57) - 控件(集合类): ListViewBase - 增量加载, 分步绘制

    [源码下载] 背水一战 Windows 10 (57) - 控件(集合类): ListViewBase - 增量加载, 分步绘制 作者:webabcd 介绍背水一战 Windows 10 之 控件(集 ...

  10. DOM扩展:DOM API的进一步增强[总结篇-下]

    本文承接<DOM扩展:DOM API的进一步增强[总结篇-上]>,继续总结DOM扩展相关的功能和API. 3.6 插入标记 DOM1级中的接口已经提供了向文档中插入内容的接口,但是在给文档 ...