POJ2418Hardwood Species(Tire树)

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.

题目大意:
给出一大堆树名,统计每种树名出现的频率。按字典序输出该种树名 及其频率。 1e6 课树,每个树名不超过30个字节。
分析:
朴素想法是先将每种树名排序 (时间复杂度O(30nlgn))然后再扫一遍统计。操作上可行,但时间会超。
于是泥需要一棵字典树。
树的每个节点代表代表一个字符,从根节点到这节点路上的所有字符构成了这个字符串。这样建树的复杂度是n*30;查询的复杂度是(种类数)m*30.
主要还是如何用指针实现的问题。
以前没有用过指针。
就目前对这个东西的理解呢,简单来说就是:指针就是一个变量类型。
只是它有一些特别。它是直接指向的这个变量在计算机内部的地址的。然后可以动态的开指针,用多少就向内存申请多少空间。

依稀记得……好像pascal也有这种东西……?

// Created by ZYD in 2015.
// Copyright (c) 2015 ZYD. All rights reserved.
//

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
const int maxn=1e6+5;
#define Size 100000
#define ll long long
#define mk make_pair
#define pb push_back
#define mem(array) memset(array,0,sizeof(array))
typedef pair<int,int> P;
struct node
{
    int cnt;
    int up;
    node* nxt[150];
    node()
    {
        for(int i=0;i<150;i++)
            nxt[i]=NULL;
        cnt=0;
    }
};
struct node* root=new node();
int tot;
char word[31];

int  insert(node* root,char* s)
{
    int index;
    struct node*p=root;
    for(int i=0;i<strlen(s);i++)
    {
        index=s[i];
        if(p->nxt[index]==NULL) p->nxt[index]=new node();
        p=p->nxt[index];
    }
    p->cnt++;
    return 0;
}
int search(node* rt,int t)
{
     node* p=rt;
     int index,fl=0;
     for(int i=0;i<150;i++)
     {
        if(p->nxt[i]!=NULL)
        {
            fl=1;
            word[t]=i;
            search(p->nxt[i],t+1);
        }
     }
     int ans=p->cnt;
     // double x=ans/tot;
     // cout<<x;
     if(fl==0) 
        {
            for(int i=0;i<t;i++)
                printf("%c",word[i]);
            printf(" %.4lf\n",double(ans*100.0/tot));
        }
     return 0;

}
char s[35];
int main()
{
    //freopen("in.txt","r",stdin);
    tot=0;
    mem(word);
    // cin>>s[1];cout<<s[1];
    while(gets(s)) 
    {
        tot++;
        insert(root,s);
    }
    if(tot) search(root,0);
    // cout<<s[tot-1];
    return 0;
}

你可能感兴趣的:(poj)