POJ - 2418 Hardwood Species(map,trie,BST)
1、输入若干行树名,输入结束后,按字典序输出树名及其所占百分比。
2、多种方法:map,trie,BST
3、
map:
#include<iostream>
#include<stdio.h>
#include<string>
#include<map>
using namespace std; int main(){
map<string,int>h;
string s;
int n;
n=;
while(getline(cin,s)){
++n;
++h[s];
}
map<string,int>::iterator it;
for(it=h.begin();it!=h.end();++it){
string name=(*it).first;
int k=(*it).second;
printf("%s %.4f\n",name.c_str(),double(k)*/double(n));
}
}
trie:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std; const int MAX=;
char tree[][];//存储不同的树名
int species=;//树的种类数目
int totalNum=;//树的总数目 struct Trie
{
Trie *next[MAX];
int num;//出现次数
};
Trie *root=new Trie; void createTrie(char *str)
{
++totalNum;
int len = strlen(str);
Trie *p = root, *q;
for(int i=; i<len; ++i)
{
int id = str[i];
if(p->next[id] == NULL)
{
q = (Trie *)malloc(sizeof(Trie));
//q = new Trie;
for(int j=; j<MAX; ++j){
q->next[j] = NULL;
q->num=;
}
p->next[id] = q;
}
p = p->next[id];
}
if(p->num==)strcpy(tree[species++],str);
++p->num;
}
int findTrie(char *str)
{
int len = strlen(str);
Trie *p = root;
for(int i=; i<len; ++i)
{
int id = str[i];
p = p->next[id];
if(p == NULL) //若为空集,表示不存以此为前缀的串
return ;
}
return p->num;
}
int cmp(const void *a,const void *b){
return strcmp((char *)a,(char *)b);//升序
}
int main()
{
char str[];
int i;
for(i=; i<MAX; i++){
root->next[i]=NULL;
root->num=;
}
while(gets(str)){
createTrie(str);
}
qsort(tree,species,sizeof(tree[]),cmp);
for(i=;i<species;++i)
printf("%s %.4f\n",tree[i],double(findTrie(tree[i]))/double(totalNum)*);
return ;
}
BST:
POJ - 2418 Hardwood Species(map,trie,BST)的更多相关文章
- [字典树] poj 2418 Hardwood Species
题目链接: id=2418">http://poj.org/problem?id=2418 Hardwood Species Time Limit: 10000MS Memory ...
- [ACM] POJ 2418 Hardwood Species (Trie树或map)
Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 17986 Accepted: 713 ...
- POJ 2418 Hardwood Species(STL在map应用)
职务地址:id=2418">POJ 2418 通过这个题查了大量资料..知道了非常多曾经不知道的东西. . .. 在代码中凝视说明吧. 代码例如以下: #include <ios ...
- POJ 2418 Hardwood Species (哈希,%f 和 %lf)
我的错因: 本来改用%f输出,我用了%lf,结果编译器直接判定为错误(一部分编译器认为lf是没有错的).当时我还以为是hash出错了.. 方法不止一种: 方法 时间 空间 Hash 891ms 5 ...
- Hardwood Species(map)
http://poj.org/problem?id=2418 题意:给定一系列字符串,要求按字典序升序输出每个串,并输出每个串出现的百分比. 用map做的,交c++A了,G++ WA..so sad. ...
- POJ 2418 Hardwood Species
Hardwood Species Time Limit: 10000MS Memory Limit ...
- poj 2418 Hardwood Species (map)
题目:http://poj.org/problem?id=2418 在poj 上交题总是有各种错误,再次感叹各个编译器. c++ AC代码,G++为超时,上代码: #include<cstdio ...
- POJ 2418 Hardwood Species 【Trie树】
<题目链接> 题目大意: 给你一堆字符串,让你按字典序输出他们出现的频率. 解题分析: 首先,这是Trie数词频统计的题目,以Trie树的边储存字母,节点存储以该节点结尾的链所代表的字符串 ...
- 二叉搜索树 POJ 2418 Hardwood Species
题目传送门 题意:输入一大堆字符串,问字典序输出每个字符串占的百分比 分析:二叉搜索树插入,然后中序遍历就是字典序,这里root 被new出来后要指向NULL,RE好几次.这题暴力sort也是可以过的 ...
随机推荐
- zoj 2886 Look and Say
Look and Say Time Limit: 2 Seconds Memory Limit: 65536 KB The look and say sequence is defined ...
- Java日志实战及解析 - 引导
日志是什么? 程序日志的作用 记录程序运行情况,程序员观察和调试使用 统计分析 日后审计 程序员,开发工程师,架构师等天天打交道. 实际中有4%的代码是日志! http://logging.apach ...
- SeLion数据驱动中遇到的问题,以及解决方案
问题描述: 使用selion框架数据驱动时,总是test ignored. 解决方案: 把这个dataprovider方法拿出来做单元测试.有详细报错. 问题1:使用wps保存,poi包只能解析xls ...
- 巴蜀3540 -- 【Violet 6 最终话】蒲公英
Description 原题的时间限制是 2s . 亲爱的哥哥: 你在那个城市里面过得好吗? 我在家里面最近很开心呢.昨天晚上奶奶给我讲了那个叫「绝望」的大坏蛋的故事的说!它把人们的房子和田地搞坏,还 ...
- [NOIP2001] 普及组
装箱问题 裸01背包,速刷过 #include<cstdio> #include<iostream> #include<cmath> using namespace ...
- msp430入门编程34
msp430中C语言的可移植--消除硬件差异
- 洛谷——P2916 [USACO08NOV]为母牛欢呼Cheering up the Cows
https://www.luogu.org/problem/show?pid=2916 题目描述 Farmer John has grown so lazy that he no longer wan ...
- uva 10559
记忆话搜索 DP 看了网上题解 状态方程真是巧妙 orz #include <cstdio> #include <cstdlib> #include <cmath> ...
- 2017多校Round10(hdu6171~hdu6181)
补题进度:5/11 1001(双向BFS) 题意: 给你一个类似移子游戏,给你初始状态和终止状态,问初始状态到终止状态至少要移多少步,如果步数>20就-1 分析: 很明显的BFS了,不过普通的B ...
- 全新开始fighting
a.python准备工作 Python种类: JPython IronPython JavaScriptPython RubyPython CPython ********** ...