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 ...
随机推荐
- FZU 2141 Sub-Bipartite Graph
Sub-Bipartite Graph Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- MSChart使用
制作报表的时候结果出现画红线处的信息太散, 如果没必要全部显示出来,我们可以使用这种效果: 注意和前面的区分,这个功能叫做Collect Pie Slices(收集分区) 要实现此功能,应先了解相关信 ...
- jquery append()详解
1 http://www.365mini.com/page/jquery-append.htm 2 http://blog.csdn.net/chaiyining007/article/details ...
- JAVAWEB安全开发
晚上在看司马的博客,看到一篇关于JAVA安全的,基础的,蛮不错的,给大家分享下 文章来源是司马的博客:http://www.nxadmin.com/web/1332.html ============ ...
- 最大后验估计 -- Maximum-a-Posteriori (MAP) Estimation
最大后验估计是根据经验数据获得对难以观察的量的点估计.与最大似然估计类似,但是最大的不同时,最大后验估计的融入了要估计量的先验分布在其中.故最大后验估计可以看做规则化的最大似然估计.
- python_way day12 sqlalchemy,原生mysql命令
python_way day12 sqlalchemy,mysql原生命令 1.sqlalchemy 2.mysql 原生命令 一,sqlalchemy SQLAlchemy本身无法操作数据库,其必 ...
- Eclipse小技巧--快速输入System.out.println();(转)
步骤1:指定“Content Assist”的快捷键,参考:善用 Eclipse 组合键,提高输入效率 步骤2:输入源代码是,先输入sysout,然后输入辅助快捷键:Alt+/,这样就可以自动生成:S ...
- poj2420A Star not a Tree?(模拟退火)
链接 求某一点到其它点距离和最小,求这个和,这个点 为费马点. 做法:模拟退火 #include <iostream> #include<cstdio> #include< ...
- JavaWeb学习总结(九)--JDBC入门
一.什么是JDBC JDBC(Java DataBase Connectivity)就是Java数据库连接,说白了就是用Java语言来操作数据库.原来我们操作数据库是在控制台使用SQL语句来操作数据库 ...
- linux内核的熵池
也可以看百度科 Linux内核采用熵来描述数据的随机性.熵(entropy)是描述系统混乱无序程度的物理量,一个系统的熵越大则说明该系统的有序性越差,即不确定性越大.在信息学中,熵被用来表征一个符号或 ...