poj_2418 Hardwood Species (字典序Trie树)

Android7以上抓取https数据包-无需root 手机内装一个虚拟机,然后在虚拟机内运行我需要抓包的应用,该虚拟机内是不需要证书验证的。完美解决android7.0以上https无法抓包的问题。 阅读详情

又是一道典型的Trie树应用题,有了Trie树的基本知识,写起来就非常容易了~~

【题目描述】

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

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.

【样例输入】

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

【样例输出】

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node //儿子兄弟表示法
{
    char c;
    char s[50];
    long int num;
    struct node* fChild;
    struct node* rCousin;
}* triTree;

triTree newTree(char ch)
{
    triTree p=(triTree) malloc(sizeof(struct node));
    p->c=ch;
    p->num=0;
    p->fChild=NULL;
    p->rCousin=NULL;
    return p;
}

void setTree(triTree t, long int* total) //建立一棵字典序Trie树
{
    char s[50];
    while (gets(s)!=NULL) //需要用gets,因为树名中可能有空格
    {
        (*total)++; //注意*和++的优先级顺序
        int i,len=strlen(s);
        triTree x=t,y;
        for (i=0;i<len;i++)
        {
            y=x->fChild;
            if ((y==NULL)||((y!=NULL)&&(y->c > s[i]))) //当前结点没有长子或者长子字符大于待查字符时,剩余字符串按长子树插入
            {
                int k;
                triTree z=newTree(s[i]);

                x->fChild=z; x=z;
                for (k=i+1;k<len;k++){x->fChild=newTree(s[k]); x=x->fChild;}
                strcpy(x->s,s);
                x->num++;

                if (y!=NULL) z->rCousin=y;
                break;
            }

            while (y!=NULL)
            {
                if (y->c==s[i]) {x=y;break;}
                if (y->c<s[i]) x=y;
                y=y->rCousin;
            }
            if (y==NULL) //按非长子树插入,注意调整儿子关系
            {
                triTree z=newTree(s[i]);
                z->rCousin=x->rCousin;
                x->rCousin=z;
                x=z;

                int k;
                for (k=i+1;k<len;k++){x->fChild=newTree(s[k]);x=x->fChild;}
                strcpy(x->s,s);
                x->num++;
                break;
            }
        }
        if (i==len) x->num++; //重复字符串
    }
}

void print(triTree t, long int total) //先序遍历整个Trie树,字典序输出所有字符串统计
{
    if (t->num>0) printf("%s %.4f\n", t->s, 100.0*t->num/total);
    if (t->fChild!=NULL) print(t->fChild,total);
    if (t->rCousin!=NULL) print(t->rCousin,total);
}

int main()
{
    triTree t=newTree('0');
    long int total=0;
    setTree(t,&total);
    print(t,total);
    return 0;
}

第十五章数据管理成熟度评估【6’】(DAMA-CDGA 2022年以后历年模拟题真题汇总,基本包含所有考点。) 阅读详情

相关推荐

Polardb训练营-本地部署 PolarDB-X

PolarDB-X是阿里巴巴自主设计研发的高性能云原生分布式数据库产品,为用户提供高吞吐、大存储、低延时、易扩展和超高可用的云时代数据库服务。云原生+MySQL生态PolarDB-X已作为标准云产品在世界范围内的13个地区提供服务。依托云资源和容器化部署能力,PolarDB-X可以在数分钟内完成集群创建和变配,整个过程中用...

yangjunfeng0604的博客 2476

二叉树--字典树应用--Hardwood Species

传送门:Hardwood Species 总时间限制: 10000ms 内存限制: 65536kB 描述 Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter. ...

天行健,君子以自强不息 1187

实战分享:如何利用svchost进程绕过360防护(附完整Shellcode注入教程)

本文深入解析系统进程安全防护机制,探讨svchost等关键进程的安全特性与防护策略。详细分析安全软件的行为检测和内存保护技术,提供系统加固与监控实践建议,助力企业构建多层次防御体系,有效应对潜在安全威胁。

weixin_30752699的博客 102

fjnu 1601 Hardwood Species

DescriptionHardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter. Americas temperate climates produce forests with hund

异リ度ュ空ウ間(镜像站) 868

告别CANoe?手把手教你用Python+PCAN搭建汽车诊断脚本(附完整代码)

本文详细介绍了如何利用Python和PCAN硬件搭建高性价比的汽车诊断自动化方案,替代昂贵的CANoe工具。通过开源库python-can和udsoncan实现UDS诊断协议,涵盖硬件选择、环境配置、核心功能实现及性能优化,附完整代码示例,助力开发者快速构建诊断脚本。

weixin_42551227的博客 377

拆解英飞凌CoolGaN:如何用Silvaco TCAD仿真评估功率GaN HEMT的关键性能(BV、Qg、开关损耗)

本文详细解析了如何使用Silvaco TCAD仿真工具评估英飞凌CoolGaN功率GaN HEMT的关键性能指标,包括击穿电压(BV)、栅电荷(Qg)和开关损耗。通过精确建模极化效应、优化边缘终端结构以及高频开关损耗仿真技术,为功率器件工程师提供了一套完整的'设计-仿真-验证'方法论,助力GaN HEMT在快充、数据中心电源等领域的应用。

weixin_29159995的博客 430

POJ2418 Hardwood Species STL

水题 直接使用MAP即可 Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 14927   Accepted: 6003 Description Hardwoods are the botanical group of trees

684

极化码理论及算法研究4-巴氏参数、GA算法以及matlab仿真

目录 (如要找其他内容,欢迎访问我的主页寻找) #前言 #极化码概述 #极化码定义 #极化码编码 #极化码译码 #5G标准中极化码的编译码概述 #5G标准中极化码的编码 #5G标准中极化码的译码 #总结 极化码编码 ...

