Flying to the Mars(hdu 1800)(trie tree)
来源:互联网 发布:广电双向网络改造方案 编辑:程序博客网 时间:2024/05/17 13:13
Flying to the Mars(hdu 1800)
8888年,地球被PPF帝国统治着。由于人口的增长,PPF需要寻找更多土地让新出生的人生存。最终,PPF决定去攻打统治火星的Kscinow帝国。现在问题出现了。士兵怎么能到达火星呢?PPF召集他的将士们来征求他们的建议。由于火星上没有路,他们决定飞过去。现在他们开始学习骑扫帚飞行的技术。我们假设每个士兵有一个数字代表他的级别,级别高的士兵可以教级别低的士兵,但是级别低的士兵不能教比他级别高的。一个士兵最多只能有一个老师,或者没有老师也是有可能的。同样,一个士兵最多只能有一个学生,或者没有学生。老师可以跟学生用同一把扫帚来学习。在飞去火星前,每个士兵都要练习过。但是,魔法扫帚价格十分昂贵,所以请你帮PPF计算一下他们最少需要几把扫帚。
例如:
有5个士兵(A B C D E)级别分别是:2 4 5 6 4;
有一种方法:
C可以教B;B可以教A;所以,A,B,C可以用同一把扫帚来学习。
D可以教E;所以D,E用同一把扫帚;
这种方法的话,则需要2把扫帚。
另外一种方法:
D教A;所以A,D同一把扫帚。
C教B;所以B,C同一把扫帚。
E没有老师或学生,自己一把扫帚。
这样就需要3把扫帚。
……
通过讨论所有可能的方法,我们发现2是所有情况中最小的。
输入:
输入有多组数据。每组数据第一行是一个正整数N代表士兵的人数(0<=N<=3000),下面有N行,每行是一个非负整数(这个整数的位数小于30),代表士兵的级别。
输出:
对于每组数据输出一行,即所需扫帚的最小值。
输入样例:
4
10
20
30
04
5
2
3
4
3
4
输出样例:
1
2
分析:
对于给定的士兵级别都不相同,那么只需要一把扫帚即可。这样,问题就转化成了找出给出的数里重复的次数,重复次数最多的数的个数即为所求。将每个数当做字符串插入trie树,记录每个数出现的次数。最后找出重复出现次数的最大值即为所求。注意04与4都表示4,我们在插入时不将0插入。
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>using namespace std;int m;struct node{ node * next[10]; int num; node() { for(int i = 0; i < 10; i++) next[i] = 0; num = 0; }} *root;void insert(char s[]){ int k = 0; node *p = root; while(s[k] != '\0') { if( ! p->next[s[k]-'0']) p->next[s[k]-'0'] = new node(); p = p->next[s[k]-'0']; k++; } p->num ++; if(p->num > m) m = p->num;}int main(){ int n; while(cin >> n) { m = 0; root = new node(); for(int i = 0; i < n; i++) { char s[33]; cin >> s; int j = 0; while(s[j] == '0') j++; insert(s + j); } cout << m << "\n"; } return 0;}
- Flying to the Mars(hdu 1800)(trie tree)
- hdu 1800 Flying to the Mars (trie树)
- HDU -- 1800 Flying to the Mars (Trie 树)
- HDU 1800-Flying to the Mars(trie树)
- HDU 1800 Flying to the Mars(trie)
- hdu 1800 Flying to the Mars trie
- HDU 1800 Flying to the Mars // Trie
- HDU 1800 Flying to the Mars Trie或者hash
- hdu 1800 flying to the Mars(BKDR Hash)
- hdu 1800 Flying to the Mars(水 ,贪心)
- hdu 1800 Flying to the Mars(贪心)
- HDU 1800 Flying to the Mars(字典树)
- Hdu oj 1800 Flying to the Mars(Map)
- HDU 1800 Flying to the Mars(字符串Hash)
- hdu 1800 Flying to the Mars(多解)
- hdu 1800 Flying to the Mars (字典树)
- Flying to the Mars (hash) 1800 hdu
- hdu 1800 Flying to the Mars
- 【django3】Django学习笔记3:Model,Template,View 基本概念
- [BZOJ2741][[FOTILE模拟赛]][可持久化Trie+分块]
- 主成分分析PCA
- C++指针
- ext4 ext4_mount
- Flying to the Mars(hdu 1800)(trie tree)
- js中将类数组转换为数组的几种方法
- virtualbox中怎么给linux添加磁盘空间
- 大数据时代应该如何投资股票
- adnroid.appwidget.AppWidgetProvider
- Java访问权限public、protected、默认、private
- Struts2动态调用方法的常见方式
- 以太坊开发--阿里云ECS(centOS 6.8)安装Ethereum TestRPC
- PAT 1070. 结绳(25)-乙级