程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> POJ 2418 Hardwood Species(字典樹)

POJ 2418 Hardwood Species(字典樹)

編輯:關於C++
Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 20085   Accepted: 7911

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.

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.
題目大意:給一組各種物種的樹,然後輸出每種樹的占總數的比例。
字典樹吧。
這題有點坑,如果你按大小寫26個字母加空格來處理的話,那你就RE到死吧,其實它給的數據不光是字母和空格,是所有字符。 也就是你的字典樹是個126-32+1=95叉樹,為了編寫方便就是0~126(127叉樹)
出現次數由節點上的變量cnt維護。
按字典序輸出各字符的:采用樹的中序遍歷+回溯。
不難又不錯的題,下面是字典樹版:
Memory: 3092K Time: 782MS

#include 
#include 
#include 

const int maxn=127;		//127叉樹

typedef struct Node{
	int cnt;
	struct Node *ch[maxn];
}node;

node *T;
int tot;

void insert(char s[]){
	int i,j,len=strlen(s);
	node *u;
	u=T;
	for(i=0;ich[id]==0){
			node *t=(node*)malloc(sizeof(node));
			for(j=0;jch[j]=0;
			t->cnt=0;
			u->ch[id]=t;
		}
		u=u->ch[id];
	}
	u->cnt++;
}

char name[40];
	
void ask(node* T,int cur){
	if(T==0) return ;
	int i;
	double res;
	if(T->cnt) {
		name[cur]='\0';
		res=(double)T->cnt/tot*100;
		printf("%s %.4f\n",name,res);
	}
	for(i=0;ich[i]!=0){
			name[cur]=i;
			ask(T->ch[i],cur+1);
		}
	return ;
}

int main()
{
	int i,res,len;
	char str[40];
	T=(node*)malloc(sizeof(node));
	for(i=0;ich[i]=0;
	T->cnt=0;
	tot=0;
	while(gets(str)){
		if(str[0]=='\0') break;
		insert(str);
		tot++;
	}
	ask(T,0);
	return 0;
}

這題map也能過:
Memory: 916K Time: 8735MS

#include 
#include 
#include 
#include
#include 
using namespace std;

int main()
{
	string str;
	int tot=0;
	map mp;
	while(getline(cin,str)){
		mp[str]++;
		tot++;
	}
	map::iterator it;
	for(it=mp.begin();it!=mp.end();it++){
		cout<first;
		printf(" %.4f\n",(double)it->second/tot*100);
	}
	return 0;
}

快排也能過。。
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved