[字典树] poj 2418 Hardwood Species
题目链接:
id=2418">http://poj.org/problem?id=2418
|
Hardwood Species
Description
Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter.
America's temperate climates produce forests with hundreds of hardwood species -- trees that share certain biological characteristics. Although oak, maple and cherry all are types of hardwood trees, for example, they are different species. Together, all the hardwood species represent 40 percent of the trees in the United States. On the other hand, softwoods, or conifers, from the Latin word meaning "cone-bearing," have needles. Widely available US softwoods include cedar, fir, hemlock, pine, redwood, spruce and cypress. In a home, the softwoods are used primarily as structural lumber such as 2x4s and 2x6s, with some limited decorative applications. Using satellite imaging technology, the Department of Natural Resources has compiled an inventory of every tree standing on a particular day. You are to compute the total fraction of the tree population represented by each species. Input
Input to your program consists of a list of the species of every tree observed by the satellite; one tree per line. No species name exceeds 30 characters. There are no more than 10,000 species and no more than 1,000,000 trees.
Output
Print the name of each species represented in the population, in alphabetical order, followed by the percentage of the population it represents, to 4 decimal places.
Sample Input Red Alder Sample Output Ash 13.7931 Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceeded.
Source |
[Submit] [Go Back] [Status]
[Discuss]
题目意思:
给n个串,求每一个串出现的频率。
n<=1000000
解题思路:
把每一个串压到字典树里,维护一个从根节点到当前节点的单词数量。
注意:有空格,大写和小写字母。
空格的ASCII是32。Z的ASCII是90,z的ASCII是122.所以能够都减去32能够转化到0~100进行处理。
代码:
//#include<CSpreadSheet.h> #include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define Maxn 110000 struct Node
{
int cnt;
struct Node * next[110]; //空格是32 把空格记为0
}root,node[Maxn*35]; char save[Maxn];
int pp,n; void inse(char * cur)
{
Node * p=&root; while(*cur)
{
if(p->next[*cur-' ']==NULL)
{
node[pp].cnt=0;
memset(node[pp].next,NULL,sizeof(node[pp].next));
p->next[*cur-' ']=&node[pp++];
}
p=p->next[*cur-' '];
cur++;
}
p->cnt++;
//printf("%d\n",p->cnt);
} void cal(Node * cur,string a)
{
//cout<<a<<endl;
//system("pause");
Node * p=cur; //while(p)
{
if(p->cnt>=1)
{
cout<<a<<' ';
printf("%.4lf\n",(p->cnt)*100.0/n);
}
for(int i=0;i<100;i++)
{
if(p->next[i])
{
//p=p->next[i];
char temp=i+' ';
cal(p->next[i],a+temp);
}
}
//p=p->next;
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
n=0,pp=0; while(gets(save)!=NULL)
{
save[strlen(save)]='\0';
inse(save);
n++;
}
cal(&root,""); return 0;
}
[字典树] poj 2418 Hardwood Species的更多相关文章
- [ACM] POJ 2418 Hardwood Species (Trie树或map)
Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 17986 Accepted: 713 ...
- POJ 2418 Hardwood Species
Hardwood Species Time Limit: 10000MS Memory Limit ...
- POJ 2418 Hardwood Species(STL在map应用)
职务地址:id=2418">POJ 2418 通过这个题查了大量资料..知道了非常多曾经不知道的东西. . .. 在代码中凝视说明吧. 代码例如以下: #include <ios ...
- POJ 2418 Hardwood Species 【Trie树】
<题目链接> 题目大意: 给你一堆字符串,让你按字典序输出他们出现的频率. 解题分析: 首先,这是Trie数词频统计的题目,以Trie树的边储存字母,节点存储以该节点结尾的链所代表的字符串 ...
- 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
题目传送门 题意:输入一大堆字符串,问字典序输出每个字符串占的百分比 分析:二叉搜索树插入,然后中序遍历就是字典序,这里root 被new出来后要指向NULL,RE好几次.这题暴力sort也是可以过的 ...
- 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 ...
随机推荐
- Laravel5.1学习笔记15 数据库1 数据库使用入门
简介 运行原生SQL查询 监听查询事件 数据库事务 使用多数据库连接 简介 Laravel makes connecting with databases and running queries e ...
- [Codeforces]Codeforces Round #489 (Div. 2)
Nastya and an Array 输出有几种不同的数字 #pragma comment(linker, "/STACK:102400000,102400000") #ifnd ...
- Microsoft SQL Server学习(一)--基本概念
数据库的分类 关系型数据库 非关系型数据库 数据库的发展 数据都是以文件的形式存储在硬盘上FATFAT32NTFS LinuxEXT 数据库设计流程 文件系统缺陷 数据库的专业术语 数据库引擎服务 E ...
- 转 IDEA 解决代码提示功能消失
转载路径是 https://blog.csdn.net/hmily_hui/article/details/78213037 原文地址:https://github.com/Damao/Intell ...
- oracle 外部表及解决ora-29400,ora-29913错误
对外部表的理解及测试,,,,如有理解不正确请大家指正 语法: create table 表名( 列名1,列名2,...... ) organization external ###说明创建外部表 ( ...
- dubbo之本地伪装
本地伪装 本地伪装 1 通常用于服务降级,比如某验权服务,当服务提供方全部挂掉后,客户端不抛出异常,而是通过 Mock 数据返回授权失败. 在 spring 配置文件中按以下方式配置: <dub ...
- excel求1加到100的和
强大的数组公式
- day12-闭包函数、装饰器
目录 闭包函数 装饰器 无参装饰器 有参装饰器 装饰器模板 闭包函数 之前我们都是通过参数将外部的值传给函数,而闭包打破了层级关系,把局部变量拿到全局使用,并把外部的变量封装到内部函数中,然后下次直接 ...
- CSS 之自定义滚动条样式
::-webkit-scrollbar {/*滚动条整体样式*/ width: 5px; /*高宽分别对应横竖滚动条的尺寸*/ height: 1px; } ::-webkit-scrollbar-t ...
- Java中内部类详解—匿名内部类
什么是内部类? 将一个类A定义在另一个类B里面,里面的那个类A就称为内部类,B则称为外部类. 成员内部类 定义在类中方法外的类. 定义格式: class 外部类 { class 内部类{ } } ...