二叉排序树: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 ...
随机推荐
- Spark Mllib里如何将如温度、湿度和风速等数值特征字段用除以***进行标准化(图文详解)
不多说,直接上干货! 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第18章 决策树回归分类Bike Sharing数据集
- 【转】如何学习Javascript
首先要说明的是,咱现在不是高手,最多还是一个半桶水,算是入了JS的门. 谈不上经验,都是一些教训. 这个时候有人要说,“靠,你丫半桶水,凭啥教我们”.您先别急着骂,先听我说. 你叫一个大学生去教小学数 ...
- eclipse查看jar包源文件
话不多说上链接 https://www.cnblogs.com/1995hxt/p/5252098.html这里介绍了完整的流程,亲自试过,可以的! 以防以后要用的时候找不到文件的下载地址,所以就先在 ...
- web常见几种处理图标方法
方法一: 用background制作小图标 像这样,拿到设计稿后把所有的图标放在一张图片上,利用background-position.width.height来控制图标的位置及大小. 代码: .ic ...
- 织梦ckeditor编辑器 通过修改js去除img标签内的width和height样式
1. 文件\include\ckeditor\plugins\image\dialogs\image.js 2. 使用工具美化js代码 3. 搜索 setStyle('width', CKEDITOR ...
- 设置DIV随滚动条滚动而滚动
有段时间没有碰Web端了,最近做了个功能,需要做个DIV随滚动条滚动而滚动,mark一下: 源码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 ...
- allure使用简介
#安装依赖包pip install requests_toolbeltpip install pyyamlpip install pytest-allure-adaptor #安装allure2 说明 ...
- 将C4C Service Request中的summary和其他附件同步到ERP的Billing Request去
C4C里将Service Request称为Work Ticket. 比如现在我的Service Request有两个行项目,只有第一个需要同步到ERP去.但是第二个行项目对于客户检查Invoice来 ...
- IM云服务领域,融云因何得以登上浪潮之巅?
每一次技术驱动下的商业变革都是以浪潮的形式到来,每一次的涨潮都会带着赶上浪潮的企业登上新的巅峰.随着移动互联网的融合发展,短短几年期间,IM通讯云服务从早期的虚无飘渺到如今已经成为现代企业的标配,引发 ...
- OpenGL小试牛刀第一季
效果截图:代码展示:using System;using System.Collections.Generic;using System.ComponentModel;using System.Dat ...