题目传送门

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

分析:二叉搜索树插入,然后中序遍历就是字典序,这里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. 35. Search Insert Position

    题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...

  2. August 31st 2016 Week 36th Tuesday

    A friend without faults will never be found. 没有缺点的朋友是永远找不到的. You can't find a friends without faults ...

  3. python基础——使用__slots__

    python基础——使用__slots__ 正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性.先定义class: cla ...

  4. iOS高效调试

    写代码难免出现bug. 储备些调试技能绝对能够提高你的工作效率,让bug无所遁形.下面就和大家分享一些我在工作中常用的iOS调试小技能. 1. 打印 最简单,基础的调试方法就是打印日志了.贴出两段封装 ...

  5. 关于s:iterator 和s:if 的结合使用

    <s:iterator value="list" status="st"> <div class="sidebar-nav" ...

  6. tableView 局部刷新

    //一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2]; [tableview reloadSections:in ...

  7. sprintf_s的使用

    int sprintf_s(char *restrict buffer, rsize_t bufsz,              const char *restrict format, ...); ...

  8. Delphi的枚举类型

    参考:http://blog.csdn.net/kissdeath/article/details/2060573 Delphi程序不仅可以用于数值处理,还更广泛的用于处理非数值的数据.例如:性别.月 ...

  9. 锁ReaderWriterLockSlim介绍

    概述 ReaderWriterLockSlim 表示用于管理资源访问的锁定状态,可实现多线程读取或进行独占式写入访问: 常用的方法: cacheLock.EnterReadLock();//加上读取锁 ...

  10. MVC4 WEBAPI(一)使用概述

    所谓概述,也就是总结一些WEB API常用的使用用法.MVC APIWEB是一个轻量级的服务接口,完全符合RestFul框架设计,每个URL代表一种资源,使用方便,没有WCF那么庞大,但是麻雀虽小五脏 ...