数据结构实验之查找三:树的种类统计

Time Limit: 400 ms Memory Limit: 65536 KiB

Problem Description

随着卫星成像技术的应用,自然资源研究机构可以识别每一个棵树的种类。请编写程序帮助研究人员统计每种树的数量,计算每种树占总数的百分比。

Input

输入一组测试数据。数据的第1行给出一个正整数N (n <= 100000),N表示树的数量;随后N行,每行给出卫星观测到的一棵树的种类名称,树的名称是一个不超过20个字符的字符串,字符串由英文字母和空格组成,不区分大小写。

Output

按字典序输出各种树的种类名称和它占的百分比,中间以空格间隔,小数点后保留两位小数。

Sample Input

2

This is an Appletree

this is an appletree

Sample Output

this is an appletree 100.00%

题解:数据量比较大,需要用到字典树。

字典树跟排序树类似,根据字符串的字典序进行的排序

#include <stdio.h>
#include <stdlib.h>
#include <string.h> char s[25]; typedef struct node
{
char s[25];
int num;
struct node *l,*r;
}node; node *newNode()
{
node *t;
t = (node *)malloc(sizeof(node));
t->l = t->r = NULL;
return t;
} node * build(node *t)
{
if(t!=NULL)
{
if(strcmp(t->s,s)==0)
t->num ++;
else if(strcmp(t->s,s)<0)
t->r = build(t->r);
else if(strcmp(t->s,s)>0)
t->l = build(t->l);
return t;
}
t = newNode();
strcpy(t->s,s);
t->l = NULL;
t->r = NULL;
t->num = 1;
return t;
} void show(node *t,int n)
{
if(t)
{
show(t->l,n);
printf("%s ",t->s);
printf("%.2f%%\n",1.0*t->num/n*100);
show(t->r,n);
}
} void del(node *t)
{
if(t)
{
del(t->r);
del(t->l);
free(t);
}
} int main()
{
int n,i,j;
node *t;
scanf("%d",&n);
t = NULL;
getchar();
for(i=0;i<n;i++)
{
gets(s);
int len = strlen(s);
for(j=0;j<len;j++)
if(s[j]>='A'&&s[j]<='Z')
s[j] = s[j] - 'A' + 'a';
//cout<<s<<endl;
t = build(t);
//printf("%d\n",t->num);
}
show(t,n);
del(t);
return 0;
}

SDUT-3375_数据结构实验之查找三:树的种类统计的更多相关文章

  1. SDUT 3375 数据结构实验之查找三:树的种类统计

    数据结构实验之查找三:树的种类统计 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 随着卫星成 ...

  2. SDUT 3374 数据结构实验之查找二:平衡二叉树

    数据结构实验之查找二:平衡二叉树 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 根据给定的输 ...

  3. SDUT 3311 数据结构实验之串三:KMP应用

    数据结构实验之串三:KMP应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 有n个小朋友 ...

  4. SDUT 3347 数据结构实验之数组三:快速转置

    数据结构实验之数组三:快速转置 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 转置运算是一 ...

  5. SDUT OJ 数据结构实验之二叉树三:统计叶子数

    数据结构实验之二叉树三:统计叶子数 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  6. SDUT OJ 数据结构实验之串三:KMP应用

    数据结构实验之串三:KMP应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  7. SDUT OJ 数据结构实验之排序三:bucket sort

    数据结构实验之排序三:bucket sort Time Limit: 250 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem D ...

  8. SDUT OJ 数据结构实验之链表三:链表的逆置

    数据结构实验之链表三:链表的逆置 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

  9. SDUT 3400 数据结构实验之排序三:bucket sort

    数据结构实验之排序三:bucket sort Time Limit: 150MS Memory Limit: 65536KB Submit Statistic Problem Description ...

随机推荐

  1. Spring MVC(一)--SpringMVC的初始化和流程

    SpringMVC是Spring提供给WEB应用的MVC框架,MVC框架一般来说由三部分组成: Model:模型层,一般由java bean完成,主要是进行数据库操作: View:视图层,用于前端展示 ...

  2. PAT甲级——A1054 The Dominant Color

    Behind the scenes in the computer's memory, color is always talked about as a series of 24 bits of i ...

  3. PAT甲级——A1025 PAT Ranking

    Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhe ...

  4. IDEA的下载安装

    一. 下载 二. 安装 安装成功!!! 选择试用版

  5. [Day1] 初识Nginx

    一. Nginx的诞生 Nginx是一个高效的web及反向代理服务器,它的第一版发布于2012年,晚于如今占据最大市场份额的Apache.那么Nginx的诞生肩负了哪些使命呢?或者说它于Apache的 ...

  6. Python - 基本数据类型及其常用的方法之列表

    列表: 特点:用 [] 括起来,切元素用逗号分隔:列表内的元素可以为任何的数据类型. 列表的基本操作: 1.修改 li = [12, 5, 6, ["Aiden", [2, 4], ...

  7. Merge array and hash in ruby if key appears in array

    I have two arrays one = [1,2,3,4,5,6,7] and two = [{1=>'10'},{3=>'22'},{7=>'40'}] Two will ...

  8. Leetcode109. Convert Sorted List to Binary Search Tree有序链表转换二叉搜索树

    给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: [-10 ...

  9. Sql 竖表转横表

    ) set @sql='select t3.BID,t5.UnitName,Sort,UnitTypeSort' select @sql=@sql+' , max(case t4.id when '' ...

  10. web前端学习(四)JavaScript学习笔记部分(9)-- JavaScript面向对象详解

    1.认识面向对象 1.1.概念 1.一切事物皆是对象 2.对象具有封装和继承特性 3.信息隐藏(类的信息隐藏,包括属性和方法) <!DOCTYPE html> <html lang= ...