*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树详解及其应用 一.知识简介 ...
随机推荐
- Servlet session
一.session介绍 Session用于保存服务端与客户端"会话"的信息.例如你逛淘宝时添加到购物车中的商品的信息就是保存到Session中.与Cookies不同的是,S ...
- PAT mooc DataStructure 4-2 SetCollection
数据结构习题集-4-2 集合的运用 1.题目: We have a network of computers and a list of bi-directional connections. Eac ...
- 《CMake实践》笔记二:INSTALL/CMAKE_INSTALL_PREFIX
<CMake实践>笔记一:PROJECT/MESSAGE/ADD_EXECUTABLE <CMake实践>笔记二:INSTALL/CMAKE_INSTALL_PREFIX &l ...
- css特殊字符总结
< : < > : >
- Java四种线程池的使用
Java通过Executors提供四种线程池,分别为:newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程.newFixe ...
- ecshop商品详情相册顺序调整
更改相册顺序为根据id升序: 找到/include/lib_goods.php 821行: 修改为: $sql = 'SELECT img_id, img_url, thumb_url, img_de ...
- 虚拟机ping不通主机,但是主机可以ping通虚拟机
http://chris2013.blog.51cto.com/6931081/1209278
- java5
1:final关键字(掌握) (1)是最终的意思,可以修饰类,方法,变量. (2)特点: A:它修饰的类,不能被继承. B:它修饰的方法,不能被重写. C:它修饰的变量,是一个常量. (3)面试相关: ...
- rpm 与 yum 源
rpm rpm -e 删除软件包rpm -i 安装软件包rpm -U 更新软件包rpm -qa ...
- maven install 构建报错(2)
错误:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin: 2.3 . 2 :compile ( default ...