poj2488(二叉查找树||串)

来源:互联网 发布:办公网络服务器 编辑:程序博客网 时间:2024/05/22 02:48

http://poj.org/problem?id=2418

Hardwood Species
Time Limit: 10000MSMemory Limit: 65536KTotal Submissions: 13412Accepted: 5502

Description

Hardwoods are the botanicalgroup of trees that have broad leaves, produce a fruit or nut, andgenerally go dormant in the winter.
America's temperate climates produce forests with hundreds ofhardwood species -- trees that share certain biologicalcharacteristics. Although oak, maple and cherry all are types ofhardwood trees, for example, they are different species. Together,all the hardwood species represent 40 percent of the trees in theUnited States.

On the other hand, softwoods, or conifers, from the Latin wordmeaning "cone-bearing," have needles. Widely available US softwoodsinclude cedar, fir, hemlock, pine, redwood, spruce and cypress. Ina home, the softwoods are used primarily as structural lumber suchas 2x4s and 2x6s, with some limited decorative applications.

Using satellite imaging technology, the Department of NaturalResources has compiled an inventory of every tree standing on aparticular day. You are to compute the total fraction of the treepopulation represented by each species.

Input

Input to your program consistsof a list of the species of every tree observed by the satellite;one tree per line. No species name exceeds 30 characters. There areno more than 10,000 species and no more than 1,000,000 trees.

Output

Print the name of each speciesrepresented in the population, in alphabetical order, followed bythe percentage of the population it represents, to 4 decimalplaces.

Sample Input

Red AlderAshAspenBasswoodAshBeechYellow BirchAshCherryCottonwoodAshCypressRed ElmGumHackberryWhite OakHickoryPecanHard MapleWhite OakSoft MapleRed OakRed OakWhite OakPoplanSassafrasSycamoreBlack WalnutWillow

Sample Output

Ash 13.7931Aspen 3.4483Basswood 3.4483Beech 3.4483Black Walnut 3.4483Cherry 3.4483Cottonwood 3.4483Cypress 3.4483Gum 3.4483Hackberry 3.4483Hard Maple 3.4483Hickory 3.4483Pecan 3.4483Poplan 3.4483Red Alder 3.4483Red Elm 3.4483Red Oak 6.8966Sassafras 3.4483Soft Maple 3.4483Sycamore 3.4483White Oak 10.3448Willow 3.4483Yellow Birch 3.4483

Hint

This problem has huge input, usescanf instead of cin to avoid time limit exceeded.

Source

Waterloo Local 2002.01.26
//差点忘了这题是有关树的被我当作字符串做了嘻嘻。。。还是给标准的解法吧。。还没有研究
http://blog.sina.com.cn/s/blog_789f45310100zzj1.html
题意:统计每个字符串出现占所有字符串出现次数比例。这题以前做过类似的,找了一下果然找到了。。嘻嘻。
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;
charword[1000001][31];
intcmp_string(constvoid*_a,constvoid*_b)
{
char*a=(char*)_a;
char*b=(char*)_b;
returnstrcmp(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);
intcnt=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;
}
30576K1891MSC++642B

 

 

 

 348K1407MSC++

1340B

 

 

http://www.cppblog.com/Victordu/archive/2008/08/16/59079.html模版
还有一种更简单的方法http://www.cnblogs.com/hxsyl/archive/2012/08/07/2627494.html
hihihi这次终于给标准的二叉搜索树的代码了,很多地方还需要理解:#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node{
   charname[33];
   inttimes;//记录该结点单词出现的次数;
   struct node*left;//结点的左右孩子
   struct node*right;
}*tree;
inttot=0;//二叉树的树根
node *build(node*root,chars[])//建树
{
   if(root==NULL)
   {
       root=new node;
       root->left=NULL;
       root->right=NULL;
       root->times=1;//单词的出现次数加1
       strcpy(root->name,s);//单词复制给新开辟的结点
       return root;
   }
   else
   {
       intt=strcmp(root->name,s);
       if(t==0)//出现相同的单词
           root->times++;//该单词数+1,并且不在重复插入
       elseif(t>0)//如果插入单词小于当前结点
           root->left=build(root->left,s);//插入当前单词的左子树
       elseroot->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()
{
       chars[33];
   while(gets(s)!=NULL)
   {
       tot++;
       tree=build(tree,s);//将输入的单词插入二叉树中
   }
   midorder(tree);//从树根开始计算每个单词
   return 0;
}
原创粉丝点击