思路就是先将每个单词存进二叉树中,没出现一次,修改该单词所在结点的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(二叉搜索树)的更多相关文章

  1. 二叉搜索树 POJ 2418 Hardwood Species

    题目传送门 题意:输入一大堆字符串,问字典序输出每个字符串占的百分比 分析:二叉搜索树插入,然后中序遍历就是字典序,这里root 被new出来后要指向NULL,RE好几次.这题暴力sort也是可以过的 ...

  2. [字典树] poj 2418 Hardwood Species

    题目链接: id=2418">http://poj.org/problem?id=2418 Hardwood Species Time Limit: 10000MS   Memory ...

  3. POJ 1577 Falling Leaves 二叉搜索树

    HDU 3791 Falling Leaves 二叉搜索树  Figure 1Figure 1 shows a graphical representation of a binary tree of ...

  4. POJ 2418 Hardwood Species

                                                     Hardwood Species Time Limit: 10000MS   Memory Limit ...

  5. POJ 2418 Hardwood Species(STL在map应用)

    职务地址:id=2418">POJ 2418 通过这个题查了大量资料..知道了非常多曾经不知道的东西. . .. 在代码中凝视说明吧. 代码例如以下: #include <ios ...

  6. [ACM] POJ 2418 Hardwood Species (Trie树或map)

    Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17986   Accepted: 713 ...

  7. poj 2418 Hardwood Species (map)

    题目:http://poj.org/problem?id=2418 在poj 上交题总是有各种错误,再次感叹各个编译器. c++ AC代码,G++为超时,上代码: #include<cstdio ...

  8. Poj 2255 Tree Recovery(二叉搜索树)

    题目链接:http://poj.org/problem?id=2255 思路分析:根据先序遍历(如DBACEGF)可以找出根结点(D),其后为左右子树:根据中序遍历(如ABCDEFG),已知根结点(D ...

  9. POJ - 2418 Hardwood Species(map,trie,BST)

    1.输入若干行树名,输入结束后,按字典序输出树名及其所占百分比. 2.多种方法:map,trie,BST 3. map: #include<iostream> #include<st ...

  10. POJ 2418 Hardwood Species( AVL-Tree )

    #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> ...

随机推荐

  1. Code Forces 21 A(模拟)

    A. Jabber ID time limit per test 0.5 second memory limit per test 256 megabytes input standard input ...

  2. 隐藏UITableView当没有数据或数据不够的时候出现的分割线.

    在没有分割先的情况下,添加如下方法,当实例化tableview的时候调用该方法. - (void)setExtraCellLineHidden: (UITableView *)tableView{ U ...

  3. 网络模型一般是指 OSI 七层参考模型和 TCP/IP 五层参考模型。

    网络模型一般是指 OSI 七层参考模型和 TCP/IP 五层参考模型. 每一层实现各自的功能和协议,并且都为上一层提供业务功能.为了提供这 种业务功能,下一层将上一层中的数据并入到本层的数据域中,然后 ...

  4. golang: multiple http.writeHeader calls

    背景: golang的http服务,读取文件,提供给client下载时候. 出现 multiple http.writeHeader calls 错误. func DownloadFile(w htt ...

  5. Java-多线程基本

    Java-多线程基本 一 相关的概念 进程:是一个正在执行中的程序 每个进程都有一个执行的顺序,该顺序是一个执行路径,或者叫一个控制单元 线程:就是进程中的一个独立的控制单元,线程在控制着进程的执行 ...

  6. Pytorch permute,contiguous

    permute(dims),常用的维度转换方法 将tensor的维度换位      参数:dim(int)---换位顺序 >>>x = torch.randn(,,) >> ...

  7. Java集合—Map

    简介 Map用户保存具有映射关系的数据,因此Map集合里保存着两组数,一组值用户保存Map里的key,另一组值用户保存Map里的value,key和value都可以是任何引用类型的数据.Map的key ...

  8. cookie和session的自我介绍

    Cookie是什么? cookie说的直白点就是保存在用户浏览器端的一个键值对,举个例子,你现在登录了京东商城,你把浏览器关闭之后,你再打开京东,你还是可以对你的账户继续操作,已经购买的商品,订单都是 ...

  9. django+celery 实现定时任务

    利用 celery 实现定时任务 celery支持定时任务,设定好任务的执行时间,celery就会定时自动帮你执行, 这个定时任务模块叫celery beat Celery安装 由于celery 4. ...

  10. 专项训练错题整理-nowcoder-算法

    一.排序 1.快速排序在下列哪种情况下最易发挥其长处? 答案是: 被排序的数据完全无序. 在数据基本有序的情况下,会退化为冒泡排序,复杂度会退化为O(n^2). ①[因为,如果是基本有序的话, 那么每 ...