二叉搜索树 POJ 2418 Hardwood Species
题意:输入一大堆字符串,问字典序输出每个字符串占的百分比
分析:二叉搜索树插入,然后中序遍历就是字典序,这里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的更多相关文章
- [字典树] poj 2418 Hardwood Species
题目链接: id=2418">http://poj.org/problem?id=2418 Hardwood Species Time Limit: 10000MS Memory ...
- 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 - 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> ...
- POJ 2418 Hardwood Species (哈希,%f 和 %lf)
我的错因: 本来改用%f输出,我用了%lf,结果编译器直接判定为错误(一部分编译器认为lf是没有错的).当时我还以为是hash出错了.. 方法不止一种: 方法 时间 空间 Hash 891ms 5 ...
- POJ 2418 Hardwood Species 【Trie树】
<题目链接> 题目大意: 给你一堆字符串,让你按字典序输出他们出现的频率. 解题分析: 首先,这是Trie数词频统计的题目,以Trie树的边储存字母,节点存储以该节点结尾的链所代表的字符串 ...
随机推荐
- .NET微信公众号开发-2.0创建自定义菜单
一.前言 开发之前,我们需要阅读官方的接口说明文档,不得不吐槽一下,微信的这个官方文档真的很烂,但是,为了开发我们需要的功能,我们也不得不去看这些文档. 接口文档地址:http://mp.weixin ...
- struts2文件上传和下载
1. struts系统中的拦截器介绍 过滤器:javaweb中的服务器组件,主要针对的请求和响应进行拦截. 拦截器:主要针对方法的调用,进行拦截器,当使用代理对象调用某个方法时候 对方法的调用进行拦截 ...
- 添加本地jar到Maven库
转自:http://dk05408.iteye.com/blog/2170986 上传: mvn install:install-file -Dfile=D:/workspace/p2p_server ...
- iOS - property,strong,weak,retain,assign,copy,nomatic 的区别及使用
1:ARC环境下,strong代替retain.weak代替assign,xcode 4.2(ios sdk4.3和以下版本)和之前的版本使用的是retain和assign,是不支持ARC的.xcod ...
- Jpinyin笔记
- JQ 全选设定与设置选中
复选数据框 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...
- Android -- View setScale, setTranslation 对View矩阵的处理
参考: 1.Android Matrix理论与应用详解 2.2D平面中关于矩阵(Matrix)跟图形变换的讲解 3.Android中关于矩阵(Matrix)前乘后乘的一些认识 4.Android Ma ...
- MVC – 4.mvc初体验(1)
1.MVC请求模式 2.MVC简单请求流程图 展开 折叠 3.返回string的mvc方法 展开 折叠 4.加载视图的方法
- Q3 2016 State of the Internet – Security Report
https://content.akamai.com/PG7476-Q3-2016-SOTI-Security-Report.html?utm_source=GoogleSearch&gcli ...
- 【转载】PHP使用1个crontab管理多个crontab任务
转载来自: http://www.cnblogs.com/showker/archive/2013/09/01/3294279.html http://www.binarytides.com/php- ...