[字典树] 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 ...
随机推荐
- html 基础演示代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- 实现strcpy
#include <stddef.h> char* strcpy(char* dest, const char* src) { if (dest == NULL || src == NUL ...
- 【转】Java 集合系列02之 Collection架构
概要 首先,我们对Collection进行说明.下面先看看Collection的一些框架类的关系图: Collection是一个接口,它主要的两个分支是:List 和 Set. List和Set都是接 ...
- cms中某些标题链接的单独写法
href="{$CATEGORYS[45][url]}" 链接写法, {$CATEGORYS[45][catname]} 标题写法 在show页面中 src="{$thu ...
- JS——百度背景图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用 Spring Social 连接社交网络
Spring Social 框架是spring 提供社交平台的分享组件 https://www.ibm.com/developerworks/cn/java/j-lo-spring-social/
- (转) 分布式文件存储FastDFS(一)初识FastDFS
http://blog.csdn.net/xingjiarong/article/details/50559849 一.FastDFS简介 FastDFS是一款开源的.分布式文件系统(Distribu ...
- HDU_1394_Minimum Inversion Number_线段树求逆序数
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- Symbolicating Crash Reports With atos
地址:0x1000e4000 + 49116 = 0x00000001000effdc都是运行时地址: 0x1000e4000:基址偏移后的地址: 0x100000000: 共知基址:各个环境都知道, ...
- css nth-child 的应用
最近改视频监控页面,由于窗口比较多,以前是通过计算窗口大小位置来处理页面布局的,其实还是比较麻烦,而且偶尔会有页面位置错乱的现象,虽然只是及其偶尔的现象,但总归是不好. 计算窗口位置的代码: /*监控 ...