POJ 2418 Hardwood Species (哈希,%f 和 %lf)

  我的错因: 本来改用%f输出,我用了%lf,结果编译器直接判定为错误(一部分编译器认为lf是没有错的)。当时我还以为是hash出错了。。

  方法不止一种:

  方法            时间        空间
  Hash           891ms      596k
  map<string,int>     2735ms      1316k
  sort            5000ms+    30000k+

  %lf 与 %f的具体区别:

  printf的%f说明符的确既可以输出float型又可以输出double型。根据“默认参数提升”规则float型会被提升为double型。因此printf()只会看到双精度数。对于scanf,情况就完全不同了,它接受指针,这里没有类似的类型提升。向float存储和向double存储大不一样,因此,scanf区别%f和%lf。

  也就是说输出的时候不管输出的是双精度还是单精度都用%f就没错了,但是输入的时候,输入单精度要用%f而输入双精度要用%lf。

  哈希

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<sstream>
using namespace std;
struct Tree
{
    char name[33];
    int sum;
} tree[10005];
int SDBMHash(char *str)
{
    int Hash = 0;
    while(*str)
    {
        Hash = (*str++) + (Hash << 6) + (Hash << 16) - Hash;
    }
    return (Hash&0x7FFFFFFF);
}
bool cmp(Tree a,Tree b)
{
    return strcmp(a.name,b.name)<0;
}
map<int,int> pos;
int main()
{
//    freopen("H.in.cpp","r",stdin);
    char tmp[33];
    pos.clear();
    int tot = 0,all = 0;
    while(gets(tmp))
    {
        if(strcmp(tmp,"") == 0) break;
        all++;
        int H = SDBMHash(tmp);
        int id = pos[H];
        if(id == 0)
        {
            strcpy(tree[++tot].name,tmp);
            tree[tot].sum = 1;
            pos[H] = tot;
        }
        else
        {
            tree[id].sum++;
        }
    }
    sort(tree+1,tree+1+tot,cmp);
    for(int i = 1; i <= tot; i++)
    {
        printf("%s %.4f\n",tree[i].name,tree[i].sum*100.0/all);
    }
    return 0;
}

  map<string,int>

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<sstream>
using namespace std;
struct Tree
{
    string name;
    int sum;
} tree[10005];
bool cmp(Tree a,Tree b)
{
    return a.name < b.name;
}
map<string,int> vis;
int main()
{
//    freopen("H.in.cpp","r",stdin);
    char a[33];
    string tmp;
    vis.clear();
    int tot = 0,all = 0;
    while(gets(a))
    {
        all++;
        int len = strlen(a);
        tmp = "";
        for(int i = 0; i < len; i++)
        {
            tmp += a[i];
        }
        int id = vis[tmp];
        if(id == 0)
        {
            tree[++tot].name = tmp;
            tree[tot].sum = 1;
            vis[tmp] = tot;
        }
        else
        {
            tree[id].sum++;
        }
    }
    sort(tree+1,tree+tot+1,cmp);
    for(int i = 1; i <= tot; i++)
    {
        cout<<tree[i].name<<" ";
        printf("%.4f\n",tree[i].sum*100.0/all);
    }
    return 0;
}

  排序

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<sstream>
using namespace std;
struct Tree
{
    char name[33];
} tree[1000005];
bool cmp(Tree a,Tree b)
{
    return strcmp(a.name,b.name)<0;
}
int main()
{
//    freopen("H.in.cpp","r",stdin);
    int all = 0;
    while(gets(tree[all].name) && strlen(tree[all].name))
    {
        all++;
    }
    sort(tree,tree+all,cmp);
    int tot = 1;
    for(int i = 0; i < all; i++)
    {
        if(strcmp(tree[i].name,tree[i+1].name) == 0) tot++;
        else
        {
            printf("%s %.4f\n",tree[i].name,tot*100.0/all);
            tot = 1;
        }
    }
    return 0;
}

时间: 2025-01-20 07:13:06

POJ 2418 Hardwood Species (哈希,%f 和 %lf)的相关文章

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

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

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

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解法

计算一个字符串数组中有多少个重复字符串出现. 如果直接使用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: 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

[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