hdu---(1800)Flying to the Mars(trie树)
Flying to the Mars
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11228 Accepted Submission(s): 3619
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);
10
20
30
04
5
2
3
4
3
4
2
//#define LOCAL
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
using namespace std;
map<int,int>aa;
int main()
{
int n,i,maxc,b;
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
while(scanf("%d",&n)!=EOF){
aa.clear();
maxc=;
for(i=;i<=n;i++){
scanf("%d",&b);
aa[b]++;
if(maxc<aa[b])maxc=aa[b];
}
printf("%d\n",maxc);
}
return ;
}
不过,还是为了练一下手,就是用比较简单的trie树吧!
代码:218ms
//#define LOCAL
#include<cstdio>
#include<cstring>
#include<cstdlib>
typedef struct node
{
struct node *child[];
int id;
}Trie;
int max(int a,int b)
{
return a>b?a:b;
}
int Insert(char *s,Trie *root)
{
Trie *cur=root,*curnew;
int i,pos;
for(i=;s[i]!='\0';i++)
{
pos=s[i]-'';
if(cur->child[pos]==NULL)
{
curnew = new Trie;
curnew->id=;
for(int j=;j<;j++)
curnew->child[j]=NULL;
cur->child[pos]=curnew;
}
cur=cur->child[pos];
}
cur->id++;
return cur->id;
}
void del(Trie *root)
{
Trie *cur=root;
for(int i=;i<;i++)
if(cur->child[i]!=NULL)
del(cur->child[i]);
delete cur;
}
char bb[];
int main()
{
int n,ans,i;
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
Trie *root;
while(scanf("%d",&n)!=EOF)
{
root=new Trie;
for(i=;i<;i++)
root->child[i]=NULL;
ans=;
while(n--){
scanf("%s",bb);
i=;
while(bb[i]==''&&bb[i+]!='\0')i++;
ans=max(ans,Insert(bb+i,root));
}
printf("%d\n",ans);
del(root);
}
return ;
}
注意几组数据:
4 004 04 0012 000
3 00 0 000
hdu---(1800)Flying to the Mars(trie树)的更多相关文章
- HDU 1800 Flying to the Mars Trie或者hash
http://acm.hdu.edu.cn/showproblem.php?pid=1800 题目大意: 又是废话连篇 给你一些由数字组成的字符串,判断去掉前导0后那个字符串出现频率最高. 一开始敲h ...
- HDU 1800 Flying to the Mars 字典树,STL中的map ,哈希树
http://acm.hdu.edu.cn/showproblem.php?pid=1800 字典树 #include<iostream> #include<string.h> ...
- hdu 1800 Flying to the Mars
Flying to the Mars 题意:找出题给的最少的递增序列(严格递增)的个数,其中序列中每个数字不多于30位:序列长度不长于3000: input: 4 (n) 10 20 30 04 ou ...
- HDU 1800——Flying to the Mars——————【字符串哈希】
Flying to the Mars Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- --hdu 1800 Flying to the Mars(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Ac code: #include<stdio.h> #include<std ...
- HDU - 1800 Flying to the Mars 【贪心】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1800 题意 给出N个人的 level 然后 高的level 的 人 是可以携带 比他低level 的人 ...
- hdu 1800 Flying to the Mars(简单模拟,string,字符串)
题目 又来了string的基本用法 //less than 30 digits //等级长度甚至是超过了int64,所以要用字符串来模拟,然后注意去掉前导零 //最多重复的个数就是答案 //关于str ...
- 杭电 1800 Flying to the Mars(贪心)
http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/Oth ...
- HDU 5269 ZYB loves Xor I Trie树
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5269 bc:http://bestcoder.hdu.edu.cn/contests/con ...
随机推荐
- V-rep学习笔记:转动关节1
V-REP(Virtual Robot Experimentation Platform),是全球领先的机器人及模拟自动化软件平台.V-REP让使用者可以模拟整个机器人系统或其子系统(如感测器或机械结 ...
- [SAP ABAP开发技术总结]字符串处理函数、正则表达式
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- Android 测试工具
有时候会发现给手机烧入的信息里少了某一些文件,比如一个图标,或者一个mp3文件之类的等等,为此做了一个小工具检查指定手机里面是否包含相应的文件. 通过程序执行手机的命令来操作手机,感觉还挺有意思的. ...
- 把多个JavaScript函数绑定到onload事件处理函数上
为了让函数只在页面加载完毕后才得到执行,我们会把函数绑定到onload事件上: window.onload = userFunction 但如果有两个函数:firstFunction() 和 seco ...
- 作用域内优先级及this指针
函数声明>变量声明 作用域:顶部----------------------> 函数声明会覆盖变量声明,但不会覆盖变量赋值 this指针不是变量.当调用括号的左边不是引用类型而是其它类型, ...
- 网页自适应@media
@media (min-width: 768px){ }/*屏幕最小为768px时调用括号里的属性*/ @media (max-width: 767px) {} /*屏幕最大为768px时调用括号里的 ...
- BeanUtils框架的简单运用
Sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景开发了一套简单.易用的API操作Bean的属性——BeanUtils Beanutils工具包的常用类: •BeanUt ...
- iOS - OC NSArray 数组
前言 @interface NSArray<__covariant ObjectType> : NSObject <NSCopying, NSMutableCopying, NSSe ...
- Linux_服务
1.服务启动顺序 http://bbs.chinaunix.net/thread-1970916-1-1.html http://bbs.csdn.net/topics/240060477 2.Lin ...
- [转载] TLS协议分析 与 现代加密通信协议设计
https://blog.helong.info/blog/2015/09/06/tls-protocol-analysis-and-crypto-protocol-design/?from=time ...