二叉排序树: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 ...
随机推荐
- JavasJavaScript笔试题
这几天参加了好多场宣讲会,记录一下遇到的编程题还有平时练习遇到的(如果有更好的方法欢迎提出,本人也是菜鸟一枚): 1.数组查重,并返回出重复次数最多的项并记录重复次数: function chacho ...
- 061 Rotate List 旋转链表
给定一个链表,将链表向右旋转 k 个位置,其中 k 是非负数.示例:给定 1->2->3->4->5->NULL 且 k = 2,返回 4->5->1-> ...
- springmvc httprequest 使用@Autowired注解
springmvc httprequest 使用@Autowired注解我一直有个疑问,就是注解后每次的httprequest 是不是都一样的了,然后会不会引发多线程问题? 代码如下: import ...
- Spring连接数据库
public class Book { private int bookid; private String bookname; private String bookauthor; private ...
- ES6学习(2)
Set和Map数据结构 ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set 本身是一个构造函数,用来生成 Set 数据结构. const s = new ...
- 手机安全卫士——在设置中心 自定义view和自定义属性
自定义组合控件 1. 自定义一个View, 继承ViewGroup,比如RelativeLayout,此文中是SettingItemView 2. 编写组合控件的布局文件,在自定义的View中加载 ...
- python3操作excel02(对excel的基础操作,进行简单的封装)3
#!/usr/bin/env python# -*- coding:UTF-8 -*- import requestsfrom bs4 import BeautifulSoupfrom bs4 imp ...
- 2017.10.1 QBXT 模拟赛
题目链接 T1 枚举右端点,前缀和优化.对于当前点x,答案为 sum[x][r]-sum[x][l-1]-(sum[z][r]-sum[z][l-1]) 整理为 sum[x][r]-sum[z][r] ...
- 5分钟部署一个Hello World Servlet到CloudFoundry
首先从我的Github下载我写好的hello world Servlet到本地. 安装Maven,然后执行命令行mvn clean install,确保build成功,在项目根目录的target文件夹 ...
- 使用ABAP代码返回S/4HANA Material上维护的Attachment明细
在事务码 MM02里为ID为16的material维护附件: 如何使用ABAP代码获得如下附件的名称和文件内容? REPORT zgos_api. DATA ls_appl_object TY ...