poj 2418 字典树 Map容器
2015-08-24 16:32
459 查看
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.
Source
Waterloo Local 2002.01.26
字典树
Map
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 20877 | Accepted: 8186 |
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.
Source
Waterloo Local 2002.01.26
字典树
</pre><pre name="code" class="cpp">#include<cstdio> //c++ 750ms<span id="transmark"></span> #include<cstdlib> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #define INF 0x3f3f3f3f using namespace std; struct node { int num; node *next[130]; }*head,*p; struct N { char name[35]; }str[10010]; node* Creat() { node *p; p=new node; p->num=0; memset(p->next,0,sizeof(p->next)); return p; } int len,flag; int Build(char s[]) { p=head; len=strlen(s); for(int i=0;i<len;i++) { int x=s[i]; if(!p->next[x]) { p->next[x]=Creat(); } p=p->next[x]; } if(p->num==0) { p->num++; return 1; } else { p->num++; return 0; } } int Query(N pp) { p=head; len=strlen(pp.name); for(int i=0;i<len;i++) { int x=pp.name[i]; p=p->next[x]; } return p->num; } bool cmp(N p1,N p2) { return strcmp(p1.name,p2.name)<0; } int main() { char s[35]; head=Creat(); int t=0,num=0; while(gets(s)!=NULL) { flag=Build(s); if(flag) { strcpy(str[num].name,s); num++; } t++; } sort(str,str+num,cmp); for(int i=0;i<num;i++) { int x=Query(str[i]); printf("%s %.4f\n",str[i].name,100.0*x/t); } }
Map
#include <iostream> //交C++为 CE #include <stdio.h> #include <iomanip> #include <string.h> #include <map> using namespace std; int main() { std::ios::sync_with_stdio(false);//不加会8000ms,加了2100ms map<string,int>mp; int cnt=0; string s; while(getline(cin,s)) { mp[s]++; cnt++; } //迭代器就是指针。指向这个关联容器啊,关联容器有键和值,first就是键,second就是值。 map<string,int >::iterator i; //比单纯的输出map[]快很多 cout.setf(ios::fixed); //控制输出格式 for(i=mp.begin();i!=mp.end();i++) { cout<<setprecision(4)<<i->first<<" "<<100.0*(i->second)/cnt<<endl; } return 0; } /* #include <iostream> #include <stdio.h> #include <iomanip> #include <string.h> #include <map> using namespace std; int main() { map<string,int>mp; int cnt=0; char s[100]; string ss; while(gets(s)) //在G++中 单纯将char类型存入map中会超时,但将char存入string类中会跑2400ms { ss=s; mp[ss]++; cnt++; } // while(getline(cin,s)) // { // mp[s]++; // cnt++; // } map<string,int >::iterator i; cout.setf(ios::fixed); for(i=mp.begin();i!=mp.end();i++) { cout<<setprecision(4)<<i->first<<" "<<100.0*(i->second)/cnt<<endl; } return 0; } #include<iostream> #include<iomanip> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<map> //#define RR freopen("in.txt","r",stdin); #define INF 0x3f3f3f3f using namespace std; char s1[39]; string s[10010],d; bool cmp(string s1,string s2) { return s1<s2; } int main() { std::ios::sync_with_stdio(false); //用于纯C++的输入输出(cin,cout)//3900ms map<string,int>Q; int num=0,t=0; while(gets(s1)!=NULL) { d=s1; t++; Q[d]++; if(Q[d]==1) s[num++]=d; } sort(s,s+num,cmp); cout.setf(ios::fixed); for(int i=0;i<num;i++) { //double ss=Q[s[i]]*100.00000/t; cout<<s[i]<<" "<<setprecision(4)<<Q[s[i]]*100.00000/t<<"\n"; //printf(" %.4f\n",ss); } // for(int i=0;i<num;i++) // { // double ss=Q[s[i]]*100.00000/t; // cout<<s[i]; // printf(" %.4f\n",ss); // } }<span id="transmark"></span> */
相关文章推荐
- 离开单位后社保会被冻结吗?
- Android ----waiting for debugger .
- 信号量CSemaphore的使用
- 黑马程序员_面向对象之继承
- Android线程和线程Handler基础一览
- Spring MVC BeanNameUrlHandlerMapping example
- 基于jquery实现在线选座订座之影院篇
- js淡入淡出的图片轮播效果代码分享
- Qt中如何利用QPushButton来实现一个弹出菜单
- 使用sbt构建scala应用
- SQLite学习笔记(9)-SQLite的简单使用
- 剑指offer面试题21-包含min函数的栈
- CSS3 照片墙
- 中国企业500强
- 递归和动态规划
- hdu2519(二维完全背包)
- java 值传递和参数传递
- HDU2222-Keywords Search AC自动机
- android-resource-remover的研究和实验
- webview加载应用宝 网页版界面,简单实现