[字典树] 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 ...
随机推荐
- 诡异之--map clear 之后可能导致size != 0的操作
map<char, int>mp; charMp[; charMp['b'] ++; cout<<charMp['a']<<endl; cout<<ch ...
- IIS Express 错误提示汇总
在做WEB Service开发中,要经常用到IIS Express服务器,有些自己遇到的问题做一个汇总以待后续查找. 错误类型: 问题来源:直接在项目上选择调试运行. 解决方案: 1.直接找到那个文件 ...
- Redis 的简单运算
Redis 的简单运算 命令 说明 备注 incr key 在原字段上加 1 只能对整数操作 incrby key increment 在原字段上加上整数 (increment) 只能对整数操作 de ...
- Android视频截图
本文介绍如何获取视频中某个时间点的数据 调用以下方法即可,特别注意,在获取图片时的参数单位为微秒,不是毫秒 如果错用了毫秒会一直获取第一帧的画面 /** * 获取某个时间点的帧图片 * * @para ...
- JS——事件的绑定与解绑
1.绑定形式 ele.addEventListener(evtName, fn) ele["on" + evtName] = function () {} ele.onclick ...
- 如何快速获取yun2win app key?
注册yun2win开发者账号 1.在注册页面输入您的邮箱,点击下方发送,yun2win将会发送一封验证邮件到您的邮箱: 2.如果没有收到邮件请查看垃圾箱或者点击重新发送: 3.打开邮箱查看验证邮件,点 ...
- VC使用CryptoAPI计算MD5
// md5.h #include <tchar.h> #include <wincrypt.h> // 计算Hash,成功返回0,失败返回GetLastError() // ...
- Centos 修改源
1首先备份原来的配置文件: mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2下载对应版本r ...
- 解决Mysql Workbench的Error Code: 1175错误
错误: Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE ...
- js的基础运用
总结: 1.定义:分为隐式定义和显式定义可以先定义后赋值. 2.+:当两边都是数值则运行加法运算,若一遍是字符型则进行拼接. 3.数值变字符:数值变量.toString()方法. 字符变数值:通过加一 ...