题目传送门

题意:输入一大堆字符串,问字典序输出每个字符串占的百分比

分析:二叉搜索树插入,然后中序遍历就是字典序,这里root 被new出来后要指向NULL,RE好几次.这题暴力sort也是可以过的...

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct BST {
char name[55];
int cnt;
BST *lch, *rch;
BST *insert(BST *p, char *s) {
if (p == NULL) {
BST *t = new BST;
strcpy (t->name, s);
t->cnt = 1;
t->lch = t->rch = NULL;
return t;
}
else if (strcmp (s, p->name) == 0) {
(p->cnt)++;
}
else if (strcmp (s, p->name) < 0) p->lch = insert (p->lch, s);
else p->rch = insert (p->rch, s);
return p;
}
void mid_travel(BST *p, int n) {
if (p != NULL) {
mid_travel (p->lch, n);
if (p->cnt != 0) printf ("%s %.4f\n", p->name, p->cnt * 1.0 / n * 100);
mid_travel (p->rch, n);
}
}
}bst; int main(void) {
int n = 0;
char str[55];
BST *root = new BST;
root = NULL;
while (gets (str)) {
root = bst.insert (root, str); n++;
}
bst.mid_travel (root, n); return 0;
}

  

sort

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std; int n;
struct Name {
char str[33];
}name[1000010]; bool cmp(Name a, Name b) {
return strcmp (a.str, b.str) < 0;
} int main(void) {
n = 0;
while (gets (name[n++].str));
sort (name, name+n, cmp);
int i = 1;
while (i < n) {
int j = i;
while (j < n && strcmp (name[j].str, name[i].str) == 0) j++;
printf ("%s %.4f\n", name[i].str, (j - i) * 1.0 / (n-1) * 100);
i = j;
} return 0;
}

  

二叉搜索树 POJ 2418 Hardwood Species的更多相关文章

  1. [字典树] poj 2418 Hardwood Species

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

  2. POJ 2418 Hardwood Species

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

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

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

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

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

  5. poj 2418 Hardwood Species (map)

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

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

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

  7. POJ 2418 Hardwood Species( AVL-Tree )

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

  8. POJ 2418 Hardwood Species (哈希,%f 和 %lf)

    我的错因: 本来改用%f输出,我用了%lf,结果编译器直接判定为错误(一部分编译器认为lf是没有错的).当时我还以为是hash出错了.. 方法不止一种: 方法 时间   空间 Hash 891ms 5 ...

  9. POJ 2418 Hardwood Species 【Trie树】

    <题目链接> 题目大意: 给你一堆字符串,让你按字典序输出他们出现的频率. 解题分析: 首先,这是Trie数词频统计的题目,以Trie树的边储存字母,节点存储以该节点结尾的链所代表的字符串 ...

随机推荐

  1. Ajax如何使用Session

    在Ajax中有时会使用到Session,在aspx.cs文件这样获取: string name = Session["name"]; 但是在Ajax中就不能这样获取Session, ...

  2. 解决java.lang.SecurityException: Access to default session denied

    原先使用的代码如下: Session session = Session.getDefaultInstance(properties, null); 后来把代码修改为: Session session ...

  3. Oracle读写分离架构

    读写分离是架构分布式系统的一个重要思想.不少系统整体处理能力并不能同业务的增长保持同步,因此势必会带来瓶颈,单纯的升级硬件并不能一劳永逸.针对业务类型特点,需要从架构模式上进行一系列的调整,比如业务模 ...

  4. XMPP框架下微信项目总结(1)环境配置

    xmpp介绍 xmpp基于模块开发的 无须自己写请求 (登陆,注册,获取好友列表,添加/删除好友等) ------>简介 ------------------------- ----->工 ...

  5. iOS 8 AutoLayout与Size Class

    转自:http://www.cocoachina.com/ios/20141217/10669.html 前言 iOS8 和iPhone6发布已经过去蛮久了,广大的果粉终于迎来了大屏iPhone,再也 ...

  6. hdu 1249 三角形

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1249 part=3*s*(s-1)+2 #include<stdio.h> #includ ...

  7. rpm -qc 来查找安装包的配置文件

    rpm -qc elasticsearch /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/jvm.options /etc/elast ...

  8. Windows 8使用这半年(企业版)

    2014-06-23 硬件:thinkpad e430c core i5-3210m 4g/500g 1.Windows 8出现开机引导问题 主要开机的时候提示缺少引导文件,按ctrl+alt+del ...

  9. hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  10. 提升 LaTeX 效率的小工具:Detexify LaTeX handwritten symbol recognition

    Detexify LaTeX handwritten symbol recognition 用 LaTeX 的人找符号的表示方法通常很费事,需要去翻长长的列表.Detexify 是一个省事的小网站,只 ...