1万+

poj 2418 Hardwood Species 字典树

Hardwood Species 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 clima

colorfulshark 949

(MATLAB)一维信号峰值检测findpeaks

一维信号波峰波谷检测,峰值、峰宽、到达时间计算。MATLAB自带的findpeaks函数能完成这一任务,在这里分享其基本用法与特殊用法。

HPC_ZY 2万+

提示工程架构师揭秘:prompt优化中的“隐性成本”如何控制?

当我们谈论Prompt优化时,往往聚焦于“效果提升”——比如让模型生成更准确的回答、更符合要求的格式。优化的本质是“成本-收益”的平衡。那些看不见的“隐性成本”,比如反复调试的时间消耗、复杂Prompt带来的认知负荷、长文本导致的系统资源浪费、后续维护的无穷麻烦,往往会在长期运营中成为压垮效率的“稻草”。本文将从提示工程架构师的视角,揭秘Prompt优化中的“隐性成本”到底是什么、如何量化,以及通过哪些工程化手段将这些“看不见的消耗”转化为“可控制的效率”。

操作系统内核探秘的博客 701

Hardwood Species(字典树)

Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 24133   Accepted: 9364 Description Hardwoods are the botanical group of trees that have broad

jinzk123的博客 508

Windows上MSYS2的安装和使用

MSYS2这个工具我是越用越喜欢,很多东西放在Linux上如鱼得水但是放在Windows上就没那么好用,比如ffmpeg,opencv等直接引用就行了,这个工具真的很省事。接下来带你们看看它的妙用。1、好好研究确实妙用无穷。

jiexijihe945的博客 4146

POJ 2418 ,ZOJ 1899 Hardwood Species - from lanshui_Yang

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 hund

lanshui_Yang的专栏 1万+

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

POJ 2418 Hardwood Species (trie树\map)

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

bandiaodai5734的博客 177

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

poj 2418 Hardwood Species (trie 树)

题意:给定一些树的种类名,求每种树所占的百分比,并按字典序输出 分析:实质就是统计每种树的数量n,和所有树的数量m, 百分比就为 n*100./m 由于数据达到一百万,直接用数组查找肯定超时, 可以用trie树,空间换取时间

happy_lcj 907

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

POJ2418 Hardwood Species Tire树

题目大意:若干棵树构成一片树林,我们知道每一棵树的名字,按树名字典序输出每种一树所占的比率。 分析:由于树名不止是一个单词,可以用gets输入,输入的同时记录树的数量,字符串排序后遍历一遍即可。为了保证重复树名只输出一次,我们可以在每种树输出一次后标记它的状态,下次不再重复输出。 实现代码如下: #include #include #include #inclu

AC_Gibson的专栏 460

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

字典树 传送门 题目大意 给你很多个单词,要你统计单词出现的次数,最后按字典升序输出 思路 因为字典序的先序遍历是排序的,所以只需建好树后先序遍历一下树就可以满足题目要求的输出方式了。裸的Trie树,输出按字符串的字典序输出,所以插入的时候,把第一次出现的字符串暂时保存起来。以后排完序在查询输出即可 字典树代码 struct node{ int val; int child[130]; node(){ val=0; memset(child,0,sizeof child); } }tire[

幼儿园大哥~ 237

POJ 2418 Hardwood Species

这个题可以用多种方法解决。 题目大意: 就是输入一堆字符串,可能重复,按字典序输出,并输出占的比例。 可以用字典树,二叉查找树,Map,快排等等多种方法。 我用的二叉查找树,下面是代码: #include #include #include struct node { int cut; char c[40]; struct node *l,*r; } h

脑残的孩子只有慢慢学。。 4017

poj 2418 Hardwood Species

Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 19313   Accepted: 7609 Description Hardwoods are the botanical group of trees that have b

Jinbao 772

POJ 2418 Hardwood Species (字典树+dfs)

题意: 给若干个串,求每一个串出现的频率。 按字典序输出:串 频率*100 例如:Ash 13.7931 ps: 频率为该串出现的次数除以总串数 Sample Input Red Alder Ash Aspen Basswood Ash Beech Yellow Birch Ash Cherry Cottonwood Ash Cypress Red Elm Gum Hackberry White ...

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 514

poj-2418 Hardwood Species

题目数据很长就不放描述了。    题意:美国有各种各样不同的树木,你需要输入树木的名字,并且最后统计出每种树木所占的比例,并按字典序输出。 思路:字典树存储树的名字,统计每种树木的数量。除以输入数据的数量得到比例,最后输出即可,本题没有什么很难的算法,读懂题意就可以AC。 #include #include #include #include #include using

心の コード 890

[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 b

逆风的方向 更适合飞翔 2996

poj 2418 Hardwood Species

题目链接:点击打开链接 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(

Vision 525

poj-2418 Hardwood Species(用map处理字符串)

题目链接 poj2418 题意 题意:美国有各种各样不同的树木,你需要输入树木的名字,并且最后统计出每种树木所占的比例,并按字典序输出。 代码 #include<iostream> #include<map> #include<string> #include<iomanip> #include<cstdio> using namespace std; int main() { //freopen("data.txt","r",stdin);

weixin_47750287的博客 177

poj 2418Hardwood Species

题意是将输入的字符串按字典序输出,并且输出它出现的次数的百分比。 典型的字典树问题,建树并记录出现的次数就OK了啊、注意输入以Ctrl+Z退出、、 Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 15937   Accepted: 6381

I am a slow walker, but I never walk backwards! 2276
上一篇: 练习:Trie树(公共前缀)
下一篇: poj_2945 Find the Clones (Trie树 内存分配)
薄帷清风
博客等级 码龄11年 31粉丝 44原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值