POJ2418 Hardwood Species(二叉搜索树+字典树+MAP)


Hardwood Species
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 21179 Accepted: 8304

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
题意:输入树的名字 然后按字典序顺序输出树名+所占的百分数
第一思路是用map做,但是仍然没能找到错误大哭
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <string.h>
using namespace std;
map<string,int> tree;
struct Node
{
    char str[35];
    int num;
};
Node node[10010];
int cmp( Node x, Node y )
{
   return strcmp(x.str,y.str) < 0;
}
int main()
{
    char s[35];
    int sum = 0,cnt = 0;
    tree.clear();
    while(gets(s))
    {
        sum ++;
        if(tree.count(s) == 0)
        {
            strcpy(node[cnt].str,s);
            node[cnt].num = 1;
            tree[s] = cnt++;
        }
        else
        {
            node[tree[s]].num ++;
        }
    }
    sort(node,node + cnt,cmp);
    for(int i = 0; i < cnt; i++)
    {
        printf("%s %.4lf\n",node[i].str,node[i].num * 0.1 * 1000 / ( sum ));
    }

    return 0;
}


二叉搜索树也是可以的,之前做的二叉搜索树都是数字,这次是字符串,但本质一样,在构造树的过程中,因为没判断好strcmp()所以第一次树构造反了,然后中序遍历。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <string.h>
using namespace std;
const int MAX = 10005;
struct node
{
    int l,r;
    char str[35];
    int num;
};
node tree[MAX];
int cnt,sum;
void build(char s[],int root)
{
    if(tree[root].num ==0)
    {
        strcpy(tree[root].str,s);
        tree[root].num ++;
        return;
    }
    if(strcmp(tree[root].str,s) > 0)
    {
        if(tree[root].l == 0)
            tree[root].l = cnt++;
        build(s,tree[root].l);
    }
    else if(strcmp(tree[root].str,s) < 0)
    {
        if(tree[root].r == 0)
            tree[root].r = cnt++;
        build(s,tree[root].r);
    }
    else if(strcmp(tree[root].str,s) == 0)
    {
        tree[root].num ++;
        return;
    }
}
void bianli(int root)
{
    if(tree[root].num == 0)
        return ;
    if(tree[root].l != 0)
        bianli(tree[root].l);
    printf("%s %0.4lf\n",tree[root].str,tree[root].num * 100.0 / sum);
    if(tree[root].r != 0)
        bianli(tree[root].r);
}
int main()
{
    for(int i = 0; i < MAX; i++)
        tree[i].l = tree[i].r = tree[i].num = 0;
    char s[35];
    gets(s);
    strcpy(tree[0].str,s);
    tree[0].num = 1;
    sum = cnt = 1;
    while(gets(s))
    {
        sum ++;
        build(s,0);
    }

    bianli(0);
    return 0;
}

这道题被分类到字典树,之前一直纠结字符串带空格怎么办,没什么因为没说一定是字母,故空格什么的符号都有可能,然后就把next[]开成所有的字符,本质跟一般字典树是一样的。输出的时候dfs搜索就可以了,以为next[]是从小到大的,深搜就行

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <string.h>
using namespace std;
const int MAX = 256;
struct node
{
    int cnt;
    node *next[MAX];
};
node *root;
char word[35];
int n;
void build(char *s, node *root)
{
    node *q,*p;
    p = root;
    int len = strlen(s);
    for(int i = 0; i < len; i++)
    {
        int id = s[i];
        if(p->next[id] == NULL)
        {
            q = (node *) malloc(sizeof(node));
            q ->cnt = 0;
            for(int j = 0; j < MAX; j++)
                q ->next[j] = NULL;
            p ->next[id] = q;
            p = p ->next[id];
        }
        else
        {
            p = p ->next[id];
        }
    }
    p -> cnt ++;
}
void dfs_travel(node *root,int pos)
{
    if(root -> cnt)
    {
        word[pos] = '\0';
        printf("%s %0.4lf\n",word,root->cnt * 100.0 / n);
    }

    for(int i = 0; i < MAX; i++)
    {
        if(root->next[i] != NULL)
        {
            word[pos] = i;
            dfs_travel(root ->next[i],pos + 1);
        }
    }

}
int main()
{
    n = 0;
    char s[35];
    root = (node *) malloc(sizeof(node));
    for(int i = 0; i < MAX; i++)
        root->next[i] = NULL;
    root ->cnt = 0;
    while(gets(s))
    {
        build(s,root);
        n ++;
    }
    dfs_travel(root,0);
    return 0;
}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值