传送门:Hardwood Species
总时间限制: 10000ms 内存限制: 65536kB
描述
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.
输出
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.
样例输入
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
提示
This problem has huge input, use scanf instead of cin to avoid time limit exceeded.
来源
Waterloo Local 2002.01.26
分析:
- 解法一:利用map存储单词项,每次更新value值,最后循环输出;
- 解法二:构建一个二叉查找树(又叫二叉搜索树/二叉排序树),然后中序遍历。
二叉查找树:
它或者是一棵空树,或者是具有下列性质的二叉树:
若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
它的左、右子树也分别为二叉排序树。
- 解法三:构建一个字典树(这种方法还没写好)
1. 解法一:利用map
1.1乱写版本 2468ms
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <iomanip>
#include <map>
using namespace std;
map<string,int> ma;
map<string,int>::iterator i;
int main()
{
string a;
int sum = 0;
while(getline(cin,a)){
int count = ma[a]+1;
ma.erase(a);
ma.insert(make_pair(a,count));
sum++;
}
for (i = ma.begin(); i != ma.end(); ++i)
cout << i->first << " " << fixed << setprecision(4) << i->second*100.0/sum << endl;
return 0;
}
1.2改进版本 1313ms
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <iomanip>
#include <map>
using namespace std;
map<string,int> ma;
map<string,int>::iterator i;
int main()
{
string a;
int sum = 0;
while(getline(cin,a)){
ma[a]++;
sum++;
}
for (i = ma.begin(); i != ma.end(); ++i)
printf("%s %.4f\n",i->first.c_str(),i->second*100.0/sum);
return 0;
}
由于printf不能直接输出扩展类中的string类型数据,因为printf输出字符串是针对char*的,只能输出c语言内置数据,所以使用printf输出时,要利用c_str()这个方法获取string对象的字符串的首地址,才能正常输出。
注意:由于printf需要调用c_str方法,所以整体和cout输出相比谁快谁慢不一定,需要看数据大小和编译环境等太多因素。但是对于本题来说,通过实验验证,第一,两种差距不大,第二,两者都很慢。
2. 解法二:构建二叉搜索树 497ms
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
struct node
{
char name[35];
int num;
node *l,*r;
node():name({0}),num(0),l(NULL),r(NULL){}
};
char tmp[35];
int sum = 0;
void insertNode(node *root,char s[35])
{
if(root->name[0] == 0) {
strcpy(root->name,s);
root->num += 1;
return;
}
int flag = strcmp(root->name,s);
if(flag > 0) {
if(root->l == NULL) root->l = new node();
insertNode(root->l,s);
}
else if(flag < 0) {
if(root->r == NULL) root->r = new node();
insertNode(root->r,s);
}
else root->num++;
}
void outputTree(node *root)
{
if(root == NULL) return;
outputTree(root->l);
printf("%s %.4f\n",root->name, 100.0*root->num/sum);
outputTree(root->r);
}
int main()
{
node *root = new node();
gets(tmp);
strcpy(root->name,tmp);
root->num++;
sum += 1;
while(gets(tmp))
insertNode(root,tmp),sum++;
outputTree(root);
return 0;
}
3. 解法三:构建字典树 199ms
0~31及127(共33个)是控制字符或通信专用字符(其余为可显示字符)
32~126(共95个)是字符(32是空格)
其中48~57为0到9十个阿拉伯数字。
65~90为26个大写英文字母,
97~122号为26个小写英文字母,其余为一些标点符号、运算符号等。
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <list>
#include <sstream>
#include <set>
#include <functional>
using namespace std;
int num = 0;
const int nameLen = 31;
const int childNum = 127;//字符最大的ascii码为126
struct node
{
char name[nameLen];
int count;
bool ok ;
node *child[childNum];
node(){//同名构造函数
count = 0;
ok = false;
for (int i = 0; i < childNum; ++i) child[i] = NULL;
}
};
node root;
void insertNode(char s[nameLen])
{
node *r = &root;
int length = strlen(s);
for (int i = 0; i < length; ++i){
int intS = s[i];
if(r->child[intS] == NULL) r->child[intS] = new node();
r = r->child[intS];
}
strcpy(r->name,s);
r->count++;
r->ok = true;
}
void outputTree(node *root)
{
if(root->ok) printf("%s %.4f\n", root->name,root->count*100.0/num);
for (int i = 0; i < childNum; ++i)
if(root->child[i] != NULL) outputTree(root->child[i]);
}
int main()
{
char s[31];
while(gets(s)) insertNode(s),num++;
outputTree(&root);
return 0;
}