- 武逸程's Note
哈希表——map
- 2023-10-29 10:17:37 @
POJ - 2418 Hardwood Species
题目描述
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 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.
输出
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.
样例输入
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
样例输出
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
代码
#include<cstdio>
#include<iostream>
#include<map>
#include<cstring>
using namespace std;
map<string,int> m;
int main(void){
string s;
int ans=0;
//freopen("a.in","r",stdin);//测试的时候注意用 freopen
while(getline(cin,s)){
m[s]++;
ans++;
}
for(map<string,int>::iterator it=m.begin();it!=m.end();it++){
cout<<it->first<<" ";
printf("%.4f\n",100.0*(it->second)/ans);
}
return 0;
}
HDU - 4585 Shaolin
题目描述
Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every year, trying to be a monk there. The master of Shaolin evaluates a young man mainly by his talent on understanding the Buddism scripture, but fighting skill is also taken into account. When a young man passes all the tests and is declared a new monk of Shaolin, there will be a fight , as a part of the welcome party. Every monk has an unique id and a unique fighting grade, which are all integers. The new monk must fight with a old monk whose fighting grade is closest to his fighting grade. If there are two old monks satisfying that condition, the new monk will take the one whose fighting grade is less than his. The master is the first monk in Shaolin, his id is 1,and his fighting grade is 1,000,000,000.He just lost the fighting records. But he still remembers who joined Shaolin earlier, who joined later. Please recover the fighting records for him.
输入
There are several test cases. In each test case: The first line is a integer n (0 <n <=100,000),meaning the number of monks who joined Shaolin after the master did.(The master is not included).Then n lines follow. Each line has two integer k and g, meaning a monk's id and his fighting grade.( 0<= k ,g<=5,000,000) The monks are listed by ascending order of jointing time.In other words, monks who joined Shaolin earlier come first. The input ends with n = 0.
输出
A fight can be described as two ids of the monks who make that fight. For each test case, output all fights by the ascending order of happening time. Each fight in a line. For each fight, print the new monk's id first ,then the old monk's id.
样例输入
3
2 1
3 3
4 2
0
样例输出
2 1
3 2
4 2
代码
#include<cstdio>
#include<iostream>
#include<map>
#define N 1000000005
using namespace std;
map<int,int> m;//level:it->first id:it->second
int n,id,g;
int main(void){
ios::sync_with_stdio(0),cin.tie(0);//cin的优化
while(cin>>n&&n!=0){
m.clear();
m[N]=1;//老和尚的数据
while(n--){
int id,g,ans;
cin>>id>>g;
m[g]=id;//进队
map<int,int>::iterator it1=m.find(g);//找到排序后的位置
if(it1==m.begin()) ans=(++it1)->second;//找最小的
else{
map<int,int>::iterator it2=it1;
it2--,it1++;//找到当前和尚左右两边的对手
ans=(g-it2->first<=it1->first-g)?it2->second:it1->second;
}
cout<<id<<" "<<ans<<endl;
}
}
return 0;
}