hdu1800Flying to the Mars (字典树)

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 .
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);
4
10
20
30
04
5
2
3
4
3
4
1
2解题:仅仅要求出数字出现最多的次数。 当n=0时。输出1.#include<stdio.h>
#include<malloc.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
typedef struct nn
{
int num,flag;
struct nn *next[10];
}node;
node *builde()
{
node *p=(node*)malloc(sizeof(node));
p->num=0; p->flag=0;
for(int i=0;i<10;i++)
p->next[i]=NULL;
return p;
}
node *root;
int insert(char ans[])
{
node *p=root;
int i=0;
while(ans[i]=='0')i++;
while(ans[i]!='\0')
{
if(p->next[ans[i]-'0']==NULL)
p->next[ans[i]-'0']=builde();
p=p->next[ans[i]-'0'];
i++;
}
p->flag=1; p->num++;
return p->num;
}
int main()
{
int n,max;
char ans[35];
while(scanf("%d",&n)>0)
{
max=0;
root=builde();
while(n--)
{
scanf("%s",ans);
int t=insert(ans);
if(t>max)max=t;
}
if(max==0)max=1;
printf("%d\n",max);
}
}
hdu1800Flying to the Mars (字典树)的更多相关文章
- HDU 1800 Flying to the Mars 字典树,STL中的map ,哈希树
http://acm.hdu.edu.cn/showproblem.php?pid=1800 字典树 #include<iostream> #include<string.h> ...
- hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- hdu 1075(字典树)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- Tire树(字典树)
from:https://www.cnblogs.com/justinh/p/7716421.html Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,P ...
- J - What Are You Talking About(map,字典树)
题意:上部分是单词表,下部分是句子,翻译句子.START开始,END结束. 思路:简单字典树. Ignatius is so lucky that he met a Martian yesterday ...
- 萌新笔记——用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字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
随机推荐
- (2)go 规范,变量,基本数据类型
.一.规范 main 函数为入口 严格区分大小写 不需要加分号 一行只写一条语句 定义的变量或者导入的包必须要用到,否则编译时会报错 左括号不能单独一行 逗号可以用来在换行时连接字符串 标识符由字母数 ...
- HRBUST 1200 装修
$dp$. $dp[i]$记录铺满$3*i$的方案数.最后两列铺了$1$个$2*2$的和两个$1*1$的,或者是最后$1$列铺了$3$个$1*1$的. 因此$dp[i]=dp[i-1]+dp[i-2] ...
- JavaScript中思考do...while 和 while语句的区别
Do...while和while...do的区别在于对于临界值的测试上.当在执行的时候只选择临界值来测试时,你会发现do...while至少要执行一次,而while...do则一次都不会执行.但是,当 ...
- 【BZOJ 4103】【THUSC 2015】异或运算
http://www.lydsy.com/JudgeOnline/problem.php?id=4103 对长的那一维建可持久化trie树(主席树?) 最主要的思路是对短的那一维每一位暴力,每一位都记 ...
- 【扩展欧几里得】Codeforces Round #406 (Div. 2) A. The Monster
扩欧,a+bx=c+dx,输出x>=0且y>=0,且a+bx最小的解. 要注意不能只保证x非负,还得看看能否保证y也非负. #include<cstdio> #include& ...
- Create process in UNIX like system
In UNIX, as we’ve seen, each process is identified by its process identifier, which is a unique inte ...
- javacripr基础总结
js中一切皆为对象 方法=函数 其实也是内建对象 函数都可以用,包括数组,函数 都可以获取 核心dom编程 -- dom d document文档 o 对象 1用户自定义对象, 2内建对象 如Arr ...
- selector简介
最近在学习java NIO,发现java nio selector 相对 channel ,buffer 这两个概念是比较难理解的 ,把学习理解的东西以文字的东西记录下来,就像从内存落地到硬盘,把内存 ...
- Android Studio导入第三方库的三种方法
叨叨在前 今天在项目中使用一个图片选择器的第三方框架——GalleryFinal,想要导入源码,以便于修改,于是上完查找了一下方法,想到之前用到过其他导入第三方库的方法,现在做个小总结,以防忘记. A ...
- jQuery:validate设置样式
jquery.validate.js插件里面的样式设置: ... errorClass: "error",validClass: "valid", ... &l ...