http://acm.hdu.edu.cn/showproblem.php?pid=1800

题目大意:

又是废话连篇

给你一些由数字组成的字符串,判断去掉前导0后那个字符串出现频率最高。

一开始敲hash数组开大点嘛。。TLE,开小WA。。肯定是我hash函数写得不好。QAQ

然后我就直接上字典树了。。。

再然后此题的哈希我问了大牛他是直接存hash值我是存个数。。。然后我就牺牲了

然后我又敲了一遍,然后就AC了。

然后就没有然后了。

方法一:字典树Trie

#include<cstdio>
#include<cstring>
struct node
{
char c;
int cnt;
node *next[10];
node()
{
cnt=0;
for(int i=0;i<10;i++)
next[i]=NULL;
}
};
struct Trie
{
node * root;
int insert(char *s)
{
while(*s=='0')
s++;
int len=strlen(s);
node *cur=root;
for(int i=0;i<len;i++)
{
int id=s[i]-'0';
if(cur->next[id]==NULL)
{
node *temp=new node;
temp->c=s[i];
cur->next[id]=temp; }
cur=cur->next[id];
} return ++cur->cnt;
}
}trie; int main()
{
int n;
while(~scanf("%d",&n))
{
trie.root=new node;
char temp[32];
int ans=1;
for(int i=0;i<n;i++)
{
scanf("%s",temp);
int cnt=trie.insert(temp);
if(ans < cnt)
ans=cnt;
}
printf("%d\n",ans);
} return 0;
}

方法二:hash

关于hash函数选取,选择比给定的个数大的素数就可以了。要素数,才能减少冲突!

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MAXN=3010;
LL hash_id[MAXN]; LL insert(char *s)
{
while(*s=='0')
s++; LL id=0;
for(;*s!='\0';s++)
id=id * 11 + *s-'0'; return id;
} int main()
{
int n;
while(~scanf("%d",&n))
{
char temp[32];
for(int i=0;i<n;i++)
{
scanf("%s",temp);
hash_id[i]=insert(temp);
}
sort(hash_id,hash_id+n);
int ans,cur;
ans=cur=1; for(int i=1;i<n;i++)
{
if(hash_id[i]==hash_id[i-1])
cur++;
else
{
cur=1;
}
if(cur > ans)
ans=cur;
} printf("%d\n",ans);
} return 0;
}

HDU 1800 Flying to the Mars Trie或者hash的更多相关文章

  1. hdu 1800 Flying to the Mars

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

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

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

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

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

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

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

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

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

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

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

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

  8. hdu---(1800)Flying to the Mars(trie树)

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

  9. HDOJ.1800 Flying to the Mars(贪心+map)

    Flying to the Mars 点我挑战题目 题意分析 有n个人,每个人都有一定的等级,高等级的人可以教低等级的人骑扫帚,并且他们可以共用一个扫帚,问至少需要几个扫帚. 这道题与最少拦截系统有异 ...

随机推荐

  1. javaweb一

    JavaWeb就是在服务器端以Java语言为解释运行基础的web程序. php代码要想在服务器端运行,需要在服务器软件(通常是Apache)上加php的解释器,Java也一样,但是这个解释器是Tomc ...

  2. 玲珑学院 1050 - array

    1050 - array Time Limit:3s Memory Limit:64MByte Submissions:494Solved:155 DESCRIPTION 2 array is an ...

  3. STM32上使用JSON

    一.STM32工程中添加JSON 最近在一网2串项目,串口和网口之间可能需要定义一下简单的通信协议,而通信协议上则需要去定义一下通信的数据格式,上次听剑锋说要用Json来定义,目前查了下资料具体如何去 ...

  4. 【习题 8-6 UVA - 1611】 Crane

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 想把数字i从位置j移动到位置i 可以这样. 假设mov(x,y)表示将(x..x+len/2-1)和(x+len/2..y)交换. ...

  5. 程序猿的量化交易之路(14)--Cointrader数据表(2)

    Cointrader表结构 转载须注明出处:http://blog.csdn.net/minimicall?viewmode=contents,http://cloudtrader.top 设置(se ...

  6. 32款iOS开发插件和工具介绍[效率]

    插件和工具介绍内容均收集于网络,太多了就不一一注明了,在此谢过!   1.Charles 为了调试与server端的网络通讯协议.经常须要截取网络封包来分析. Charles通过将自己设置成系统的网络 ...

  7. SGU 253 Theodore Roosevelt 快速判断点是否在凸包内

    http://acm.sgu.ru/problem.php?contest=0&problem=253 题意简单易懂...给你n个点的凸包(经测试已经是极角序)...判断m个点是否在凸包内.. ...

  8. C# 解决ListView控件显示数据出现闪屏的问题

    一.发现问题 如果发送数据过快的情况下,ListVies滚屏显示数据时会显示闪屏,如下所示现象: 二.解决问题 根据出现闪屏的情况,在网上查了资料要使用双缓存的办法来处理.其原理是数据在缓存区中进行处 ...

  9. CISP/CISA 每日一题 17

     CISSP 每日一题(答) What are often added to passwords to maketheir resultant hash secure and resistant to ...

  10. tomcat做成windows服务之后使用JMX监控的问题

    转载:http://blog.chinaunix.net/uid-20449851-id-2369842.html