http://poj.org/problem?id=2418
Hardwood Species
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 13412 | Accepted: 5502 |
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.
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
//差点忘了这题是有关树的被我当作字符串做了嘻嘻。。。还是给标准的解法吧。。还没有研究
题意:统计每个字符串出现占所有字符串出现次数比例。这题以前做过类似的,找了一下果然找到了。。嘻嘻。
http://blog.sina.com.cn/s/blog_99ca2df50101905b.html看看吧。。是不是。哥的记忆力还是可以的。。
还是在具体说下吧。就是先把输入的单词按照字典序排序之后再通过相邻的前后两个进行比较,相同的只能在相邻,一不相同就输出。。。简单吧。。
还有个地方提示下,输出的时候用cout会方便很多的,我用printf()坑爹啊居然编译都有问题。。嗨。。自己太弱了,干脆直接cout。。。
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
using namespace std;
int n;
char word[1000001][31];
int cmp_string(const void*_a,const void*_b)
{
char *a=(char*)_a;
char *b=(char*)_b;
return strcmp(a,b);
}
int main()
{
int n=0,i;
while(gets(word[n])!='\0')
{
//sscanf(s,"%s",word[n]);
n++;
}
qsort(word,n,sizeof(word[0]),cmp_string);
int cnt=0;
for(i=0;i<n;i++)
{
cnt++;
if(strcmp(word[i+1],word[i]))
{
cout<<word[i]<<" ";
printf("%.4lf\n",100*(double)cnt/(double)(n));
cnt=0;
//printf("%s %.4lf\",word[i][0],cnt);
}
//cnt=0;
}
return 0;
}
#include<stdlib.h>
#include<iostream>
#include<string.h>
using namespace std;
int n;
char word[1000001][31];
int cmp_string(const void*_a,const void*_b)
{
char *a=(char*)_a;
char *b=(char*)_b;
return strcmp(a,b);
}
int main()
{
int
while(gets(word[n])!='\0')
{
//sscanf(s,"%s",word[n]);
n++;
}
qsort(word,n,sizeof(word[0]),cmp_string);
int cnt=0;
for(i=0;i<n;i++)
{
cnt++;
if(strcmp(word[i+1],word[i]))
{
cout<<word[i]<<" ";
printf("%.4lf\n",100*(double)cnt/(double)(n));
cnt=0;
//printf("%s %.4lf\",word[i][0],cnt);
}
//cnt=0;
}
return 0;
}
| |
348K | 1407MS | C++ | 1340B |
hihihi这次终于给标准的二叉搜索树的代码了,很多地方还需要理解:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node{
char name[33];
int times;//记录该结点单词出现的次数;
struct node *left;//结点的左右孩子
struct node *right;
}*tree;
int tot=0;//二叉树的树根
node *build(node *root,char s[])//建树
{
if(root==NULL)
{
root=new node;
root->left=NULL;
root->right=NULL;
root->times=1;//单词的出现次数加1
strcpy(root->name,s);//单词复制给新开辟的结点
return root;
}
else
{
int t=strcmp(root->name,s);
if(t==0)//出现相同的单词
root->times++;//该单词数+1,并且不在重复插入
else if(t>0)//如果插入单词小于当前结点
root->left=build(root->left,s);//插入当前单词的左子树
else root->right=build(root->right,s);//否则插入右子树
return root;
}
}
void midorder(node *root)//按照中序遍历模式计算输出就是字典序
{
if(root!=NULL)
{
midorder(root->left);
printf("%s %.4f\n",root->name,root->times*1.0/tot*100);
//visit(root);
midorder(root->right);
}
}
int main()
{
char s[33];
while(gets(s)!=NULL)
{
tot++;
tree=build(tree,s);//将输入的单词插入二叉树中
}
midorder(tree);//从树根开始计算每个单词
return 0;
}
#include<string.h>
#include<stdlib.h>
struct node{
}*tree;
int tot=0;//二叉树的树根
node *build(node *root,char s[])//建树
{
}
void midorder(node *root)//按照中序遍历模式计算输出就是字典序
{
}
int main()
{
}