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
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.
题意:输入多行,每行代表一种树的名字,输入结束后,按字典序输出这些树的名字并且输出每种树出现的频率,频率保留4位小数。
思路:直接用map就能过,当然这个也可以用字典树,但弱太渣了,不知道怎么用字典树取重(23333
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.
map做法 356K 3110MS:
#include <cstdio>
#include <iostream>
#include <iterator>
#include <map>
#include <string>
#include <cstring>
using namespace std;
int main()
{
//freopen("lalala.text","r",stdin);
string st;
map <string,int> s; //int记录该种树出现的次数
int sum=0;
while(getline(cin,st)) //可以用getline输入含有空格的字符串,其结束符默认是换行符,而cin不可以输入含有空格的字符串
{
s[st]++;
sum++;
}
map<string,int>::iterator it;
for(it=s.begin(); it!=s.end(); it++) //字符串按字典序输出
{
cout<<it->first;
printf(" %.4lf\n",(double)it->second/sum*100);
}
return 0;
}
字典树做法 1784K 766MS:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#define MAX 100005
using namespace std;
int ch[MAX][150]; //该数组记录的是某字母节点的编号而并不是该字母所以用整数型
int value[MAX];
int cnt,sizes;
struct node //将二维字符串数组定义成结构体可以重载运算符,然后可以直接按字典须排序
{
char s[50];
bool operator < (const struct node aa)const
{
return strcmp(s,aa.s)<0;
}
} st[MAX];
void init()
{
memset(ch[0],0,sizeof(ch[0]));
cnt=0;
sizes=1;
}
int insertt(char *sh) //插入字符串
{
int n=0,len=strlen(sh);
for(int i=0; i<len; i++)
{
int c=sh[i];
if(!ch[n][c])
{
memset(ch[sizes],0,sizeof(ch[sizes]));
ch[n][c]=sizes++;
}
n=ch[n][c];
}
value[n]++; //只要出现该字符串,那么该字符串的末端字母计数器就++
if(value[n]>1) //如果该字符串已经出现过一次则不用再存入结构体中
return 0;
else
return 1;
}
int serch(char *sh)
{
int n=0,len=strlen(sh);
for(int i=0; i<len; i++)
{
int c=sh[i];
if(!ch[n][c])
return 0;
n=ch[n][c];
}
return value[n]; //查询该字符串,返回该字符串出现的次数
}
int main()
{
freopen("../oo.text","r",stdin);
init();
int ans=0;
char ss[50];
while(gets(ss))
{
int flu=insertt(ss);
if(flu)
strcpy(st[ans++].s,ss);
cnt++;
}
sort(st,st+ans); //直接排序
for(int i=0; i<ans; i++)
{
double cc=(double)(serch(st[i].s))/cnt*100;
printf("%s %.4lf\n",st[i].s,cc);
}
return 0;
}