PKU2418_树种统计(map应用||Trie树)
Description
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
Output
Sample Input
- Red Alder
- Ash
- Aspen
- Basswood
- Ash
- Beech
- Yellow Birch
- Ash
- Cherry
- Cottonwood
- Ash
- Cypress
- Red Elm
- Gum
- Hackberry
- White Oak
- Hickory
- Pecan
- Hard Maple
- White Oak
- Soft Maple
- Red Oak
- Red Oak
- White Oak
- Poplan
- Sassafras
- Sycamore
- Black Walnut
- Willow
Sample Output
- Ash 13.7931
- Aspen 3.4483
- Basswood 3.4483
- Beech 3.4483
- Black Walnut 3.4483
- Cherry 3.4483
- Cottonwood 3.4483
- Cypress 3.4483
- Gum 3.4483
- Hackberry 3.4483
- Hard Maple 3.4483
- Hickory 3.4483
- Pecan 3.4483
- Poplan 3.4483
- Red Alder 3.4483
- Red Elm 3.4483
- Red Oak 6.8966
- Sassafras 3.4483
- Soft Maple 3.4483
- Sycamore 3.4483
- White Oak 10.3448
- Willow 3.4483
- Yellow Birch 3.4483
Hint
解法一:map应用(自动按照键值字典序排序,强)
- #include<bits/stdc++.h>
- using namespace std;
- map<string,int>mp;
- int main()
- {
- int n;
- cin>>n;
- int t=n;
- getchar();
- string s;
- while(n--){
- getline(cin,s);
- mp[s]++;
- }
- map<string,int>::iterator it;
- for(it=mp.begin();it!=mp.end();it++)
- cout<<it->first<<" "<<setiosflags(ios::fixed)<<setprecision()<<100.0*it->second/t<<'%'<<endl;
- return ;
- }
解法二:Trie树
- #include<bits/stdc++.h>
- using namespace std;
- int n,len;
- char s[];
- struct Trie
- {
- int cnt;
- bool ok;
- char name[];
- Trie *next[];
- Trie(){
- cnt=,ok=;
- memset(next,,sizeof(next));
- }
- }*root;
- void Insert(char *s,int id)
- {
- Trie *p=root;
- int len=strlen(s);
- for(int i=;i<len;i++){
- if(!p->next[s[i]-'A'])
- p->next[s[i]-'A']=new Trie();
- p=p->next[s[i]-'A'];
- }
- p->cnt++;
- if(!p->ok) p->ok=;
- strcpy(p->name,s);
- }
- void dfs(Trie *root)
- {
- Trie *p=root;
- if(p->ok){
- printf("%s",p->name);
- printf(" %.4lf%\n",100.0*p->cnt/n);
- }
- for(int i=;i<;i++){//字典序
- if(p->next[i])
- dfs(p->next[i]);
- }
- }
- int main()
- {
- cin>>n;
- getchar();
- root=new Trie;
- for(int i=;i<=n;i++){
- gets(s);
- Insert(s,i);
- }
- dfs(root);
- }
PKU2418_树种统计(map应用||Trie树)的更多相关文章
- BZOJ 2754 [SCOI2012]喵星球上的点名 (AC自动机+map维护Trie树)
题目大意:略 由于字符集大,要用map维护Trie树 并不能用AC自动机的Trie图优化,不然内存会炸 所以我用AC自动机暴跳fail水过的 显然根据喵星人建AC自动机是不行的,所以要根据问题建 然而 ...
- hdu 1251:统计难题[【trie树】||【map】
<题目链接> 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131 ...
- 统计难题(trie树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- hdu 1004 Let the Balloon Rise strcmp、map、trie树
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- HDU1251 统计难题 【trie树】
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU 1251 统计难题 (Trie树模板题)
题目链接:点击打开链接 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单 ...
- HDU - 1251 统计难题(Trie树)
有很多单词(只有小写字母组成,不会有重复的单词出现) 要统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). 每个单词长度不会超过10. Trie树的模板题.这个题内存把控不好容易MLE. ...
- hdu 1251 统计难题(trie 树的简单应用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给你多个字符串,求以某个字符串为前缀的字符串数量. 思路:简单的trie数应用,在trie ...
- Trie树及其应用
Trie树及其应用 Trie树 Trie树,又称单词查找树.字典树,是一种树形结构,是一种哈希树的变种,是一种用于快速检索的多叉树结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经 ...
随机推荐
- 认识OpenStack中的flatnetwork
目录 [隐藏] 1 Understanding FlatNetworking 1.1 FlatNetworking 1.1.1 Single Adapter, All in one setup 1.1 ...
- XmlSerializer的GenerateTempAssembly性能问题例外
XmlSerializer的两个构造函数不会出现每次构造都创建TempAssembly的性能问题,其内部做了缓存. public XmlSerializer(Type type) public Xml ...
- VS2013打开2008的项目
找到 .csproj 后缀的文件.然后右键选择文本打开. 找到下面一段话: <ProjectTypeGuids>-00065b846f21};{fae04ec0-301f-11d3-bf4 ...
- Java内存泄露监控工具:JVM监控工具介绍【转】
jstack?-- 如果java程序崩溃生成core文件,jstack工具可以用来获得core文件的java stack和native stack的信息,从而可以轻松地知道java程序是如何崩溃和在程 ...
- iOS - ShareSDK第三方分享(图文和视频)和登录
由于近期工作需要自己抽时间搞了一下第三方分享,这里使用的是shareSDK的第三方,在使用的过程中有一些心得和体会,特在此和大家分享一下~ 1.在经过将近一周时间的开发,终于搞定ios分享了. 2.由 ...
- python获取windows所有com口
import serial import serial.tools.list_ports port_list = list(serial.tools.list_ports.comports()) po ...
- vue之介绍
vue的作者叫尤雨溪,中国人.自认为很牛逼的人物,也是我的崇拜之神. 关于他本人的认知,希望大家读一下这篇关于他的文章,或许你会对语言,技术,产生浓厚的兴趣.https://mp.weixin.qq. ...
- Equinox P2 介绍(一)Getting Start
一直觉得 Equinox 的 P2 是个神秘的东西,常常使得 Eclipse 或 Equinox 表现出一些奇怪的行为,于是找来官方文档读一读,试图更好地理解与应用 Equinox . 官方文档很多, ...
- easyui---easyloader.js
1.easyloader.js 是根据用户指定,动态加载组件,可以替换下面jqueryeasyui <!-- <script type="text/javascript" ...
- [No00004F]史上最全Vim快捷键键位图(入门到进阶)vim常用命令总结
在命令状态下对当前行用== (连按=两次), 或对多行用n==(n是自然数)表示自动缩进从当前行起的下面n行.你可以试试把代码缩进任意打乱再用n==排版,相当于一般IDE里的code format.使 ...