POJ 2418 Hardwood Species(trie的串排序运用)

题意:输入众多字符串(中间有空格),按字典序输出,且输出每个字符串所占整个字符串数量的百分比

思路:用字典树的先序遍历,遍历到字符串的末尾便输出并算出百分比即可

这题同样用C++stl map 可以很好解决,但毕竟题目是字典序,比如逆序就字典树同样可以解决

//1632K	782MS
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
struct trie{
    int cnt;
    char str[35];
    trie *child[127];
    trie(){
        cnt=0;
        for(int i=0;i<127;i++)
            child[i]=NULL;
    }
};
trie *root=new trie;
trie *cur,*newnode;
int num;
void Insert(char *str){
    cur=root;
    int len=strlen(str);
    for(int i=0;i<len;i++){
        int m=str[i];
        if(cur->child[m])
            cur=cur->child[m];
        else {
            newnode=new trie;
            cur->child[m]=newnode;
            cur=newnode;
        }
    }
    cur->cnt++;
    strcpy(cur->str,str);
}
void dfs(trie *rt){ /先序遍历
    if(rt->cnt){
        printf("%s %.4f\n",rt->str,100.0*rt->cnt/(double)num);
    }
    for(int i=1;i<=126;i++)
        if(rt->child[i]!=NULL)
        dfs(rt->child[i]);
}
int main(){
    char tmp[35];
    while(gets(tmp)!=NULL){
        num++;
        Insert(tmp);
    }
    dfs(root);
    return 0;
}

map:

//408K	3032MS
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
map<string ,int > all;
int cnt;
int main(){
   // freopen("1.in","r",stdin);

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

时间大增...

然后我用gets读入避免用cin读入,在把字符数组复制成string试了一发,时间缩短一半:(果然cin酷炫)

//412K	1500MS
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
map<string ,int > all;
int cnt;
int main(){
    //freopen("1.in","r",stdin);
    char tmp[40];
    string s;
    while(gets(tmp)!=NULL){
        s=tmp;
        all[s]++;
        cnt++;
    }
    for(map<string,int>::iterator it=all.begin();it!=all.end();it++){
        cout<<it->first;
        printf(" %.4f\n",100.0*it->second/(double)cnt);
    }
    return 0;
}
时间: 2024-10-12 05:30:31

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

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

[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(字典树)

题目链接: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(STL中map的应用)

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

poj 2418 Hardwood Species (trie 树)

链接:poj 2418 题意:给定一些树的种类名,求每种树所占的百分比,并按字典序输出 分析:实质就是统计每种树的数量n,和所有树的数量m, 百分比就为 n*100./m 由于数据达到一百万,直接用数组查找肯定超时, 可以用trie树,空间换取时间 注:这题树的品种名除了包括大写字母,小写字母和空格外,还有其他字符, 所以要注意trie树的子结点的个数 #include<cstdio> #include<cstdlib> #include<cstring> #inclu

poj 2418 Hardwood Species(二叉排序树)

Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 19428 Accepted: 7658 http://poj.org/problem?id=2418 Description Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go d

POJ 2418 Hardwood Species(字典树 || map运用)

题目链接:http://poj.org/problem?id=2418 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