POJ 2418 Hardwood Species (字典树 || map)
2016-08-18 08:09
483 查看
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
Sample Output
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceeded.
分析:
给出一系列的字符串,有相同的,问每个字符串出现的次数站总字符串个数的百分比是多少,输出的时候按字符串的字典序输出。
第一种方法:用STL里面的map<string ,int >, first为字符串,int为出现的个数,更重要的是map是默认string按字典序从小到大排列的,所以输出的时候直接从头到尾就可以了。
第二种方法:建立Trie树,对于输入的每一个字符串都插入到树中,每个字符串在树中的最后一个字母节点保存着该字符串出现的次数,并用一个ok标记该节点是不是一个字符串的最后一个字母节点。
法一:字典树
法二:map
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 23543 | Accepted: 9169 |
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 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
This problem has huge input, use scanf instead of cin to avoid time limit exceeded.
分析:
给出一系列的字符串,有相同的,问每个字符串出现的次数站总字符串个数的百分比是多少,输出的时候按字符串的字典序输出。
第一种方法:用STL里面的map<string ,int >, first为字符串,int为出现的个数,更重要的是map是默认string按字典序从小到大排列的,所以输出的时候直接从头到尾就可以了。
第二种方法:建立Trie树,对于输入的每一个字符串都插入到树中,每个字符串在树中的最后一个字母节点保存着该字符串出现的次数,并用一个ok标记该节点是不是一个字符串的最后一个字母节点。
法一:字典树
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 127; int num; struct tree { int cnt; //某个字符串出现的总个数 bool ok; //是不是走到了字符串的最后一个字母,保存最后一个字母的那个节点也保存着整个字符串的Name char name[40]; //保存的字符串 tree *next[maxn]; //子节点,ASCII最大值为126 tree() { cnt = 0; ok = 0; memset(next, 0, sizeof(next)); } }; tree *root; void Insert(char *word) { tree *tmp = root; int len = strlen(word); for (int i = 0; i < len; i++){ int x = word[i]; if (tmp -> next[x] == NULL){ tmp -> next[x] = new tree; } tmp = tmp -> next[x]; } tmp -> cnt++; //走到字符串的最后一个字母的节点 strcpy(tmp -> name, word); tmp -> ok = 1; } void Search(tree *root) //递归输出 { tree *tmp = root; if (tmp -> ok){ printf("%s %.4lf\n", tmp -> name, tmp -> cnt * 100.0 / num); } for (int i = 0; i < 127; i++){ if (tmp -> next[i] != NULL){ Search(tmp -> next[i]); } } } int main() { root = new tree; char s[40]; num = 0; while (gets(s)){ Insert(s); num++; } Search(root); return 0; }
法二:map
#include <iomanip> #include <string> #include <iostream> #include <map> using namespace std; int main() { map<string, int> mp; int cnt = 0; string s; while (getline(cin, s)){ mp[s]++; cnt++; } map<string, int> :: iterator i; for (i = mp.begin(); i != mp.end(); i++){ cout << i -> first << " " << fixed << setprecision(4) << i -> second * 100.0 / cnt << endl; } return 0; }
相关文章推荐
- Linux 平台下 Python 脚本编程入门(一)
- 数组和链表
- Linux归档压缩、脚本编程之循环控制
- Matlab问答记day1
- 2016/8/17总结
- 1041. 考试座位号(15)
- 两种异步处理的源码分析(一):handler,message,looper
- POJ 2001 Shortest Prefixes (zidiansh)
- 为什么print在python3中变成了函数?
- 安卓中的存储(SQLite)
- POJ 3411 Paid Roads
- This system is not registered with RHN问题解决
- 浙江移动亿万级实时流计算之路与踩过的坑
- JAVA 网络编程(2) SOCKET 非阻塞NIO示例
- #android#activity
- "传输速率每秒100G "
- unity小知识集合帖
- java后台判断客户端是手机/PC并返回不同页面
- package explorer不报错,项目和包以及类都不提示不提示红x
- Submatrix Sum equals to K