HDU-2418:Hardwood Species

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.

trie树第一道题,将所有字符串放到一个trie树中,然后dfs一下就是答案了(正好也是字典序)。不过指针运用还不熟悉,调了好久。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
char st[11000];
int sum,tot;
struct node
{
    node *son[200];
    int cnt;
    node()
    {
        cnt=0;
        for(int i=0;i<200;i++) son[i]=NULL;
    }
}*root=new node;
void trie()
{
    int len=strlen(st);
    node *now=root;
    for(int i=0;i<len;i++)
    {
        int tmp=st[i];
        if(now->son[tmp]==NULL)
            now->son[tmp]=new node;
        now=now->son[tmp];
    }
    now->cnt++;
    return ;
}
char ch[11000];
void dfs(node *x)
{
    if(x->cnt)
    {
        for(int i=1;i<=tot;i++)
            printf("%c",ch[i]);
        printf(" %.4lf\n",x->cnt*100.0/sum);
    }
    for(int i=0;i<200;i++)
    {
        if(x->son[i]!=NULL)
        {
            ch[++tot]=i;
            dfs(x->son[i]);
            tot--;
        }
    }
}
int main()
{
    while(gets(st))
    {
        trie();
        sum++;
    }
    dfs(root);
    return 0;
}

发表评论

邮箱地址不会被公开。 必填项已用*标注