hdu-1800
思路:
这题被坑的不轻。
首先花了一段时间想明白了思路是要找出现次数最多数字,以为这题就这样解决了,结果发现每个数字的最大长度是30,long long都装不下,因此就要用字符串来保存处理。然后在insert的时候进行一下计数就可以了
最关键的地方是struct内的构造函数中,在初始化next数组的时候,一定要面面俱到!不然漏掉哪个,如果没有初始化,就相当于为空指针,就会出现内存泄露,就因为这点被卡了好久。
AC代码:
#include <iostream>
#include <cstring>
#define max(a,b) (a)>(b)?(a):(b)
using namespace std; struct node
{
int e;
node* next[];
node():e(){
for(int i = ;i <= ;i++)
next[i] = ;
}
};
node* root;
int ans; void insert(char* s)
{
int n;
node* p = root;
for(;*s!='\0';s++)
{
n = *s-'';
if(p->next[n] == )
p->next[n] = new node();
p = p->next[n];
}
p->e++;
ans = max(ans,p->e);
} int main()
{
int j;
int n;
while(cin>>n)
{
ans = ;
root = new node();
char str[];
for(int i = ;i <= n;i++) {
cin>>str;
for(j = ;j < strlen(str);j++)
if(str[j] != '')
break;
insert(&str[j]);
}
cout<<ans<<endl;
}
return ;
}
hdu-1800的更多相关文章
- --hdu 1800 Flying to the Mars(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Ac code: #include<stdio.h> #include<std ...
- hdu 1800 (map)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/ ...
- 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 字典树,STL中的map ,哈希树
http://acm.hdu.edu.cn/showproblem.php?pid=1800 字典树 #include<iostream> #include<string.h> ...
- HDU 1800 Flying to the Mars Trie或者hash
http://acm.hdu.edu.cn/showproblem.php?pid=1800 题目大意: 又是废话连篇 给你一些由数字组成的字符串,判断去掉前导0后那个字符串出现频率最高. 一开始敲h ...
- Hdu 1800 字符串hash
题目链接 题意: 给出n(n<=3000)个字符串(长度<30,数字组成,肯能存在前导0), 问该序列最少可以分成多少个单调序列.可以转化成求相同字符串的个数的最大值 附上代码: /*** ...
- hdu 1800 Flying to the Mars(简单模拟,string,字符串)
题目 又来了string的基本用法 //less than 30 digits //等级长度甚至是超过了int64,所以要用字符串来模拟,然后注意去掉前导零 //最多重复的个数就是答案 //关于str ...
- 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 hash 找出现最多次数的字符串的次数
乘法hash: 这类hash函数利用了乘法的不相关性 int Hash(char *str){ int seed = 131 , value=0; while(*str != '\0'){ ...
随机推荐
- 表达式求值 (栈) 用C++实现
#include <cstdio> #include <cstdlib> #include <cmath> #include <stack> #incl ...
- 使用nice命令调整进程优先级
Adjusting Process Priority with nice When Linux processes are started, they are started with a spe ...
- Managing linux Shell Jobs
Managing Shell Jobs When moving jobs between the foreground and background, it may be useful to ha ...
- python 下的数据结构与算法---7:查找
一:线性查找(Sequential Search) 线性查找可以说是我们用的最早也会是用的最多的查找方式了.其对应的是线性数据结构,回顾一下线性数据结构,其特点是先后加入的元素是有顺序的,相邻的.而线 ...
- (转)jquery.validate.js 的 remote 后台验证
之前已经有一篇关于jquery.validate.js验证的文章,还不太理解的可以先看看:jQuery Validate 表单验证(这篇文章只是介绍了一下如何实现前台验证,并没有涉及后台验证remot ...
- 前端CSS兼容的一些思路
半夜睡不着觉,起来写第一博. 近段时间,公司要给一个网站产品增加一个换色功能,安排我负责该事项. 之前参与过一些定制项目,是基于该产品的二次开发,说实话里面的前端结构很混乱.所以第一步就是将html前 ...
- java.io.FileNotFoundException: class path resource [bean/test/User.hbm.xml] cannot be opened because it does not exist
确定下 WEB-INF/classes下有没有,不是src下哦 工程的src下创建后,会发布到tomcat下项目下的classes中
- 第一次启动MySQL时报错
[root@localhost~]#/usr/local/webserver/mysql/bin/mysql_install_db --basedir=/usr/local/webserver/mys ...
- PL/SQL中的变量案例解析
1.标量: ag1: declare v_ename emp.ename%type;--自己称为单变量 begin select ename into v_ename from emp where e ...
- 关于cocopads 不能正确安装的问题
通过几个网页 我搜到 看着几个网页就够了 绝对可以实现的 http://code4app.com/article/cocoapods-install-usage http://www.cnblogs. ...