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 ...
随机推荐
- SQL生成随机数
ALTER FUNCTION [dbo].[ufn_Random] ( @A INT, @B INT ) RETURNS INT AS BEGIN /* 功能:生成随机数 */ ,) , SELECT ...
- wamp出现could not execute run action问题
wamp出现could not execute run action问题 原文地址:http://blog.sina.com.cn/s/blog_4a60ba9c0100zzlr.html上午 ...
- sar 找出系统瓶颈的利器
sar 找出系统瓶颈的利器sar是System Activity Reporter(系统活动情况报告)的缩写.sar工具将对系统当前的状态进行取样,然后通过计算数据和比例来表达系统的当前运行状态.它的 ...
- js判断ie11浏览器
var isIE11 = (/Trident\/7\./).test(navigator.userAgent);
- 异步上传图片,光用jquery不行,得用jquery.form.js插件
异步上传图片,光用jquery不行,得用jquery.form.js插件,百度一下下载这个插件,加jquery,引入就可以了 <form id="postbackground" ...
- JMS【四】--Spring和ActiveMQ整合的完整实例
第一篇博文JMS[一]--JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点. 第二篇博文JMS[二 ...
- Spring XML配置实现AOP
1: 首先我们要定义 配置成切面的类 package cn.gbx.example; import org.aspectj.lang.ProceedingJoinPoint; import org. ...
- 【T-SQL系列】新的排序函数
如:ROW_NUMBER.RANK.DENSE_RANK三个分析函数都是按照col1分组内从1开始排序 ROW_NUMBER() 是没有重复值的排序(即使两天记录相等也是不重复的),可以利用它来实现分 ...
- 阿里云大数据三次技术突围:Greenplum、Hadoop和“飞天”
阿里云大数据三次技术突围:Greenplum.Hadoop和"飞天" 对于企业来说,到底什么是云计算?相信很多企业都有这样的困惑,让我们一起回到这个原始的起点探讨究竟什么是云 ...
- mysql概要(七)表字段管理,字段的增删改
1.添加列 放在某列之后 放第一列: 2.修改(声明和名字),删除列 2.1修改声明 2.2删除列