poj 2418 Hardwood Species (trie 树)

链接:poj 2418

题意:给定一些树的种类名,求每种树所占的百分比,并按字典序输出

分析:实质就是统计每种树的数量n,和所有树的数量m,

百分比就为 n*100./m

由于数据达到一百万,直接用数组查找肯定超时,

可以用trie树,空间换取时间

注:这题树的品种名除了包括大写字母,小写字母和空格外,还有其他字符,

所以要注意trie树的子结点的个数

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
typedef struct stu
{
    struct stu *next[240]; //注意next数组的个数
    int n;
}node;
struct word
{
    char s[35];
    double r;
}w[10010];
int n,m;
int cmp(struct word s1,struct word s2) //按字典序排序
{
    return strcmp(s1.s,s2.s)<0?1:0;
}
node* creat_node()                //创建结点并初始化
{
    node *p=(node *)malloc(sizeof(node));
    p->n=0;
    memset(p->next,0,sizeof(p->next));
    return p;
}
void trie_insert(node *p,char *s)  //trie的插入
{
    int i;
    char *t=s;
    while(*s!='\0'){
        i=*s;
        if(p->next[i]==0)
            p->next[i]=creat_node();
        p=p->next[i];
        s++;
    }
    if(p->n==0){
        strcpy(w[n].s,t);
        n++;
    }
    p->n++;
}
void trie_search(node *p,char *s,int j)  //查找
{
    int i;
    while(*s!='\0'){
        i=*s;
        p=p->next[i];
        s++;
    }
    w[j].r=(p->n)*100./m;  //算百分比
}
int main()
{
    node *root=NULL;
    int i=0;
    char s[35];
    root=creat_node();
    n=m=0;
    while(gets(s)!=NULL){
        m++;
        trie_insert(root,s);
    }
    sort(w,w+n,cmp);
    for(i=0;i<n;i++){
        trie_search(root,w[i].s,i);
        printf("%s %.4lf\n",w[i].s,w[i].r);
    }
    return 0;
}
时间: 2024-09-30 14:18:00

poj 2418 Hardwood Species (trie 树)的相关文章

poj 2418 Hardwood Species (trie树)

poj   2418   Hardwood Species http://poj.org/problem?id=2418 trie树+dfs 题意: 给你多个单词,问每个单词出现的频率. 方法:通过字典树,将所有单词放入树中,通过dfs遍历(题目要求按ASSIC码顺序输出单词及其频率),dfs可满足 注意:单词中不一定只出现26个英文字母,ASSIC码表共有256个字符 1 #include <stdio.h> 2 #include <string.h> 3 #include &l

[ACM] POJ 2418 Hardwood Species (Trie树或map)

Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17986   Accepted: 7138 Description Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter. Ameri

[ACM] POJ 2418 Hardwood Species (Trie树或者map)

Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17986   Accepted: 7138 Description Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter. Ameri

POJ 2418 Hardwood Species Trie解法

计算一个字符串数组中有多少个重复字符串出现. 如果直接使用map容器,那么这条题就很简单了,一下就AC了,因为map已经处理好一切了: 不过时间超过1532ms,有点慢. 如下: int main() { map<string, int> msi; int total = 0; char treeName[40]; while (gets(treeName)) { msi[treeName]++; total++; } for (map<string, int>::iterator

POJ 2418 Hardwood Species(字典树)

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. Ameri

POJ 2418 Hardwood Species(字典树)

题目链接:POJ 2418 Hardwood Species [题意]给出一大串树的名字,可能有重复,然后按字典序输出名字和百分比. [思路]我已开始偷懒用了map来做,这道题给的时间是10s,用map的8s也还是水过了,真是神奇啊,后来还是写了一下字典树,700ms+就过了,效率提升显著啊.这里要注意的是建字典树的ChildSize是256,题目输入不只有字母,还有一些其它字符. 下面贴上代码,先是map的: 1 /* 2 ** POJ 2418 Hardwood Species 3 ** C

[字典树] poj 2418 Hardwood Species

题目链接: http://poj.org/problem?id=2418 Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17511   Accepted: 6949 Description Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and gene

POJ训练计划2418_Hardwood Species(Trie树)

解题报告 Tire树. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; struct node { int v; node *next[256]; }; int cnt=0,q; char ch[100],str1[100]; node *newnode() { node *p=new node; p->

POJ 2418 Hardwood Species(STL中map的应用)

题目地址:POJ 2418 通过这个题查了大量资料..知道了很多以前不知道的东西.... 在代码中注释说明吧. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue&