zoukankan      html  css  js  c++  java
  • poj 2418 Hardwood Species

    题目意思:给你若干棵树,问你每种树出现的概率;

    解题思路:(1)trie树 ,不过这题的输出很让人纠结,它是按ACSII表的顺序输出的,本以为大小写字母再加上一个空格,开53个数组就够了,但是不幸的WA了, 后来看了别人的解题报告,直接开到了95,改了一下就过了~

    (2)stl的map容器。不过这个很慢,可以过,但是没法很trie树的比,一个700+ms,一个3000+ms。呃,第一次用map,权当练练手。

    trie树:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <math.h>
    #include <queue>
    #define  maxx  95
    using namespace std ;
    
    struct node
    {
        int num ;
        struct node *next[maxx] ;
    }*s ;
    int sum ;
    char str[35] ;
    
    struct node *creat ( )
    {
        struct node *p ;
        p = ( struct node *) malloc ( sizeof ( struct node ));
        for ( int i = 0 ; i < maxx ; i++ )
        p->next[i] = NULL ;
        p->num = 0 ;
        return p ;
    }
    
    void insert ( char str[] )//建树
    {
        int i , x ;
        struct node *p , *q ;
        p = s ;
        for ( i = 0 ; str[i] != '\0' ; i++ )
        {
            x = str[i] - ' ' ;
            if ( p->next[x] == NULL )
            {
                q = creat( ) ;
                p->next[x] = q ;
                p = q;
            }
            else
            p = p->next[x] ;
        }
        p->num++;
    }
    
    void find ( struct node *p , int step )//查找
    {
        for ( int i = 0 ; i < maxx ; i++ )
        {
            if ( p->next[i] != NULL && p->num == 0)
            {
                str[step] = i + ' ' ;//将查找到的字符存起来
                find ( p->next[i] , step + 1 );
            }
            else if ( p->num != 0 )//如果p->num 不为0的话输出
            {
                str[step] = '\0' ;
                printf ( "%s %.4lf\n" , str , ( double )p->num / sum * 100 );
                return ;
            }
        }
    }
    
    int main()
    {
        s = creat( );
        for ( sum = 0 ; gets( str ) ; sum++ )
        {
            insert ( str );
        }
        find ( s , 0 );
        return 0 ;
    }

    map容器的:

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <map>
    #include <string>
    using namespace std ;
    
    int main()
    {
        string str ;
        map<string , double>mm;
        map<string , double>::iterator s ;
        int sum = 0 ;
        while ( getline ( cin , str ))
        {
            mm[str]++;
            sum++ ;
        }
        for ( s = mm.begin() ; s != mm.end() ; s++ )
        {
            cout<<s->first;
            printf ( " %.4lf\n" , s->second * 100 / sum );
        }
        return 0 ;
    }
  • 相关阅读:
    Python——字符串、文件操作,英文词频统计预处理
    了解大数据的特点,来源与数据的呈现方式
    hadoop综合大作业
    分布式并行计算MapReduce
    ·分布式文件系统HDFS 练习
    安装关系型数据库MySQL 安装大数据处理框架Hadoop
    爬虫综合大作业
    爬取全部的校园新闻
    获取一篇新闻的全部信息
    理解爬虫原理
  • 原文地址:https://www.cnblogs.com/misty1/p/2528855.html
Copyright © 2011-2022 走看看