POJ-2418 Hardwood Species(二叉搜索树)
思路就是先将每个单词存进二叉树中,没出现一次,修改该单词所在结点的cnt++;
最后通过递归中序遍历输出结果。
思路很清晰,主要注意一下指针的使用,想一想为什么要这么用?
简单的解释就是,insert函数修改的是指针的属性而不是指针指向的目标地址内容的属性。
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
int sum=;
//结构体里面似乎不能用string
// 指针的使用
struct node{
int cnt;
char word[];
node*l;
node*r;
}; void insertBST(node**root,char* word){
if(*root==NULL){
node*p=(node*)malloc(sizeof(node));
p->l=NULL;p->r=NULL;
p->cnt=;
strcpy(p->word,word);
*root=p;
}
else{
//if(word==(*root)->word){
if(strcmp(word,(*root)->word)==){
((*root)->cnt)++;
}
else if(strcmp(word,(*root)->word)<){
insertBST(&((*root)->l),word);
}
else{
insertBST(&((*root)->r),word);
}
}
} void midsearch(node*root){
if(root!=NULL){
midsearch(root->l);
printf("%s %.4lf\n",root->word,((double)root->cnt/(double)sum)*);
midsearch(root->r);
}
} int main(void){
node* root;
char w[];
while(gets(w)!=NULL){
insertBST(&root,w);//指针的指针,所以取的是root的地址而不是root
sum++;
}
midsearch(root);
return ;
}
POJ-2418 Hardwood Species(二叉搜索树)的更多相关文章
- 二叉搜索树 POJ 2418 Hardwood Species
题目传送门 题意:输入一大堆字符串,问字典序输出每个字符串占的百分比 分析:二叉搜索树插入,然后中序遍历就是字典序,这里root 被new出来后要指向NULL,RE好几次.这题暴力sort也是可以过的 ...
- [字典树] poj 2418 Hardwood Species
题目链接: id=2418">http://poj.org/problem?id=2418 Hardwood Species Time Limit: 10000MS Memory ...
- POJ 1577 Falling Leaves 二叉搜索树
HDU 3791 Falling Leaves 二叉搜索树 Figure 1Figure 1 shows a graphical representation of a binary tree of ...
- POJ 2418 Hardwood Species
Hardwood Species Time Limit: 10000MS Memory Limit ...
- POJ 2418 Hardwood Species(STL在map应用)
职务地址:id=2418">POJ 2418 通过这个题查了大量资料..知道了非常多曾经不知道的东西. . .. 在代码中凝视说明吧. 代码例如以下: #include <ios ...
- [ACM] POJ 2418 Hardwood Species (Trie树或map)
Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 17986 Accepted: 713 ...
- poj 2418 Hardwood Species (map)
题目:http://poj.org/problem?id=2418 在poj 上交题总是有各种错误,再次感叹各个编译器. c++ AC代码,G++为超时,上代码: #include<cstdio ...
- Poj 2255 Tree Recovery(二叉搜索树)
题目链接:http://poj.org/problem?id=2255 思路分析:根据先序遍历(如DBACEGF)可以找出根结点(D),其后为左右子树:根据中序遍历(如ABCDEFG),已知根结点(D ...
- POJ - 2418 Hardwood Species(map,trie,BST)
1.输入若干行树名,输入结束后,按字典序输出树名及其所占百分比. 2.多种方法:map,trie,BST 3. map: #include<iostream> #include<st ...
- POJ 2418 Hardwood Species( AVL-Tree )
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> ...
随机推荐
- Python全栈day14(字符串格式化)
一,%字符串格式化 1,使用%s 后面一一对应输入对应的字符串,%s可以接受任何参数 print ("I am %s hobby is zhangsan"%'lishi') pri ...
- R中基本命名(未完)
ls() #查看 rm(list=ls()) #清除内存变量 library() #载入库包 help() #查看帮助文档 head(iris) #查看数据集 class(iris) #查看数据集的类 ...
- 解决:R读取含中文excel文件,read.xlsx乱码问题
1.新建testexcel.xlsx文件 2.创建R文件:test.R # 定义文件变量 excel_path <- "chapter2/testexcel.xlsx" # ...
- 伪造堆块绕过unlink检查(ctf-QiangWangCup-2015-shellman)
目录 堆溢出点 伪造空闲堆块 释放时重写指向伪造堆块的指针 如何利用 参考资料 堆溢出点 图1 堆溢出点 在edit函数中,没有对输入的长度和原来的长度做判断. 伪造空闲堆块 正常 ...
- 小程序 wx.navigateTo传值总结
wx.navigateTo(Object object) 保留当前页面,跳转到应用内的某个页面.但是不能跳到 tabbar 页面.使用 wx.navigateBack 可以返回到原页面.小程序中页面栈 ...
- Myeclipse下配置struts2和hibernate
最近维护一个项目,是用struts2做的,所以特意学了下struts的入门,否则代码都看不懂啊.下面记录下过程.Myeclipse 版本为2014.struts2,hibernate为4.1. 1.既 ...
- The Highest Mark---hdu5501(问题转化 01背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5501 二维数组: #include<stdio.h> #include<iostre ...
- 为什么说”人生苦短,我用python“?
本文不扯什么大道理,只是先介绍Python的背景,然后从实用的角度出发举一两个真实栗子. 首先要想了解要一门语言的好坏,或者为什么招程序员喜欢(卧槽,原来程序员喜欢不是女朋友?)我们的先从语言的产 ...
- 前端 Dom 直接选择器
文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM把 ...
- java 多线程 day09 线程池
import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.c ...