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
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做法,但是还存在漏洞。
代码:
1 //#define LOCAL
2 #include<cstdio>
3 #include<cstring>
4 #include<iostream>
5 #include<map>
6 using namespace std;
7 map<int,int>aa;
8 int main()
9 {
10 int n,i,maxc,b;
11 #ifdef LOCAL
12 freopen("test.in","r",stdin);
13 #endif
14 while(scanf("%d",&n)!=EOF){
15 aa.clear();
16 maxc=1;
17 for(i=1;i<=n;i++){
18 scanf("%d",&b);
19 aa[b]++;
20 if(maxc<aa[b])maxc=aa[b];
21 }
22 printf("%dn",maxc);
23 }
24 return 0;
25 }
不过,还是为了练一下手,就是用比较简单的trie树吧!
代码:218ms
1 //#define LOCAL
2 #include<cstdio>
3 #include<cstring>
4 #include<cstdlib>
5 typedef struct node
6 {
7 struct node *child[10];
8 int id;
9 }Trie;
10 int max(int a,int b)
11 {
12 return a>b?a:b;
13 }
14 int Insert(char *s,Trie *root)
15 {
16 Trie *cur=root,*curnew;
17 int i,pos;
18 for(i=0;s[i]!=' ';i++)
19 {
20 pos=s[i]-'0';
21 if(cur->child[pos]==NULL)
22 {
23 curnew = new Trie;
24 curnew->id=0;
25 for(int j=0;j<10;j++)
26 curnew->child[j]=NULL;
27 cur->child[pos]=curnew;
28 }
29 cur=cur->child[pos];
30 }
31 cur->id++;
32 return cur->id;
33 }
34 void del(Trie *root)
35 {
36 Trie *cur=root;
37 for(int i=0;i<10;i++)
38 if(cur->child[i]!=NULL)
39 del(cur->child[i]);
40 delete cur;
41 }
42 char bb[32];
43 int main()
44 {
45 int n,ans,i;
46 #ifdef LOCAL
47 freopen("test.in","r",stdin);
48 #endif
49 Trie *root;
50 while(scanf("%d",&n)!=EOF)
51 {
52 root=new Trie;
53 for(i=0;i<10;i++)
54 root->child[i]=NULL;
55 ans=1;
56 while(n--){
57 scanf("%s",bb);
58 i=0;
59 while(bb[i]=='0'&&bb[i+1]!=' ')i++;
60 ans=max(ans,Insert(bb+i,root));
61 }
62 printf("%dn",ans);
63 del(root);
64 }
65 return 0;
66 }
注意几组数据:
4 004 04 0012 000
3 00 0 000
- 用最简单的图文带你看懂区块链
- 微信小游戏正式上线!小程序的一个类目,即点即玩!
- CentOS7安装GitLab、汉化、邮箱配置及使用
- Bootstrap源码分析之transition、affix
- Redis从单机到集群,一步步教你环境部署以及使用
- C++内存管理学习堆和栈
- 人人公司宣布收购美国卡车社区平台Trucker Path
- Mysql字符串截取总结:left()、right()、substring()、substring_index()
- c++程序的内存格局
- SpringMVC中利用@InitBinder来对页面数据进行解析绑定
- 门罗盘点:2017年那些AI击败人类的瞬间
- sizeof和strlen的区别
- mysql索引使用技巧及注意事项
- 条码支付新规,微信支付宝还好么?
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Apache下通过shell脚本提交网站404死链的方法
- centos查找已安装的jdk路径的方法
- CentOS7+apache+php7+mysql5.7配置教程详解
- 如何利用SystemTap统计函数执行耗时详解
- 关于安装linux redhat后无法使用yum命令安装gcc-c++问题的解决过程
- 基于代码的自动化和无代码自动化
- Java Stream中map和flatMap方法
- 关于Apache shiro实现一个账户同一时刻只有一个人登录(shiro 单点登录)
- 视频上云安防视频云服务平台EasyCVR通过海康sdk协议接入多路设备拉流时出现部分设备拉流不稳定是什么原因?
- Centos6.x服务器配置jdk+tomcat+mysql环境(jsp+mysql)
- Linux的压缩和解压缩的方法总结
- RTSP/GB28181/HIKSDK/Ehome协议视频融合平台EasyCVR编译C++报参见“XXX”的声明错误
- 详解 Linux 常用目录的作用
- CentOS6环境下搭建路由器的方法
- centos7下NFS使用与配置的步骤