思路就是先将每个单词存进二叉树中,没出现一次,修改该单词所在结点的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. CentOS开启telnet连接

    开启telnet连接通道 yum安装telnet yum -y install telnet-server* 关闭防火墙 /etc/init.d/iptables stop 编辑配置文件 vim /e ...

  2. CEF3 HTML5 audio标签为什么不能播放mp3格式的音频文件

    CEF3 HTML5 audio标签 为什么不能播放mp3格式的音频文件   原因略.   解决方法: 找一个最新版的chrome ,我用的是24版本.路径 C:\Documents and Sett ...

  3. 解决;R语言使用sqldf库是报错"Failed to connect to database: Error: Access denied for user '..'@'localhost' (using password: NO) Error in !dbPreExists : invalid argument type"

    原因:在使用sqldf时,不需要加载RMySQL库 解决方案:在控制台执行释放RMySQL库加载 detach("package:RMySQL", unload=T);

  4. Myeclipse中js总是报错

    1.右键选择 MyEclipse-->Exclude From Validation .2.再右键选择 MyEclipse-->Run Validation 即可.

  5. Sublime Text 3如何编译运行c++程序

    扯 去了一趟清北学堂感觉自己玩的特别嗨,算法没学到什么,前端和爬虫的知识到是会了不少. 然后知道了有一个叫做sublime text 3的编辑器,好用不好用不知道,就冲着它好看,就决定以后就用它了. ...

  6. VS调用python方法

    1.  安装python3.7 2.  Vs2010中配置python: 3.添加头文件:#include <Python.h> 4.问题:error LNK2001: 无法解析的外部符号 ...

  7. H5应用程序缓存 - Cache manifest

    一.作用 离线浏览 - 根据文件规则把资源缓存在本地,脱机依然能够访问资源,联网会直接使用缓存在本地的文件.优化加载速度,节约服务器资源. 二.适用场景 正如 manifest 英译的名字:离线应用程 ...

  8. 监控之snmpd 服务

    监控离不开数据采集,经常使用的Mrtg ,Cacti,Zabbix,等等监控软件都是通过snmp 协议进行数据采集的! 1 什么是snmp 协议? 简单网络管理协议(SNMP,Simple Netwo ...

  9. django高级应用(分页功能)

    django高级应用(分页功能) 1.原生分页应用 前端html代码 <!DOCTYPE html> <html lang="en"> <head&g ...

  10. Django接收自定义http header(转)

    add by zhj: Django将所有http header(包括你自定义的http header)都放在了HttpRequest.META这个Python标准字典中,当然HttpRequest. ...