二叉排序树:POJ2418-Hardwood Species(外加字符串处理)
Hardwood Species
Time Limit: 10000MS
Memory Limit: 65536K
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.
解题心得:
- 题意就是给你很多的字符串,要求你按照字典序输出字符串并且输出字符串在所有字符串中出现次数的百分比。
- 这个题可以使用map映射,也可以使用二叉排序树,这里写的是二叉排序树,没什么难度,主要就是怎么处理字符串的问题。
- 关于字符串的处理很有意思,先在输入的时候进行固定的形式的输入,不懂的可以去看看sscanf的用法,里面介绍了怎么固定输入的问题。然后在每个二叉树的节点中记录出现的次数和名字,名字可以定义为string类型,因为string不能使用strcmp和printf,所以可以将string直接转换成c语言的字符数组类型,转换很简单,string.c_str()就可以了,新知识啊,才学到。
#include<stdio.h>
#include<cstring>
#include<string>
using namespace std;
const int maxn = 1e4+100;
char ch[35];
struct node
{
string name;
float num;
node *lchild,*rchild;
}tree[maxn];
int tot;
node* newnode()
{
tree[tot].name = ch;
tree[tot].num = 1;
tree[tot].lchild = NULL;
tree[tot].rchild = NULL;
return &tree[tot++];
}
void init(node*& root)
{
root = NULL;
}
void insertBST(node*& root)
{
if(root == NULL)
{
root = newnode();
return ;
}
int cmp = strcmp(root->name.c_str(),ch);//按照字典序建树
if(cmp == 0)
{
root->num++;
return ;
}
else if(cmp > 0)
insertBST(root->lchild);
else
insertBST(root->rchild);
}
void buildtree(node*& root)
{
insertBST(root);
}
void prin(node*& root,int sum)
{
if(root == NULL)
return ;
prin(root->lchild,sum);
printf("%s %.4f\n",root->name.c_str(),(float)(((root->num*100)/sum)));
prin(root->rchild,sum);
}
int main()
{
tot = 0;
int sum = 0;
node *p;
init(p);//这里一定要初始化
while(scanf("%30[^\n]",ch) != EOF)//字符串固定输入三十位,遇到回车终止
{
getchar();
sum ++;//记录总共输入了多少个字符串
buildtree(p);
}
prin(p,sum);
}
二叉排序树:POJ2418-Hardwood Species(外加字符串处理)的更多相关文章
- POJ2418——Hardwood Species(map映射)
Hardwood Species DescriptionHardwoods are the botanical group of trees that have broad leaves, produ ...
- POJ2418 Hardwood Species—二叉查找树应用
1. Hardwood Species原题描述 Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 14326 Acce ...
- POJ-2418 Hardwood Species(二叉搜索树)
思路就是先将每个单词存进二叉树中,没出现一次,修改该单词所在结点的cnt++: 最后通过递归中序遍历输出结果. 思路很清晰,主要注意一下指针的使用,想一想为什么要这么用? 简单的解释就是,insert ...
- Hardwood Species 分类: POJ 树 2015-08-05 16:24 2人阅读 评论(0) 收藏
Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 20619 Accepted: 8083 De ...
- Hardwood Species(水)
Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u SubmitStatus Descrip ...
- 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 ...
- [ACM] POJ 2418 Hardwood Species (Trie树或map)
Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 17986 Accepted: 713 ...
- 洛谷 UVA10226 Hardwood Species
洛谷 UVA10226 Hardwood Species 洛谷评测传送门 题目描述 PDF 输入格式 输出格式 输入输出样例 输入 #1复制 输出 #1复制 题目翻译: 给定若干字符串,输出格式为:( ...
- POJ 2418 Hardwood Species
Hardwood Species Time Limit: 10000MS Memory Limit ...
随机推荐
- HTML5利用FormData对象实现显示进度条的文件上传
摘自:https://blog.csdn.net/q1056843325/article/details/53759963 自己做是按这个实现的,兼容性还不错 完整简约的解决方案 下面的代码清单是包括 ...
- QrenCode : linux命令行下生成二维码图片
原文链接:http://wowubuntu.com/qrencode.html # 作者:riku/ / 本文采用CC BY-NC-SA 2.5协议授权,转载请注明本文链接. 对于二维码大家应该并不陌 ...
- IO(File、递归)
第1章 File 1.1 IO概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下次再想使用这些数据,可是已经没有了.那怎么办呢?能不能把运算完的数据都保存下来,下 ...
- Mysql中WHERE IN,UNION 用法详解
WHERE IN 用法 这里分两种情况来介绍 1.in 后面是记录集,如: select * from table where uname in(select uname from ...
- Java VS Python 应该先学哪个?
http://blog.segmentfault.com/hlcfan/1190000000361407 http://www.tuicool.com/articles/fqAzqi Java 和 P ...
- vmware 虚机NAT模式,局域网可访问
本地VMware虚拟机,网络模式为NAT,现在需要局域网其他电脑通过ssh连接这台VMware虚拟机 宿主机地址:192.168.3.26 VMware虚拟机地址:192.168.239.137 局域 ...
- MVC web api转换JSON 的方法
- FreeRTOS_信号量
FreeRTOS信号量 信号量是操作系统总重要的一部分,信号量一般用来进行资源管理和任务同步,FreeRTOS中信号量又分为二值信号量.计数型信号量.互斥信号量和递归互斥信号量.不同的信号量其应用场景 ...
- IntelliJ IDEA Debug模式的启动
在服务器启动参数中加入: -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 在程序中设置断点. 运行程序,将停留在断点处. = ...
- javaweb基础(16)_jsp指令
一.JSP指令简介 JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指令: pa ...