HDU 1800 Flying to the Mars(字典树水题)
来源:互联网 发布:广联达施工进度软件 编辑:程序博客网 时间:2024/06/05 11:00
题意:士兵要学骑扫帚。每个士兵有一个level,level高的能在同一把扫帚上教level低的怎么骑。一个人最多有一个老师,一个学生。也可以没有。给n个士兵的level值,问最少需要多少扫帚。
mark:显然就是找出现次数最多的数字的次数。但因为数字长度多达30个字符,因此long long都存不下,用字符串。
wa了一次,忘记处理前导0。
AC代码:
#include<iostream>#include<cstring>#include<cstdio>using namespace std;struct Trie{ int n; Trie *next[10]; Trie() //构造函数 { n = 0; memset(next,0,sizeof(next)); }};char s[35];int n,maxnum;void Insert(Trie *root,char s[]){ Trie *p = root; for(int i = 0; s[i]; i++) { int j = s[i] - '0'; if(p->next[j] == NULL) p->next[j] = new Trie(); p = p->next[j]; } p->n++; if(p->n > maxnum) maxnum = p->n;}void Delete(Trie *root) //释放空间{ for(int i = 0; i < 10; i++) { if(root->next[i]) { Delete(root->next[i]); delete(root->next[i]); } } root = NULL;}int main(){ //freopen("1002.txt","r",stdin); while(scanf("%d",&n) != EOF) { int i; Trie *root = new Trie(); maxnum = 0; while(n--) { char c[35]; scanf("%s",s); for(i = 0; s[i]; i++) //去前导0 if(s[i]!='0' || !s[i+1]) { strcpy(c,&s[i]); break; } Insert(root,c); } printf("%d\n",maxnum); Delete(root); } return 0;}
0 0
- 字典树水题Flying to the Mars HDU 1800
- HDU 1800 Flying to the Mars(字典树水题)
- hdu 1800 Flying to the Mars(字典树)
- [字典树、map] HDU 1800 - Flying to the Mars
- hdu 1800 Flying to the Mars 字典树
- HDU 1800 Flying to the Mars(字典树)
- hdu 1800 Flying to the Mars 字典树
- hdu 1800 Flying to the Mars 字典树
- 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
- Hdu-1800 Flying to the Mars
- hdu 1800 Flying to the Mars
- hdu 1800 Flying to the Mars
- HDU-1800-Flying to the Mars
- hdu 1800 Flying to the Mars
- hdu 1800 Flying to the Mars
- [Spring Boot Debug]在 intellij idea 中无法编译 javad代码 -source 1.6 中不支持 diamond 运算符
- jQuery 的属性选择器
- 内联函数
- android中log的区别
- Unicode码和中文互转
- HDU 1800 Flying to the Mars(字典树水题)
- mac下安装mysql
- libnet资料整理
- 匿名函数的必包形式
- fprintf
- linux 版本中 i386/i686/x86-64/pcc 等... 的区别
- char * a, char ** a, char * a[], char a[][], char * a[][], char ** a[][], char * a [][][], and so on
- HTML 注释
- java中遍历Map的方法