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

Problem Description

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.

 
Input
Input file contains multiple test cases.
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);
 
Output
For each case, output the minimum number of broomsticks on a single line.
 
Sample Input
4
10
20
30
04
5
2
3
4
3
4
 
Sample Output
1
2
 
Author
PPF@JLU
 
由于数据疲弱,所以其实使用map就可以了,虽然这道题有人为的想卡掉STL做法,但是还存在漏洞。
代码:
 //#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树)的更多相关文章

  1. HDU 1800 Flying to the Mars Trie或者hash

    http://acm.hdu.edu.cn/showproblem.php?pid=1800 题目大意: 又是废话连篇 给你一些由数字组成的字符串,判断去掉前导0后那个字符串出现频率最高. 一开始敲h ...

  2. HDU 1800 Flying to the Mars 字典树,STL中的map ,哈希树

    http://acm.hdu.edu.cn/showproblem.php?pid=1800 字典树 #include<iostream> #include<string.h> ...

  3. hdu 1800 Flying to the Mars

    Flying to the Mars 题意:找出题给的最少的递增序列(严格递增)的个数,其中序列中每个数字不多于30位:序列长度不长于3000: input: 4 (n) 10 20 30 04 ou ...

  4. HDU 1800——Flying to the Mars——————【字符串哈希】

    Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. --hdu 1800 Flying to the Mars(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Ac code: #include<stdio.h> #include<std ...

  6. HDU - 1800 Flying to the Mars 【贪心】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1800 题意 给出N个人的 level 然后 高的level 的 人 是可以携带 比他低level 的人 ...

  7. hdu 1800 Flying to the Mars(简单模拟,string,字符串)

    题目 又来了string的基本用法 //less than 30 digits //等级长度甚至是超过了int64,所以要用字符串来模拟,然后注意去掉前导零 //最多重复的个数就是答案 //关于str ...

  8. 杭电 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 ...

  9. 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 ...

随机推荐

  1. 3DES 加解密

    using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Comm ...

  2. Debug不崩溃Release版本崩溃的一种原因

    今天有一个工程Debug是正常,Release崩溃,郁闷至极. 研究了一下下午,原因是一个类成员变量没有构造函数中初始化.而Debug版本正好没有问题. 所以定义类成员,一定不能忘记初始化!!

  3. 终端、shell、bash的区别联系

    最佳答案 终端,即所谓的命令行界面,又称命令终端,用户输入shell命令用的窗口,跟Windows里的DOS界面差不多. shell,Shell就是用户和操作系统之间的壳,中介,GUI和CLI都算是S ...

  4. linux设置tomcat开机自启动

    本文假设jdk环境安装成功,如何安装JDK请参考这个链接: http://www.cnblogs.com/yoyotl/p/5395208.html 1. 下载apache的安装包,例如本例下载了ap ...

  5. Java_类文件及加载机制

    类文件及类加载机制 标签(空格分隔): Java 本篇博客的重点是分析JVM是如何进行类的加载的,但同时我们会捎带着说一下Class类文件结构,以便对类加载机制有更深的理解. 类文件结构 平台无关性 ...

  6. C语言程序设计现代方法1,2,3章

    1:浮点型(float)运算比int慢,并且可能存在舍入误差 如float存储0.1,以后使用可能会变成0.099999999987 2:宏定义只用大写,这是大多数C程序猿遵循的规范! C语言区分大小 ...

  7. 常用的STL查找算法

    常用的STL查找算法 <effective STL>中有句忠告,尽量用算法替代手写循环:查找少不了循环遍历,在这里总结下常用的STL查找算法: 查找有三种,即点线面: 点就是查找目标为单个 ...

  8. Spark Streaming官方文档学习--上

    官方文档地址:http://spark.apache.org/docs/latest/streaming-programming-guide.html Spark Streaming是spark ap ...

  9. jquery 跳转到当前页面指定位置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. iOS - ASIHTTPRequest 网络请求

    前言 使用 iOS SDK 中的 HTTP 网络请求 API,相当的复杂,调用很繁琐,ASIHTTPRequest 就是一个对 CFNetwork API 进行了封装,并且使用起来非常简单的一套 AP ...