二叉排序树: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 ...
随机推荐
- 067 Add Binary 二进制求和
给定两个二进制字符串,返回他们的和(用二进制表示).案例:a = "11"b = "1"返回 "100" .详见:https://leetc ...
- Solr创建索引问题
问题描述: 8月 19, 上午10点27:58.219 WARN com.ngdata.hbaseindexer.supervisor.IndexerSupervisor No indexer pro ...
- 用canvas裁剪图片
var selectObj = null; function ImageCrop(canvasId, imageSource, x, y, width, height) { var canvas = ...
- 使用 xib 设置 button 等款等高
很多时候需要使用平分的控件来布局,当然xib中可以之间使用 UIToolBar 使用 UIBarButtonItem 添加弹簧即可完成平均分布 但是,直接使用 button 也可以实现平均布局
- Bootstrap设置按钮禁用
在Bootstrap中,按钮可以使用button标签或者a标签.设置按钮禁用可以通过两种方式,一种是通用CSS样式,一种是用过JS脚本动态设置,下面举例说明! <!DOCTYPE html> ...
- 打印机 Microsoft Print to PDF 所需的驱动程序 Microsoft Print To PDF 未知。登录之前,请与管理员联系,安装驱动程序。
这个问题发生后,我觉得很疑惑,因为服务器上确定没有安装打印机.那么打印机是从哪里来的呢? 通过百度搜索,发现网上的一个帖子解答了我的疑惑.原帖地址:http://blog.chinaunix.net/ ...
- 使用代码获得Netweaver里某个software component和C4C的版本
有同事问如何通过代码的方式获得Netweaver里某个Software component的版本信息,以及Cloud for Customer(C4C)的版本信息. Netweaver 点了Detai ...
- Unity中的各种寻找GameObject方法归纳
1.GameObject.Find():寻找Hierarchy面板中的activie 不为false的游戏对象: 路径如官方事例写法: public class ExampleClass : Mono ...
- ueditor1_3_6 一点问题记录
文件:getRemoteImage.php 第49行: if ( !in_array( $fileType , $config[ 'allowFiles' ] ) || stristr( $heads ...
- python基础一 day14 生成器函数进阶(1)