*HDU1800字典树
Flying to the Mars
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18240 Accepted Submission(s): 5877

In
the year 8888, the Earth is ruled by the PPF Empire . As the
population growing , PPF needs to find more land for the newborns .
Finally , PPF decides to attack Kscinow who ruling the Mars . Here the
problem comes! How can the soldiers reach the Mars ? PPF convokes his
soldiers and asks for their suggestions . “Rush … ” one soldier answers.
“Shut up ! Do I have to remind you that there isn’t any road to the
Mars from here!” PPF replies. “Fly !” another answers. PPF smiles
:“Clever guy ! Although we haven’t got wings , I can buy some magic
broomsticks from HARRY POTTER to help you .” Now , it’s time to learn to
fly on a broomstick ! we assume that one soldier has one level number
indicating his degree. The soldier who has a higher level could teach
the lower , that is to say the former’s level > the latter’s . But
the lower can’t teach the higher. One soldier can have only one teacher
at most , certainly , having no teacher is also legal. Similarly one
soldier can have only one student at most while having no student is
also possible. Teacher can teach his student on the same broomstick
.Certainly , all the soldier must have practiced on the broomstick
before they fly to the Mars! Magic broomstick is expensive !So , can
you help PPF to calculate the minimum number of the broomstick needed .
For example :
There are 5 soldiers (A B C D E)with level numbers : 2 4 5 6 4;
One method :
C could teach B; B could teach A; So , A B C are eligible to study on the same broomstick.
D could teach E;So D E are eligible to study on the same broomstick;
Using this method , we need 2 broomsticks.
Another method:
D could teach A; So A D are eligible to study on the same broomstick.
C could teach B; So B C are eligible to study on the same broomstick.
E with no teacher or student are eligible to study on one broomstick.
Using the method ,we need 3 broomsticks.
……
After checking up all possible method, we found that 2 is the minimum number of broomsticks needed.
In a test case,the first line contains a single positive number N indicating the number of soldiers.(0<=N<=3000)
Next
N lines :There is only one nonnegative integer on each line ,
indicating the level number for each soldier.( less than 30 digits);
//本质就是找最多有多少相同的数字。处理前导零,还有这个数就是0的情况;又糊涂了,竟然把每个经过的点都加了一,只加最后一个才能标记一个数啊!!!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<malloc.h>
using namespace std;
struct Trie
{
int v;
Trie *next[];
};
Trie *root;
void init()
{
root=new Trie;
for(int i=;i<;i++)
root->next[i]=NULL;
root->v=;
}
int Tinsert(char s[])
{
int len=strlen(s),j=;
Trie *p=root,*q;
for(int i=;i<len;i++)
{
if(s[i]!=''||i==len-)
{
j=i;
break;
}
}
for(int i=j;i<len;i++)
{
int id=s[i]-'';
if(p->next[id]==NULL)
{
q=new Trie;
q->v=;
for(int i=;i<;i++)
q->next[i]=NULL;
p->next[id]=q;
}
p=p->next[id];
}
p->v++;
return p->v;
}
void Tdelete(Trie *t)
{
if(t==NULL)
return;
for(int i=;i<;i++)
if(t->next[i]!=NULL)
Tdelete(t->next[i]);
free(t);
}
int main()
{
int n;
char ch[];
while(scanf("%d",&n)!=EOF)
{
init();
int ans=;
for(int i=;i<n;i++)
{
scanf("%s",ch);
int tem=Tinsert(ch);
if(tem>ans) ans=tem;
}
printf("%d\n",ans);
Tdelete(root);
}
return ;
}
*HDU1800字典树的更多相关文章
- HDU1800 字典树写法
题意:高级魔法师可以教低级魔法师 魔法扫把技能,同时教会了的低级魔法师又可以教比他更低级是,是传递的关系 同时如果教会了的话,他们可以同时坐一个扫把 问最少需要多少个扫把 思路:就是判断相同的数字最多 ...
- 【字符串算法】字典树(Trie树)
什么是字典树 基本概念 字典树,又称为单词查找树或Tire树,是一种树形结构,它是一种哈希树的变种,用于存储字符串及其相关信息. 基本性质 1.根节点不包含字符,除根节点外的每一个子节点都包含一个字符 ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树+博弈 CF 455B A Lot of Games(接龙游戏)
题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
- 山东第一届省赛1001 Phone Number(字典树)
Phone Number Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 We know that if a phone numb ...
- 字典树 - A Poet Computer
The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...
- trie字典树详解及应用
原文链接 http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用 一.知识简介 ...
随机推荐
- Ulink2 "No Ulink Device found" 解决办法
一.背景 keil使用ULINK2调试的时候,提示 "No Ulink Device found", "error: flash download failed - Ta ...
- iptables4张表5条链
4张表:filter nat mangle raw filter:协议过滤: nat:地址转换,端口映射等: mangle:协议修改 TTL等: raw:This table is used m ...
- centos 安装 rabbitMQ
此类文章一大堆,本文主要站在开发角度保证基本rabbitmq的基本访问. 系统:centos6 64bit 官方指引:https://www.rabbitmq.com/install-rpm.html ...
- PHP 变量声明的意义
有人说,PHP是没有类型的语言,个人比较倾向于,PHP是弱类型的计算机脚本语言的说法. 计算机存储都是二进制的数据,要么是0,要么是1. 在抽象数据的过程中,是要有各种各样的标志位来识别数据. 虽然, ...
- 网页设计之jQuery
1.在html中引入css和jQuery <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- 设计模式--单例模式Singleton(创建型)
单例模式很显然是定义一个类,这个类在程序中只有唯一的实例对象.一般单例类的构造函数是私有的,只能通过调用静态函数GetInstance来获取实例. 一.单例模式有三种:懒汉式单例.饿汉式单例.登记式单 ...
- 昨天开发引入的两个错误--Parcelable
以下的代码有两处明显的错误,一不小心就会写出如下错误的代码,其中第2个问题调试起来很麻烦: 1 public class AddedBannerInfo implements Parcelable { ...
- Modbus总线CRC16效验算法C语言
unsigned short CRC16_Modbus ( unsigned char *pdata, int len){ unsigned short crc=0xFFFF; int i, j; f ...
- 模拟搭建Web项目的真实运行环境(三)
一.解决Redis出现的RDB权限问题 当你在安装redis的时候,如果是使用超级用户root安装, 开启redis服务的时候没有用超级用户去开启, 在用客户端登录redis,然后使用shutdown ...
- C语言笔记一
学习C语言已经有一段时间,然而发现越学不知道的东西越多,这是在印象笔记中记得一些东西,现在再回顾一遍顺便补充一些新东西. 一,基础知识 运算符号 优先级 单目>算术>关系 从高到低 ...