[字典树] 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 ...
随机推荐
- CSS怎样改变行内样式(通过外部级联样式表) css !important用法CSS样式使用优先级判断
CSS样式优先级 行内>内部>外部 使用!important的css定义是拥有最高的优先级的.只是在ie6下出了一点小的bug,注意书写方式一般可以轻松避开的. CSS中的!importa ...
- Mysql中timestamp用法详解
前言:时间戳(timestamp),一个能表示一份数据在某个特定时间之前已经存在的. 完整的. 可验证的数据,通常是一个字符序列,唯一地标识某一刻的时间.使用数字签名技术产生的数据, 签名的对象包括了 ...
- cms判断写法
cms比较容易写出循环的网页内容,对于有些循环的网页内容有不同css设定,这样在写cms时需要对循环做出条件判断:{if 判断条件}输出内容{else}输出内容{/if}.通过判断可以实现图片轮播效果 ...
- mac中显示隐藏文件和.开头的文件
在控制台中执行一下命令,即可在finder中看到此类文件: defaults write com.apple.Finder AppleShowAllFiles YES killall Finder
- 控制台——args参数的赋值方法
args参数的赋值方法有好几种,主要介绍两种. 外部传参的方法:先找到bin目录下的exe文件,并创建快捷方法,在目标后面追加参数. 控制台主函数入口实现方法 static void Main(str ...
- Windows10下Docker的安装
安装前先说明一下,这里windows10要是专业版(Professional)或者是企业版(Enterprise),而且要是64位,否则会出现错误.不是这两个版本的win10没有自带的虚拟机(教育版未 ...
- Centos6.7 安装Naigos教程
Centos6.7 安装Naigos教程参考文档:https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/quickst ...
- codeforces_738C_二分
C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- python实战教程之自动扫雷
1.找到游戏窗口与坐标 #扫雷游戏窗口class_name = "TMain"title_name = "Minesweeper Arbiter "hwnd = ...
- join 和 left join 和 right join的区别?
join等价于 inner join 是内连接 ,返回两个表都有的符合条件的行. left join 是左连接,返回坐表中所有的行以及右表中符合条件的行. right join右连接,是返回右表中所有 ...