二叉搜索树 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树的边储存字母,节点存储以该节点结尾的链所代表的字符串 ...
随机推荐
- Web上的支持的图片格式以及它们之间的区别
一.GIF(图形交换格式) GIF格式的图片最多只能保存256中颜色,该格式支持透明色,支持动画效果. 二.JPEG(联合图像专家组) JPEG格式不支持透明色及动画,颜色可达1670种. 三.PNG ...
- iOS应用架构谈(二):View层的组织和调用方案(中)
iOS客户端应用架构看似简单,但实际上要考虑的事情不少.本文作者将以系列文章的形式来回答iOS应用架构中的种种问题,本文是其中的第二篇,主要讲View层的组织和调用方案.中篇主要讨论MVC.MVCS. ...
- September 2nd 2016 Week 36th Friday
How does the world look through your eyes? 你眼里的世界是什么样子的? How does the world look through your eyes? ...
- grep(Global Regular Expression Print)
.grep -iwr --color 'hellp' /home/weblogic/demo 或者 grep -iw --color 'hellp' /home/weblogic/demo/* (-i ...
- WindowManagerPolicy的后缀 解释
转自:http://blog.csdn.net/hunanwy/article/details/8563090 Ti,called from the input thread. Input threa ...
- C# 使用Conditional特性而不是#if条件编译
概述 #if/#endif 语句常用来基于同一份源码生成不同的编译结果,其中最常见的就是debug版和release版.但是这些工具在实际应用中并不是非常友好,因为它们容易被滥用,其代码页进而难以理解 ...
- 【JAVA常用类演示】
一.java.lang.System类. public final class Systemextends Object 该类包含一些有用的类字段和方法.它不能被实例化. 在 System 类提供的 ...
- <转>WCF实例化模式与高并发处理
WCF实例化模式与高并发控制 1.实例化模式InstanceModel 1.1 PerCall:单调模式 每次调用都会产生一个实例 例[ServiceBehavior(InstanceContextM ...
- Vue入门笔记#过渡
Vue过渡,可以在元素从DOM中移除,插入时自动调用过渡效果.根据设定,会适时的触发过渡效果. 在使用的目标标签里添加 transition: <div transition="my_ ...
- 两个本地(localhost)html文件之间的传值
什么是iframe? iframe 元素会创建包含另外一个文档的内联框架(即行内框架).可以理解为把iframe解释成“浏览器中的浏览器“ 在IE中: document.frames[i].docum ...