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

    PYTHONIOENCODING=utf8

  2. MongoDB 使用 ObjectId 代替时间

    An ObjectId is a 12-byte unique identifier consisting of: a 4-byte value representing the seconds si ...

  3. Android中的 style 和 theme

    通过设置 view 控件的属性,达到设置android UI的目的,如果某些 属性值复用率很高,可以考虑将属性单独声明在 style中,这样就可以达到复用的效果. 一.style Style 概念:A ...

  4. /dev/poll, kqueue(2), event ports, POSIX select(2), Windows select(), poll(2), and epoll(4)

    /dev/poll, kqueue(2), event ports, POSIX select(2), Windows select(), poll(2), and epoll(4) libevent ...

  5. MapReduce分区和排序

    一.排序 排序: 需求:根据用户每月使用的流量按照使用的流量多少排序 接口-->WritableCompareable 排序操作在hadoop中属于默认的行为.默认按照字典殊勋排序. 排序的分类 ...

  6. 【JVM】程序调优

    现实企业级Java开发中,有时候我们会碰到下面这些问题: OutOfMemoryError,内存不足 内存泄露 线程死锁 锁争用(Lock Contention) Java进程消耗CPU过高 .... ...

  7. (转)Unity3d的3种截图方法

    下面是我总结的.在u3d中的,三种截屏方法: 1.使用Application类下的CaptureScreenshot方法. void CaptureScreen() { Application.Cap ...

  8. uchome android 开发记录

    一.uchome 1.无法转移临时图片到服务器指定目录 cp_upload.php----------- function.cp.php ---------mobile_picture_tempora ...

  9. django views.py视图 获取用户请求相关信息以及请求头

    请求的其他信息 用户发来请求时候,不仅发来数据,也把请求头也发过来 在views.py 怎么找请求数据? request是一个对象,这个对象封装很多信息,可以先查这个对象的类 print(type(r ...

  10. python学习笔记(五)os、sys模块

     一.os模块 print(os.name) #输出字符串指示正在使用的平台.如果是window 则用'nt'表示,对于Linux/Unix用户,它是'posix'. print(os.getcwd( ...