[字典树] 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 ...
随机推荐
- Servlet到Servlet的请求转发与重定向的区别
Servlet跳转到另一个Servlet中: request.getRequestDispatcher().forward();代表默认的是post方法,即在被跳转的Servlet的doPost()方 ...
- 11 在C#中写文件
在这个练习中,我们来学习如何把我们想要的东西写到文件中.我们在这个练习中还是使用File类中的方法来完成写文件的操作. 在这个练习中我们要用C#创建一个纯文本文件ex11.txt 放到c盘的Exerc ...
- Android 将图片网址url转化为bitmap
public Bitmap returnBitMap(final String url){ new Thread(new Runnable() { @Override public void run( ...
- Android项目实战_手机安全卫士手机防盗界面
#安全卫士手机防盗# ###1.Activity的任务栈 1.类似一个木桶,每层只能放一个木块,我们放入木块和取出木块的时候只能从最上面开始操作 ###2.Android中的坐标系 DOM节点
node节点属性 nodeName属性 oneBox= document.getElementsById('box');var s = oneBox.nodeName; //nodeName与tag ...
- HDU_Reward_拓扑排序
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- jenkins环境搭建(Windows)
1.下载并解压Tomcat Tomcat官方网站:http://tomcat.apache.org/ 下载并解压,解压后的目录结构如下: 2.下载并安装适合自己电脑系统的 jenkins Jenkin ...